/**
 *
 *	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 : 4000;
		this.start 			= oArgs.start ? oArgs.start : 0;
		this.duration		= oArgs.duration ? oArgs.duration : 1.0;
		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.hasControls	= oArgs.hasControls ? oArgs.hasControls : false;
		this.hasNavigation	= oArgs.hasNavigation ? oArgs.hasNavigation : false;
		this.iImageId		= this.start;
		this.id				= oArgs.id;
		this.timer			= null;
		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, queue: { position: 'end', scope: this.id+'_appear' } });
		$(this.slides[y]) && $(this.slides[y]).fade({duration: this.duration, queue: { position: 'end', scope: this.id+'_fade' } });
	},
	
	// the onload event handler that starts the fading.
	startSlideShow: function () {
		this.resetTimer();
		
		if (this.hasControls) {
			$(this.playButton).hide();
			$(this.pauseButton).appear({ duration: 0});
		}

		this.updateCounter();
									
	},
	
	resetTimer: function() {
		clearInterval(this.timer);
		this.timer = setInterval(this.play.bind(this),this.wait);
	},
	
	play: function () {
		
		var imageShow, imageHide;
	
		imageShow = this.iImageId+1;
		imageHide = this.iImageId;
		
		if (imageShow == this.numOfImages) {
			imageShow = 0;
			this.iImageId = 0;					
		} else {		
			this.iImageId++;
		}
		this.swapImage(imageShow,imageHide);
		
		if (this.hasNavigation) {
			$(this.id+"_img_navi_but_"+(imageShow+1)).toggleClassName("on");
			$(this.id+"_img_navi_but_"+(imageHide+1)).toggleClassName("on");
		}
		
		this.textIn = this.iImageId+1 + ' of ' + this.numOfImages;
		this.updateCounter();
	},
	
	stop: function  () {
		clearInterval(this.timer);
		
		if (this.hasControls) {
			$(this.playButton).appear({ duration: 0});
			$(this.pauseButton).hide();
		}
	},
	
	goNext: function (andStop) {
		
		if (andStop) {
			this.stop();
		}
		else {
			this.resetTimer();
		}
		
		var imageShow, imageHide;
	
		imageShow = this.iImageId+1;
		imageHide = this.iImageId;
		
		if (imageShow == this.numOfImages) {
			imageShow = 0;	
			this.iImageId = 0;					
		} else {		
			this.iImageId++;
		}
		this.swapImage(imageShow,imageHide);	
		
		if (this.hasNavigation) {
			$(this.id+"_img_navi_but_"+(imageShow+1)).toggleClassName("on");
			$(this.id+"_img_navi_but_"+(imageHide+1)).toggleClassName("on");
		}
	
		this.updateCounter();
	},
	
	goPrevious: function (andStop) {
	
		if (andStop) {
			this.stop();
		}
		else {
			this.resetTimer();
		}
	
		var imageShow, imageHide;
					
		imageShow = this.iImageId-1;
		imageHide = this.iImageId;
		
		if (this.iImageId == 0) {
			imageShow = this.numOfImages-1;
			this.iImageId = this.numOfImages-1;
		} else {			
			this.iImageId--;
		}
		this.swapImage(imageShow,imageHide);
		
		if (this.hasNavigation) {
			$(this.id+"_img_navi_but_"+(imageShow+1)).toggleClassName("on");
			$(this.id+"_img_navi_but_"+(imageHide+1)).toggleClassName("on");
		}
		
		this.updateCounter();
	},
	
	goImage: function(id,andStop) {
	
		if (id == this.iImageId) { return; }
	
		if (andStop) {
			this.stop();
		}
		else {
			this.resetTimer();
		}
		
		var imageShow, imageHide;
	
		imageShow = id;
		imageHide = this.iImageId;
		
		this.swapImage(imageShow,imageHide);			
		this.iImageId = imageShow;
		
		if (this.hasNavigation) {
			$(this.id+"_img_navi_but_"+(imageShow+1)).toggleClassName("on");
			$(this.id+"_img_navi_but_"+(imageHide+1)).toggleClassName("on");
		}
	
		this.updateCounter();
	},
	
	updateCounter: function () {
		var textIn = this.iImageId+1 + ' of ' + this.numOfImages;
		$(this.counter) && ( $(this.counter).innerHTML = textIn );
		if ($(this.caption)) {
			
			var tmp_cap = $(this.slides[this.iImageId]).down('img').title;
			if (tmp_cap.length < 1 || tmp_cap == 'undefined') { tmp_cap = "Image "+this.iImageId; }
			
			// checking for copyright data
			var copy_pattern = /(.*) (©|&copy;) (.*)$/;
			if ($(this.slides[this.iImageId]).down('img').longDesc.length && $(this.slides[this.iImageId]).down('img').longDesc != window.location.href && tmp_cap.search(copy_pattern) !== -1) {
				//alert($(this.slides[this.iImageId]).down('img').longDesc);
				tmp_cap = tmp_cap.replace(copy_pattern,'$1 $2 <a href="'+$(this.slides[this.iImageId]).down('img').longDesc+'" target="_blank">$3</a>');
			}
			
			$(this.caption).innerHTML = tmp_cap;
		}
	}
}
