/*
	eclipse-creative.com
	20100526 tomc / ashd
*/
/*!
	PRODUCT CYCLER
	
*/
var ProductScroller = new Class({
	Implements	: Options,
	options 	: {
		element			:false,
		delay			:2000,
		transitionTime	:1900,
		transition		:'pow:out',
		direction		: -1,
		leftButtonHtml	:'&lt',
		rightButtonHtml	:'&gt'
	},
	items 			:[],
	cycleHandle		:false,
	dyingElement	:false,
	dyingHandle		:false,
	
	/*
		CONSTRUCTOR
	*/
	initialize	:function(options){
		this.setOptions(options);
		if(!this.options.element) throw("Invalid Element");

		
		// DECIDE & POSITION ITEMS //
		var offsetX	= 0;
		this.items	= this.options.element.getElements(".item");
		if( !this.items.length ) return;
		this.items.each(function(el,i,items){
			
			el.set('tween', {
				duration	:this.options.transitionTime,
				transition	:this.options.transition
			});
			
			el.setStyles({
				position	:"absolute",
				left		:offsetX
			});
			
			offsetX += el.getSize().x;
			
		},this);
		
		this.sortItems();
		
		this.createButtons();
		
		this.cycleHandle = this.cycleProducts.periodical( this.options.delay, this, this.options.direction );
	},
	
	
	/*
		create nav buttons
		?? they cant intrrupt transition
	*/
	createButtons	:function(){
		
		new Element('a',{
			html			:this.options.leftButtonHtml,
			'class'			:'click left-button',
			styles			:{
				position	:'absolute',
				zIndex		:1000
			},
			events			:{
				click		:function(){
					
					
					this.freezeCycle();
					this.cycleProducts( 1 );
					
					
				}.bind(this)
			}
		}).inject(this.options.element, 'bottom');
		
		new Element('a',{
			html			:this.options.rightButtonHtml,
			'class'			:'click right-button',
			styles			:{
				position	:'absolute',
				zIndex		:1000
			},
			events			:{
				click		:function(){
					
					
					this.freezeCycle();
					this.cycleProducts( -1 );
					
					
				}.bind(this)
			}
		}).inject(this.options.element, 'bottom');
	},
	
	
	/*
		STOP FOR AMOUNT OF TIME
		0 = forever
	*/
	freezeCycle		:function( time ){
		$clear(this.cycleHandle);
		this.killTransitions();
		//if(time) cycleHandle = (){perodical}.delay(time)....
	},	
	
	
	/*
		kill transitions
	*/
	killTransitions	:function(){
		$clear(this.dyingHandle);
		
		this.items.each(function(el){
			el.get('tween').cancel();
			// MOVE TO DESTINATION //
			if( $defined(el.tweenDestination) ){
				el.setStyle('left', el.tweenDestination );
			}
		});
		
		this.destroyItem();
	},
	
	
	/*
		sort the items array by x position
	*/
	sortItems:function(){
		this.items.sort(function(a,b){
			return a.getPosition(this.options.element).x - b.getPosition(this.options.element).x
		}.bind(this));
	},
	
	
	/*
		end of tween??
	*/
	destroyItem:function(){
		if( this.dyingElement ){
			this.items.erase( this.dyingElement );
			this.dyingElement.destroy();
			this.dyingElement = false;
		}
	},
	
	
	/*
		SHUFFLE / TWEEN ITEMS
		
		todo: sort could be avoided just by inserting the clone in
		the appropriate place 
	*/
	cycleProducts	:function( direction ){
		if( !this.items || !this.items.length ) throw("Invalid items");
		if( !direction || direction < -1 || direction > 1 ) direction = 1;
		this.killTransitions();
		
		// note this only works because the array was sorted previously //
		var firstEl			= this.items[ 0 ];
		var lastEl			= this.items.getLast();
		
		switch(direction){
			
			// items go + on X (move to right) //
			case 1:
				// clone the last el to a position before the beginning //
				var clone = lastEl.clone();
				clone.setStyles({
					left	: 0 - firstEl.getSize().x
				}).set('tween', {
					duration	:this.options.transitionTime,
					transition	:this.options.transition
				}).inject(this.options.element);;
				
				// add clone to items //
				this.items.push(clone);
				this.sortItems();
				
				// tween items //
				var offset = this.items[0].getPosition(this.options.element).x;
				this.items.each(function(el,i,items){
					el.tweenDestination = offset + el.getSize().x;
					el.tween('left', el.tweenDestination );
					offset += el.getSize().x;
				},this);
			
				// set lastEl on self destruct //
				this.dyingElement	= lastEl;
				this.dyingHandle	= this.destroyItem.delay(this.options.transitionTime, this );
			break;
			
			
			// items go - on X (move to left) //
			case -1:
				var clone = firstEl.clone();
				clone.setStyles({
					left	: lastEl.getPosition(this.options.element).x + lastEl.getSize().x
				}).set('tween', {
					duration	:this.options.transitionTime,
					transition	:this.options.transition
				}).inject(this.options.element);
				
				// add clone to items //
				this.items.push(clone);
				this.sortItems();
				
				// tween the items //
				var offset = 0;
				this.items.each(function(el,i,items){
					el.tweenDestination = offset - el.getSize().x;
					el.tween('left', el.tweenDestination );
					offset += el.getSize().x;
				},this);
				
				// set firstEl on self destruct //
				this.dyingElement	= firstEl;
				this.dyingHandle	= this.destroyItem.delay(this.options.transitionTime, this);
			break;
			
			
			default:
				throw("Invalid direction");
			break;
		}
	}
});
