/*
	20100907	tomc
	eclipse-creative.com
*/
var EclipseSlideShow = new Class({
	Implements		:[Options],
	options			:{
		parent		:false
	},
	slideLimit		:false,
	showInterval	:false,
	
	/*!
		CONSTRUCTOR
	*/
	initialize:function(options){
		this.setOptions(options);
		if(!this.options.parent){
			throw("invalid element");
		}
		
		//
		//	TRY TO SORT OUT PARENT ELEMENTS
		//
		// right this brings the slideshow to the front of body //
		var slideLimit 		= new Element("div",{
			"styles":{
				"overflow"		:"hidden",
				"position"		:"absolute",
				"z-index"		:10,
				"top"			:190,//this.options.parent.getPosition( document.getElement("body") ).y-32,
				"width"			:"100%",
				"height"		:430,//this.options.parent.getSize().y,
				"margin"		:"auto"
				,"opacity"		:0
				//,"background"	:"lime"
				//,"outline":"1px dotted red"
			}
		}).inject( document.getElement("body"),"bottom");
		var dummy = this.options.parent.clone();
		dummy.empty().inject(this.options.parent, "after");
		this.options.parent.inject(slideLimit);
		
		
		//
		//	DECIDE SLIDES
		//
		var slides = this.options.parent.getChildren(".slide");
		if(!slides.length){
			// fail //
			return;
		}
		slides.setStyles({
			left		:"auto",
			right		:"auto",
			display		:"none"
		});
		slides[0]
			.addClass("active")
			.setStyle("display","block")
		;
		
		//
		//	MAKE BUTTONS FUNCTIONAL
		//
		this.options.parent.getElements(".nav-left").addEvents({
			click:function(e){
				this.endShow();
				this.nextSlide(-1);
			}.bind(this)
		});
		this.options.parent.getElements(".nav-right").addEvents({
			click:function(e){
				this.endShow();
				this.nextSlide(1);
			}.bind(this)
		});
		
		
		// TRANSITON //
		this.options.parent.setStyles({
			"visibility"	:"visible"
		});
		slideLimit.fade(1);
		
		this.beginShow();
	},
	

	/*!
		BEGIN SLIDE SHOW
	*/
	beginShow:function(){
		this.showInterval = this.nextSlide.periodical( 10000, this, [1] );
	},
	
	
	/*!
		END SLIDE SHOW
	*/
	endShow:function(){
		$clear(this.showInterval);
	},
	
	
	/*!
		slide next
		directon: 1 or -1
	*/
	nextSlide:function( direction ){
		if( !direction || direction < -1 || direction > 1 ){
			direction = 1;
		}
		
		var slideLimit	= this.slideLimit;
		var parent 		= this.options.parent;
		var slides 		= this.options.parent.getChildren(".slide");
		var activeSlide	= parent.getElement(".active");
		var nextSlide 	= false; 
		
		if( !activeSlide ){
			activeSlide = slides[0];
		}
		
		// DECIDE NEXT SLIDE //
		switch( direction ){
			case 1:
				if( !(nextSlide = activeSlide.getNext(".slide") ) ){
					nextSlide = slides[0];			// back to start //
				}
				
			break;
			
			case -1:
				if( !(nextSlide = activeSlide.getPrevious(".slide")) ){
					nextSlide = slides.getLast();	// back to end //
				}
			break;
		}
		
		// CANNOT ANIMATE SINGLE SLIDE //
		if( activeSlide == nextSlide ){
			return;
		}
		
		
		// freeze any transitions //
		// ....
		
		activeSlide.removeClass("active");
		nextSlide.addClass("active");
		
		//
		// active slide goes off screen to left
		//
		var xTween
			= activeSlide.getPosition(slideLimit).x
			+ activeSlide.getSize().x
		;
		activeSlide.fade.delay(800,activeSlide,[0]);
		activeSlide.set('morph', {
			fps			:50,
			duration	:1200,
			transition	:Fx.Transitions.Sine.easeInOut,
			onComplete	:function(){
				//activeSlide.fade(0);
			}
		});
		activeSlide.setStyles({
			"left"	:"auto",
			"right"	:"auto"
		});
		switch(direction){
			case 1:
				activeSlide.morph({"left":[0,0-xTween]});
			break;
			
			case -1:
				activeSlide.morph({"right":[0,0-xTween]});
			break;
		}
		
		//
		// next slide scolls from right to center
		//
		nextSlide.set('morph', {
			fps			:50,
			duration	:1200,
			transition	:Fx.Transitions.Sine.easeInOut,
			onComplete	:function(){
				
			}
		});
		//nextSlide.fade.delay(200,nextSlide,[1]);
		nextSlide.fade(1);
		switch(direction){
			case 1:
				nextSlide.setStyles({
					"opacity"	:0,
					"display"	:"block",
					"right"		:0-xTween,
					"left"		:"auto"
				}).morph({"right":0});
			break;
			
			case -1:
				nextSlide.setStyles({
					"opacity"	:0,
					"display"	:"block",
					"right"		:"auto",
					"left"		:0-xTween
				}).morph({"left":0});
			break;
		}
	}
}); 

