

var Slideshow = Class.create({
	container: null,
	current: null,
	slideInterval: 5,
	slideDuration: 0.7,
	
	initialize: function(container) {
		this.container = $(container);
		if (!Object.isElement(this.container)) {
			throw new Error("Element passed to Slideshow constructor must be an element or an id of an element!");
		}
		this.images = container.select('img');
		
		if (this.images.length > 1) {
			this.current = 0;
			this.start();
		}
	},
	
	start: function() {
		setTimeout(this.slide.bind(this), this.slideInterval*1000);
	},
	
	slide: function() {
		new Effect.Fade($(this.images[this.current]), {
			duration: this.slideDuration,
			afterFinish: function() {
				this.current += 1, elmt = this.images[this.current];
				if (typeof(elmt) == 'undefined') {
					this.current = 0;
				}
				
				new Effect.Appear($(this.images[this.current]), {
					duration: this.slideDuration,
					afterFinish: this.start.bind(this)
				});
			}.bindAsEventListener(this)
		});
	}
});

var SlideshowInstance;
$(document).observe('dom:loaded', function() {
	$$('.slideshow').each(function(element) {
		new Slideshow(element);
	});
});

