// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

 
var SlideShow = new Class.create();

SlideShow.prototype = {
	
	initialize : function (getArgs){
		this.wait 			= getArgs.wait ? getArgs.wait : 4000;
		this.start 			= getArgs.start ? getArgs.start : 0;
		this.duration		= getArgs.duration ? getArgs.duration : 0.5;
		this.autostart		= (typeof(getArgs.autostart)=='undefined') ? true : getArgs.autostart;
		this.slides 		= getArgs.slides;
		this.counter		= getArgs.counter;
		this.caption		= getArgs.caption;
		this.playButton		= getArgs.playButton ? getArgs.playButton : 'PlayButton';
		this.pauseButton	= getArgs.pauseButton ? getArgs.pauseButton : 'PauseButton';
		this.iImageId		= this.start;
		if ( this.slides ) {
			this.numOfImages	= this.slides.length;
			if ( !this.numOfImages ) {
				alert('No slides?');
			}
		}
		if ( this.autostart ) {
			this.startSlideShow();
		}
	},
	
	// 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.play = setInterval(this.play.bind(this),this.wait);
		$(this.playButton).hide();
		$(this.pauseButton).appear({ duration: 0});

		this.updatecounter();
									
	},
	
	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.play);				
		$(this.playButton).appear({ duration: 0});
		$(this.pauseButton).hide();
	},
	
	goNext: function () {
		clearInterval(this.play);
		$(this.playButton).appear({ duration: 0});
		$(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();
	},
	
	goPrevious: function () {
		clearInterval(this.play);
		$(this.playButton).appear({ duration: 0});
		$(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;		
			
			//alert(NumOfImages-1 + ' and ' + imageHide + ' i=' + i)
						
		} else {
			this.swapImage(imageShow,imageHide);			
			this.iImageId--;
			
			//alert(imageShow + ' and ' + imageHide)
		}
		
		this.updatecounter();
	},
	
	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;
		}
	}
}


Event.observe(window, 'load', function(){
  WMLSlides = new SlideShow({
     autostart 	: true,		
     start		: 0,	 
     wait 		: 5000, 
     slides 		: [
       'slide1', 
       'slide2', 
       'slide3' 
     ],
//     counter		: 'counter-div-id', 
//     caption 	: 'caption-div-id', 
//     playButton	: 'PlayButton', 	
//     pauseButton	: 'PauseButton', 
   });
   WMLSlides.startSlideShow();
 });

// window.onload = WMLSlides.startSlideShow;






