/*	+-----------------------------------------------------+
	| COPYRIGHT 2011 GUSTAVE HILL (ABLE DAEMON)				|
	| =========================================				|
	| YOU ARE FREE TO DISTRIBUTE THIS SOFTWARE; 				|
	| THIS COPYRIGHT NOTICE (THIS BEAUTIFUL ASCII BOX)		|
	| MUST BE INCLUDED.												|
	| Find more great software @ http://abledaemon.net/ 	|
	| Thanks for your usage and / or support! :-)			|
	+-----------------------------------------------------+ */

/*	Show and Tell Image Slider
	--------------------------
	An extensible image slideshow with
		1. Fading text
		2. "Growing" text located at the top or the bottom
			of the slide show
		3. A host of configurable options */

var ImageSlider=Class.create({

	initialize: function (newUid) { 
		this.uid = newUid;
		this.img = new Array();
		this.title = new Array();
		this.desc = new Array();
		this.nextAutoloadTime=new Date();

		this.lstImg=0; 
		this.curImg=0; 
		this.maxImg=-1;

		this.imgTimerOn=false; 
		this.txtTimerOn=false;

		this.timeElapsed=0;
		this.imgTime=200; 
		this.txtTime=100; 
		this.nxtImgTime=4000;
		this.txtIncTimeBy=this.txtTime/50;
		this.imgIncTimeBy=this.imgTime/65;
		this.pauseImgTime=200;
	
		this.ttlHeight=200;
		this.ttlOpacity=.9;

		this.firstRun=true; 
		this.curEvent='';

		this.autoloadNext=true; 
		this.inAnimationSeq=false;

		this.sliderShowText=true;
		this.sliderGrowText=true;
		this.sliderFadeText=true;

	},

	// Set some options
	showText: function(showTextBool) { this.sliderShowText=showTextBool; },
	growText: function(growTextBool) { this.sliderGrowText=growTextBool; },
	fadeText: function(fadeTextBool) { this.sliderFadeText=fadeTextBool; },
	setTxtHeight: function(txtHeight) { this.ttlHeight=txtHeight; },
	setTxtOpacity: function(txtOpc) { this.ttlOpacity=txtOpc; },
	setNextImgTime: function(nxtTime) { this.nxtImgTime=nxtTime; },
	setTxtOpacity: function(txtOpc) { this.ttlOpacity=txtOpc; },
	setImgTransTime: function(transTime) { this.imgTime=transTime; },
	setTxtTransTime: function(transTime) { this.txtTime=transTime; },
	setImgPauseTime: function(pauseTime) { this.pauseImgTime=pauseTime; },
	
	// Start the slide show
	startSlideShow: function() { 
		this.writeMainDivs();
		this.eventDirector(0,'autonext'); 
	},
	writeMainDivs: function() {
		// Writes the divs to the screen
		document.write('<div id="slideshow'+this.uid+'">'+
		 '<div id="slideshowimgs'+this.uid+'"></div>'+
		 '</div>');
	},
	// Go to the next / previous slide
	nextSlide: function() { this.eventDirector(1,'usr'); },
 	prevSlide: function() { this.eventDirector(-1,'usr'); },
	contAnimationSeq: function() { this.eventDirector(0,'animloop'); },
	contSlideShowSeq: function() { this.eventDirector(1,'autonext'); },

	addSlide: function(imgPath,titleTxt,descText) {
		this.maxImg++;
		this.img[this.maxImg]=imgPath;
		this.title[this.maxImg]=titleTxt;
		this.desc[this.maxImg]=descText;
		// Preload the image as well
		preloadPic=new Image();
		preloadPic.src=imgPath;
	},

	eventDirector: function(modInt,src) {
		rightNow=new Date();
		rightNow.getTime();
		if ((src=='autonext' && this.autoloadNext==true && 
			 rightNow.getTime()>(this.nextAutoloadTime.getTime()-1000)) 
			 || (src=='animloop' && this.inAnimationSeq==true)
			 || (src=='usr' && this.inAnimationSeq==false)) {
			this.inAnimationSeq=true; this.autoloadNext=false;
			if (this.curEvent=='') this.curEvent='txtfadeout';
			if (this.firstRun==true) this.curEvent='imgchange';
			if (modInt!=0) {
				this.lstImg=this.curImg;
				this.curImg+=modInt;
				if (this.curImg>this.maxImg) this.curImg=0;
				if (this.curImg<0) this.curImg=this.maxImg; }
			// Increase the time elapsed if one of the timers is set
			incTimeBy=0;
			if (this.imgTimerOn==true) this.timeElapsed+=this.txtIncTimeBy;
			if (this.txtTimerOn==true) this.timeElapsed+=this.imgIncTimeBy;
			// Call the correct function
			switch (this.curEvent) {
				case 'txtfadeout':		/* 1.	*/
					if (this.sliderShowText) {
						if (this.txtTimerOn==false) this.txtTimerOn=true;
						if (this.timeElapsed>this.txtTime) {
							this.txtTimeOn=false; this.timeElapsed=0;
							this.curEvent='imgchange';
						} else {
							this.changeText('fadeout');
							break;
						}
					} else {
						this.txtTimeOn=false; this.timeElapsed=0;
						this.curEvent='imgchange';
						break;
					}
				case 'imgchange':			/* 2.	*/
					if (this.imgTimerOn==false) this.imgTimerOn=true;
					if (this.timeElapsed>this.imgTime) {
						this.imgTimerOn=false; this.timeElapsed=0;
						this.curEvent='txtfadein';	
					} else {
						newImg=false; 
						if (this.timeElapsed==0) newImg=true;
						this.changeImage(newImg);
						if (this.firstRun==true) {
							// Don't set an image fade instance
							// for the firstRun
							this.firstRun=false;
							this.imgTimerOn=false; this.timeElapsed=0;
							this.curEvent='txtfadein';
						}
						break;
					}
				case 'txtfadein':			/* 3.	*/
					if (this.sliderShowText) {
						if (this.txtTimerOn==false) this.txtTimerOn=true;
						if (this.timeElapsed>this.txtTime) {
							this.txtTimerOn=false; this.timeElapsed=0;
							this.curEvent='';
							incTimeBy=-1;
						}
						else {
							this.changeText('fadein');
						}
						break;
					} else {
						this.curEvent='';
						incTimeBy=-1;
						break;
					}
			}
			if (this.imgTimerOn==true) incTimeBy=this.imgIncTimeBy;
			if (this.txtTimerOn==true) incTimeBy=this.txtIncTimeBy;
			var thisShow=this;
			if (incTimeBy!=-1) {
				setTimeout(
				 function(){thisShow.contAnimationSeq()},incTimeBy);
			} else {
				this.autoloadNext=true; this.inAnimationSeq=false;
				this.nextAutoloadTime=null;
				this.nextAutoloadTime=new Date();
				this.nextAutoloadTime.setTime(
				 this.nextAutoloadTime.getTime()+this.nxtImgTime);
				setTimeout(
				 function(){thisShow.contSlideShowSeq()},this.nxtImgTime);
			}
		}
	},

	changeText: function(action) {
		if (this.timeElapsed==0) {
			if (action=='fadein') { startOp=0; startH=0; }
			else { startOp=this.ttlOpacity; startH=this.ttlHeight; }
			// Set the new text
			if (action=='fadein') {
				// Fade outs don't need the text changed yet
				slideshowtxt='<div id="slideshowtxt'+this.uid+'" '+
				 'style="height:'+
				 startH+';"></div>';
				$('slideshowimgs'+this.uid).innerHTML=''+slideshowtxt;
				$('slideshowtxt'+this.uid).innerHTML=''+
				 '<h2>'+this.title[this.curImg]+'</h2><p>'+
				 this.desc[this.curImg]+'</p>';
				if (this.sliderFadeText)
				 this.setOpacity($('slideshowtxt'+this.uid),
				 startOp);
			}
		} else {
			if (action=='fadein') {	
				curOp=(this.timeElapsed/this.txtTime)*this.ttlOpacity;
				curH=(this.timeElapsed/this.txtTime)*this.ttlHeight;
			} else { // Fadeout
				curOp=((this.txtTime-this.timeElapsed)/this.txtTime)*
				 this.ttlOpacity;
				curH=((this.txtTime-this.timeElapsed)/this.txtTime)*
				 this.ttlHeight;
			}
			if (this.sliderFadeText)
			 this.setOpacity($('slideshowtxt'+this.uid),curOp);
			if (this.sliderGrowText)
			 $('slideshowtxt'+this.uid).style.height=curH;
		}
	},

	changeImage: function(newImg) {
		if (newImg==false) {
			// Fade the image in as normal
			curOp=(this.imgTime-this.timeElapsed)/this.imgTime;
			if (curOp==0) { 
			 $('slideshowfore'+this.uid).style.visibility="hidden"; }
			else {
			 this.setOpacity($('slideshowfore'+this.uid),
			 curOp); }
		} else {
			// Initialize the image
			$('slideshowimgs'+this.uid).innerHTML=
			 '<div id="slideshowfore'+this.uid+'"></div>';
			$('slideshowfore'+this.uid).style.backgroundImage=
			 'url("'+this.img[this.lstImg]+'")'; 
			this.delayScript(this.pauseImgTime/2);
			$('slideshowimgs'+this.uid).style.backgroundImage=
			 'url("'+this.img[this.curImg]+'")'; 
			this.delayScript(this.pauseImgTime/2);
		}
	},

	setOpacity: function(obj,opacity) {
		// Internet explorer
		ieopacity=opacity*100;
		obj.style.filter='alpha(opacity='+ieopacity+')';
		// Mozilla-based
		obj.style.MozOpacity=opacity;
		// Everybody else
		obj.style.opacity=opacity;
	},

	delayScript: function(ms) {
		startDate=new Date();	
		curDate=null;
		do { curDate=new Date(); }
		while ((curDate.getTime()-startDate.getTime()) <= ms);
	}

});

