/**
 *	Basic Slideshow App
 *	
 *	Plagerised from Tom Doyle by Isotope Communications Ltd, Dec 2008
 *	http://www.tomdoyletalk.com/2008/10/28/simple-image-gallery-slideshow-with-scriptaculous-and-prototype/
 *	
 *	Published [16/12/08]:
 *	http://www.icommunicate.co.uk/articles/all/simple_slide_show_for_prototype_scriptaculous_38/	
 *
 *	Changes: Basically made it an object so that you can run multiple instances and so that
 *			 it doesn't get interfered with by other scripts on the page.
 *	
 *			 Have also added a few things, like "Captions" and the option of changing the
 *			 effects..
 *
 *	Example:
 *			Event.observe(window, 'load', function(){
 *				oMySlides = new iSlideShow({
 *					autostart 	: true		// optional, boolean (default:true)
 *					start		: 0,	 	// optional, slides[start] (default:0) 
 *					wait 		: 4000, 	// optional, milliseconds (default:4s)
 *					slides 		: [
 *						'image-div-a', 
 *						'image-div-b', 
 *						'image-div-c', 
 *						'image-div-d' 
 *					],
 *					counter		: 'counter-div-id', // optional...
 *					caption 	: 'caption-div-id', // optional... 
 *					playButton	: 'PlayButton', 	// optional (default:playButton)
 *					pauseButton	: 'PauseButton', 	// optional (default:PauseButton)
 *				});
 *				oMySlides.startSlideShow();
 *			});
 *
 *			To start the slideshow:
 *				oMySlides.startSlideShow();
 *
 *			To skip forward, back, stop:
 *				oMySlides.goNext();
 *				oMySlides.goPrevious();
 *				oMySlides.stop();
 */

var iSlideShow = new Class.create();

