var ImagesSlideshow = Class.create();
ImagesSlideshow.prototype = {
	
	//========================================================
	//=== CREATE THE SLIDESHOW
	//========================================================
	initialize: function(
		stage, 		//id of the image to use as the stage
		images,		//hash of the images to use in the slideshow
		caption, 		//where you want the caption to be displayed
		captions		//what the captions are for each image
		) {
		
		//set the properties passed as arguements
		this.stage = $(stage);
		this.image = this.stage.down();
		this.images = $A(images);
		this.caption = $(caption);
		this.captions = $A(captions);
		
		//calculate some more properties
		this.total_images = 0;
		this.current_image = 0;
		this.images.each(function (image, counter) {
			this.total_images++;
		}.bindAsEventListener(this));
		
		//set the current image as the first image
		this.image.src = this.images[0];
		
		//if captions are being used, set the caption
		if (this.caption) {
			this.caption.update(this.captions[0]);
		}
		
	},
	
	//========================================================
	//=== START THE SLIDE SHOW
	//========================================================
	start: function(seconds_delay) {
		
		//save the seconds
		this.seconds_delay = seconds_delay;
		
		//if there is just one image... don't do a slideshow
		if (this.total_images <= 1) {
			return '';
		}
		
		//set the ball rolling
		this.next.bindAsEventListener(this).delay(this.seconds_delay);
		
		//preload the next image ready
		img= new Image(); 
		img.src = this.images[1];
		
	},
	
	//========================================================
	//=== CREATE THE SLIDESHOW
	//========================================================
	next: function () {
		
		//set the cycle so that the current image when it reaches the end, will go back to the beginning
		if (this.current_image+1 < this.total_images) {
			this.current_image++;
			} else {
			this.current_image = 0;
		}
		
		//Fade the image out, and then fade the new one in
		this.image.visualEffect("Fade", { duration:1, 
			afterFinish: function () {
				this.image.src = this.images[this.current_image];
				
				if (this.caption) {
					this.caption.update(this.captions[this.current_image]);
				}
				
				this.image.visualEffect("Appear", { duration:1 }); 
				
				//preload the next image ready
				if (this.current_image+1 < this.total_images) {
					img= new Image(); 
					img.src = this.images[this.current_image+1];
				}
				
			}.bindAsEventListener(this)
		});
		
		//call itself after X seconds
		this.next.bindAsEventListener(this).delay(this.seconds_delay);
		
	}
	
};
