/**
 * stepSlider - Step by step translate  slider items
 * @version: 0.3.1 - (2010/10/26)
 * @requires jQuery v1.4.2 or later 
 * @author Nicolae Gabriel
  
 * Licensed under MIT licence:
 *   http://www.opensource.org/licenses/mit-license.php
 **/

 	function stepSlider(elemSelector, elNavigation, options){
		this.elSlider = $(elemSelector);
		this.elItemsCount = $('li',elemSelector).size();
		this.selPosition = 1;
		this.selPositionMax = 4;
		this.lastElem = 0;
		this.selItem = 1;
		
		this.allInitEfects = new Array(3);
		
		this.allInitEfects[0] = 'fade';
		this.allInitEfects[1] = 'slideUp';
		this.allInitEfects[2] = 'slideDown';
		this.allInitEfects[3] = 'randomEach';
		
		this.initPositions = function() {
			crrPosition = 0;
			crrCounter = 0
			
			$('li', elSlider).each(function(index) {
				crrCounter=crrCounter+1;
				
				$(this).addClass('stepSlider-item-'+crrPosition);
				
				if (crrCounter>4){
					$(this).addClass('stepSlider-hidden');
				}
				
				if (crrPosition == selPositionMax-1){
					crrPosition = 0;
				}else {
					crrPosition = crrPosition +1;
				}
			  });
		}
		
		this.showItems = function (){
			if (options.initEffect=='random'){
				radomIndex = Math.floor(Math.random()*4);
				options.initEffect = allInitEfects[radomIndex];
			}
			
			setTimeout(function(){
				var firstItem = $('li:eq(0)', elSlider);
				chainShow(firstItem, 4);
			}, 1200)
		}
		
		this.chainShow = function(item, onIndex){
			var index = $(item).index();
			var currentEffect = options.initEffect;
			
			//log ('currentEffect: '+currentEffect+' - index:'+index);
			
			if (options.initEffect=='randomEach'){
				radomIndex = Math.floor(Math.random()*3);
				currentEffect = allInitEfects[radomIndex];
				
				//log ('randomShowEffect: '+currentEffect+' - index:'+index);
			}
			
			if (currentEffect == 'fade'){
				$(item).css('top','0px')
					   .delay(100)
					   .fadeIn('slow', function(){
							if (onIndex-1>index){ chainShow( $(item).next(), onIndex); }
						});
			}
			
			if (currentEffect == 'slideUp'){
				$(item).css({ 	top: '280px', 
							display: 'block'})
					   .delay(100)
					   .animate(
						{       top :'0px'},
						{  duration :450,
						     easing :'easeOutExpo',
						   complete : function(){
								
								if (onIndex-1>index){ chainShow($(item).next(), onIndex); }
							} 
						});
			}
			
			if (currentEffect == 'slideDown'){
				$(item).css({ 	top: '-260px', 
							display: 'block'})
					   .delay(100)
					   .animate(
						{       top :'0px'},
						{  duration :450,
						     easing :'easeInExpo',
						   complete : function(){
								
								if (onIndex-1>index){ chainShow($(item).next(), onIndex); }
							} 
						});
			}
		}
		
		this.init = function (){
			selPosition = selPositionMax;
			selItem = selPositionMax;
			
			showItems();
			
			initPositions();
			
			$('.next', elNavigation).click(function(){
				
				
				if (elItemsCount > selItem){
					//log('Click: next');
					
					if (selPosition == selPositionMax){
						newPosition = 1;
					}else{
						newPosition = selPosition+1;
					}
					
					setPosition(newPosition,selItem+1, 'next');
				}else{
					transitionFixed(lastElem,1);
				}
				
				return false;
			});
			
			$('.prev', elNavigation).click(function(){
				if (selItem>4){
					//log('Click: prev');
					
					if (selPosition == 1){
						newPosition = 4;
					}else{
						newPosition = selPosition-1;
					}
					
					setPosition(newPosition, selItem-1, 'prev');
				}else{
					lastElem = $('li:eq(0)', elSlider)
					transitionFixed(lastElem,1);
				}
				
				return false;
			});
			
			//log('Found Elements:'+elItemsCount);
		};
	
		this.setPosition = function(crrPosition, newSelection, direction){
			if (direction=='next'){		
				selPosition = crrPosition;
				selItem = newSelection;
			
				oldEl = $('li:eq('+(newSelection-5)+')' ,elSlider);
				elNew = $('li:eq('+(newSelection-1)+')' ,elSlider);
			
				transitionDown(oldEl, elNew);
			}
			
			if (direction=='prev'){		
				selPosition = crrPosition;
				selItem = newSelection;
				
			
				oldEl = $('li:eq('+(selItem)+')' ,elSlider);
				elNew = $('li:eq('+(selItem-4)+')' ,elSlider);
			
				transitionUp(oldEl, elNew);
			}
				
			//log('SelectedItem: '+(selItem)+' Position:'+crrPosition);
		}
		
		this.transitionUp = function (elOld, elNew){
			translateHeight = 280;
			$(elNew).css('top',translateHeight+'px');
			
			$(elOld).animate(
				{ top:'-'+translateHeight+'px' },
				{	 duration:450,
					easing:'easeInExpo',
					  step: function( top ){
						newTop = parseInt(top)+translateHeight;
						
						$(elNew).css('top',newTop+'px');
					}, complete:function(){

					}
				}
			);
		}
		
		this.transitionDown = function (elOld, elNew){
			translateHeight = 280;
			$(elNew).css('top','-'+translateHeight+'px');
			lastElem = elNew;
			
			$(elOld).animate(
				{ top:translateHeight+'px' },
				{	 duration:450,
					easing:'easeInExpo',
					  step: function( top ){
						newTop = parseInt(top)-translateHeight;
						$(elNew).css('top',newTop+'px');
					}, complete:function(){

					}
				}
			);
		}
		
		this.transitionFixed = function(elCurrent, repeat){
			translateHeight = 8;
			
			//log('Transition Fixed!');
			
			$(elCurrent).animate(
				{	 top:translateHeight+'px' },
				{	 duration:120,
						easing:'easeInExpo',
					complete:function(){

						$(elCurrent).animate(
							{ top:'0px' },
							{	 duration : 120,
									easing : 'easeInExpo', 
								complete : function(){
									if (repeat>0){
										repeat = repeat -1;
										transitionFixed(elCurrent, repeat);
									}
								}
							});
				
					
					}
				});
		}
	
		this.init();
	}