iSlideShow.prototype = {
	
	initialize : function (oArgs){
		this.wait 			= oArgs.wait ? oArgs.wait : 5000;
		this.restartTime	= oArgs.restartTime ? oArgs.restartTime : 10000;
		this.start 			= oArgs.start ? oArgs.start : 0;
		this.duration		= oArgs.duration ? oArgs.duration : 0.5;
		this.autostart		= (typeof oArgs.autostart == 'undefined' ) ? true : oArgs.autostart;
		this.slides 		= oArgs.slides;
		this.counter		= oArgs.counter;
		this.caption		= oArgs.caption;
		this.playButton		= oArgs.playButton ? oArgs.playButton : 'PlayButton';
		this.pauseButton	= oArgs.pauseButton ? oArgs.pauseButton : 'PauseButton';
		this.iImageId		= this.start;
		
		/**
		 *	Add Previous/Next buttons
		 */
		if ( $(oArgs.nextButton) ){
			$(oArgs.nextButton).observe('click',function(e){
				Event.stop(e);
				this.goNext();
			}.bind(this));
		}
		
		if ( $(oArgs.prevButton) ){
			$(oArgs.prevButton).observe('click',function(e){
				Event.stop(e);
				this.goPrevious();
			}.bind(this));
		}
		
		
		
		
		// To prevent swaps running into each other... 
		this._unlocked		= true;
		
		if ( this.slides ) {
			this.numOfImages	= this.slides.length;
			if ( !this.numOfImages ) {
				// alert('No slides?');
			}
		}
		if ( this.autostart && (this.numOfImages > 1) ) {
			this.startSlideShow();
		}
	},
	
	// The Fade Function
	swapImage: function (x,y,oArgs) {
		// console.log("Swapping " + y + " for " + x);
		// Lock the SS against further changes..
		this.lock();
		// and swap the images.. 
		duration = (oArgs && oArgs.duration) ? oArgs.duration : this.duration
		$(this.slides[x]) && $(this.slides[x]).appear({
			duration		: duration,
			afterFinish		: this.unlock.bind(this)
		});
		$(this.slides[y]) && $(this.slides[y]).fade({duration:duration});
	},
	
	unlock: function(){
		this._unlocked = true;
		// console.log('Unlocked');
	},
	
	lock: function(){
		this._unlocked = false;
		// console.log('Locked');
	},
	
	// the onload event handler that starts the fading.
	startSlideShow: function () {
		this.playId = setInterval(this.play.bind(this),this.wait);
		$(this.playButton) && $(this.playButton).hide();
		$(this.pauseButton) && $(this.pauseButton).appear({ duration: 0});

		this.updatecounter();						
	},
	
	setRestart: function () {
		this.playId = setTimeout(this.startSlideShow.bind(this),this.restartTime);
	},
	
	play: function () {
		if ( this._unlocked ) {
			var imageShow, imageHide;
		
			imageShow = this.iImageId+1;
			imageHide = this.iImageId;
			
			if (imageShow == this.numOfImages) {
				this.swapImage(0,imageHide);	
				this.iImageId = 0;					
			} else {
				this.swapImage(imageShow,imageHide);			
				this.iImageId++;
			}
			
			this.textIn = this.iImageId+1 + ' of ' + this.numOfImages;
			this.updatecounter();
		}
	},
	
	stop: function  () {
		clearInterval(this.playId);				
		$(this.playButton) && $(this.playButton).appear({ duration: 0});
		$(this.pauseButton) && $(this.pauseButton).hide();
	},
	
	goNext: function (oArgs) {
		if ( this._unlocked ) {
			clearInterval(this.playId);
			$(this.playButton) && $(this.playButton).appear({ duration: 0});
			$(this.pauseButton) && $(this.pauseButton).hide();
			
			var imageShow, imageHide;
		
			imageShow = this.iImageId+1;
			imageHide = this.iImageId;
			
			if (imageShow == this.numOfImages) {
				this.swapImage(0,imageHide,oArgs);	
				this.iImageId = 0;					
			} else {
				this.swapImage(imageShow,imageHide,oArgs);			
				this.iImageId++;
			}
		
			this.updatecounter();
			this.setRestart();
			return false;
		}
	},
	
	goPrevious: function (oArgs) {
		if ( this._unlocked ) {
			clearInterval(this.playId);
			$(this.playButton) && $(this.playButton).appear({ duration: 0});
			$(this.pauseButton) && $(this.pauseButton).hide();
		
			var imageShow, imageHide;
						
			imageShow = this.iImageId-1;
			imageHide = this.iImageId;
			
			if (this.iImageId == 0) {
				this.swapImage(this.numOfImages-1,imageHide,oArgs);	
				this.iImageId = this.numOfImages-1;		
			} else {
				this.swapImage(imageShow,imageHide,oArgs);			
				this.iImageId--;
			}
			
			this.updatecounter();
			this.setRestart();
			return false;
		}
	},
	
	goTo: function( id ){
		if ( this._unlocked ) {
			clearInterval(this.playId);
			$(this.playButton) && $(this.playButton).appear({ duration: 0});
			$(this.pauseButton) && $(this.pauseButton).hide();
		
			var imageShow, imageHide;
					
			imageShow = id;
			imageHide = this.iImageId;
			if ( imageShow != this.iImageId ) {
				this.swapImage(imageShow,imageHide);	
				this.iImageId = imageShow;
			}
			this.updatecounter();
			this.setRestart();
			return false;
		}
	},
	
	updatecounter: function () {
		var textIn = this.iImageId+1 + ' of ' + this.numOfImages;
		$(this.counter) && ( $(this.counter).innerHTML = textIn );
		if ( $(this.caption) && ( oNewCaption = $(this.slides[this.iImageId]).down('.image-caption') ) ) {
			$(this.caption).innerHTML = oNewCaption.innerHTML;
		} else if ( $(this.slides[this.iImageId]) && ($(this.slides[this.iImageId]).down('img') && $(this.slides[this.iImageId]).down('img').title) ){
			if ( oNewCaption = $(this.slides[this.iImageId]).up('div').down('.caption') ) { // <<- Crock?
				$(this.slides[this.iImageId]).up('div').down('.caption').innerHTML = $(this.slides[this.iImageId]).down('img').title;
			} else {
				if ( sTitle = $(this.slides[this.iImageId]).down('img').title ) {
					if ( $(this.caption) ) {
						$(this.caption).innerHTML = $(this.slides[this.iImageId]).down('img').title;
					}
				}
			}
		}
	}
}


