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;
		if ( this.slides ) {
			this.numOfImages	= this.slides.length;
			if ( !this.numOfImages ) {
				alert('No slides?');
			}
		}
		if ( this.autostart ) {
			this.stop();
		}
	},
	
	// The Fade Function
	swapImage: function (x,y) {		
		$(this.slides[x]) && $(this.slides[x]).appear({ duration: this.duration });
		$(this.slides[y]) && $(this.slides[y]).fade({duration: this.duration });
	},
	
	// 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 () {
		
		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 () {
		/*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);	
			this.iImageId = 0;
							
		} else {
			this.swapImage(imageShow,imageHide);			
			this.iImageId++;
		}
	
		this.updatecounter();
		/*this.setRestart();*/
		
	},
	
	goPrevious: function () {
		/*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);	
			this.iImageId = this.numOfImages-1;		
				
		} else {
			this.swapImage(imageShow,imageHide);			
			this.iImageId--;
		}
		
		this.updatecounter();
		/*this.setRestart();*/
		
	},
	
	goTo: function( id ){
		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();
	
	},
	
	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').title ){
			if ( oNewCaption = $(this.slides[this.iImageId]).up('div').down('.caption') ) {
				$(this.slides[this.iImageId]).up('div').down('.caption').innerHTML = $(this.slides[this.iImageId]).down('img').title;
			}
		}
	}
}
