//Generic Site helpers class
XX.slideShow = Class.create();

XX.slideShow.prototype = {
	/*********************************/
	//Constructor
	initialize: function( opts ){
		
		opts = opts || {};

		//General Options
		this.debug = false;
		this.objectName = opts.objectName ? opts.objectName : "xxSlideSlow";
		
		this.imgs = opts.imgs ? opts.imgs : false; //The image urls to use in the slide show
		this.elementContainer = opts.elementContainer ? opts.elementContainer : "slideShow"; // The id of the element to display the slide show
		this.timePerSlide = opts.timePerSlide ? opts.timePerSlide : 5000; //The time in ms to show each slide
		this.effectIn = opts.effectIn ? opts.effectIn : "Appear"; //the effect that should be used to display new slide
		this.effectOut = opts.effectOut ? opts.effectOut : "Fade"; //the effect that should be used to remove old slide
		this.effectDur = opts.effectDur ? opts.effectDur : 1; // the time the effect should take
		
		this.totalSlides = this.imgs.length;
		this.currentSlide = false;
		
		// Cache the images.
		if( this.imgs ) {
			this.cacheImages( this.imgs );
		} else {
			return true;
		}
		
		
		//Insert images into page
		this.injectSlides();
		
		//Start it up
		this.start();

		
	},


	/*********************************/
	showSlide: function(  ){

		//Stop if slideshow goes away
		if( ! $(this.elementContainer)){ return true; }

		//alert("Going to show slide "+this.currentSlide+" of "+this.imgs.length);
		//Effects option string
		effectFinishFunc = new Function(this.objectName+".startRemoveTimer();");
		
		effectOpts = "duration:"+this.effectDur+", afterFinish:effectFinishFunc";
			
		//Show Slide
		eval("Effect."+this.effectIn+"('"+this.elementContainer+"Slide"+this.currentSlide+"', {"+effectOpts+"});");
		//alert($(this.elementContainer+"Slide"+this.currentSlide));
		
		//If this is the only slide then just quit
		if( this.imgs.length == 1 ) {
			this.stop();
		}
		
	},
	
	
	/*********************************/
	startRemoveTimer: function() {
		//Stop if slideshow goes away
		if( ! $(this.elementContainer)){ return true; }

		//Set a timer to remove this slide
		this.slideTimer = window.setTimeout(""+this.objectName+".removeSlide();", this.timePerSlide);	
	
	},
	
	/*********************************/
	removeSlide: function(  ){

		//Dont remove if stopped
		if( this.running == false ||  ! $(this.elementContainer) ) {
			//Kill timer if it is going
			clearTimeout(this.slideTimer);

			return true;
		}

		//Set old Slide
		this.killSlide = this.currentSlide;
		
		//Determine which slide to show next
		if( (this.currentSlide + 1) == this.imgs.length ) {
			//Reset to 0
			this.currentSlide = 0;
		} else {
			this.currentSlide = (this.currentSlide) + 1;
		}
		
		//Effects option string
		effectFinishFunc = new Function(this.objectName+".showSlide();");
		effectOpts = "duration:"+this.effectDur+", afterFinish:effectFinishFunc";
			
		//Remove Slide
		eval("Effect."+this.effectOut+"('"+this.elementContainer+"Slide"+this.killSlide+"', {"+effectOpts+"});");
		//Effect.Fade("slideShowSlide0");
		
	},
	
	

	/*********************************/
	stop: function(  ){
		
		//Kill timer if it is going
		clearTimeout(this.slideTimer);

		this.running = false;
		
		//Set to start when clicked on element
		if( eObj = $(this.elementContainer)) {
			eObj.onclick = new Function(this.objectName+".start();");
		}
		
		
	},
	
	/*********************************/
	start: function(  ){

		if(! $(this.elementContainer)) { return true; }

		//Set current Slide to 0
		this.currentSlide = 0;
		
		//Kill timer if it is going
		clearTimeout(this.slideTimer);

		this.running = true; // Set running var

		//Make sure that the image are hidden
		for( i=0; i < this.imgs.length; i++) {
			//Insert img code
			Element.hide(this.elementContainer+"Slide"+i);
		}
		
		
		//Set to stop when clicked on element
		if( eObj = $(this.elementContainer)) {
			eObj.onclick = new Function(this.objectName+".stop();");
		}
		
		//Show it
		this.showSlide();
	},
	
	
	/*********************************/
	injectSlides: function(  ){
		
		//Loop imgs
		for( i=0; i < this.imgs.length; i++) {
			//Insert img code
			new Insertion.Top(this.elementContainer, "<img id='"+this.elementContainer+"Slide"+i+"' src='"+this.imgs[i]+"' style='display:none;'>");
		}
		
	},
	
	

	/*********************************/
	// Replaces all occurances of {cmsObjectName} with the objectName in the he passed txt argument
	cacheImages : function( imgs ) {
		
		if(! this.cacheContainer ) { this.cacheContainer = new Array(); }
    
		var i, j = this.cacheContainer.length; 
		
		for(i=0; i < imgs.length; i++) {
    	if ( imgs[i].indexOf("#") != 0 ){ 
				this.cacheContainer[j] = new Image; 
				this.cacheContainer[j++].src = imgs[i];
			}
		}
		
	},
	

	
	

	/*********************************/
	//Test function
	tester : function( inp ) {
		inp = inp || this.objectName;
		alert(inp);

	}
	
	
	
	
}

