

var slideshow = Class.create( {
//	screen is the DOM element that will hold the screenshow images. Must contain a single IMG element
	initialize: function(container, tag, imgs) {
		this.imgs = $A(imgs);
		this.container = $(container);
		this.tag =$(tag);		

		this.loop = 1;
		this.number_of_images = this.imgs.length;
		if(this.number_of_images==0) return false;	
		this.current_image = 0;
		this.previous_image = null;
		this.current_loop = 0;
		this.hideImages();
		this.imgloadtimeout = this.loadImages.bind(this).delay(2);
 	},

 	hideImages: function() {
		this.tag.hide(); // hide the image, but not the parent div
		this.container.toggleClassName('loading');
	},
	
	loadImages: function() {
		var obj = this;
		this.imgs.each(function(s, ix) {
			s.preload = new Image(); // create an IMG object 
			s.preload.onload = function() {obj.isLoaded(ix);}; // set the onload event handler
			s.preload.src = s; // set the img src, causes image to load from server
		});
	},

	isLoaded : function(ix) {
		this.imgs[ix].loaded = (this.imgs[ix].preload.width!=0) ? true : null;
		if(ix==0) { this.showImage(); }
	},

	showImage : function() {
		if(!this.imgs[this.current_image].loaded)
			{// if this image not loaded
			if(this.current_image!=this.number_of_images-1)
				{ this.current_image++; }
			else {
				this.current_image=0;
				this.current_loop++;
			}
		}
		this.tag.src = this.imgs[this.current_image].preload.src;
		new Effect.Appear(this.tag.id, {duration: 1.0}); // got to 1
		this.imgtimeout = this.fadeImage.bind(this).delay(5); // go to 5
		this.previous_image=this.current_image;
		if (this.current_loop==this.loop || this.number_of_images==1) 
			{ this.pause();	}
		if (this.current_image!=this.number_of_images-1) 
			{ this.current_image++; }
		else
			{ this.current_image=0; this.current_loop++; }
	},	
 
 	pause : function() {
		window.clearTimeout(this.imgtimeout);
		window.clearTimeout(this.timein);
	},
 
	fadeImage : function() {
		new Effect.Opacity(this.tag.id, {from: 1.0, to: 0, duration: 1.00});
		this.timein = this.showImage.bind(this).delay(1); // go to 1
	}

});

window.onunload = function() { 
//	clearTimeout(this.linktimein);
//	this.slideshow.pause();
};


