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.randomstart    = (typeof(oArgs.randomstart)=='undefined') ? true : oArgs.randomstart;
		this.duration		= oArgs.duration ? oArgs.duration : 0.5;
		this.autostart		= (typeof(oArgs.autostart)=='undefined') ? true : oArgs.autostart;
		this.slides 		= oArgs.slides;
		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[y]) && $(this.slides[y]).fade({duration: this.duration });
		setTimeout(function () {
			$(this.slides[x]) && $(this.slides[x]).appear({ duration: this.duration });
		}.bind(this), this.duration);
	},
	
	// the onload event handler that starts the fading.
	startSlideShow: function () {
        if(this.randomstart) {
            this.iImageId = Math.floor(Math.random()*this.slides.length);
        }
        $(this.slides[this.iImageId]).appear({duration:this.duration/2});
		setInterval(this.play.bind(this), this.wait);
	},
	
	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++;
		}
	}
}
