/*
		Usage:
		new SimpleSlideShow(wrapperElement, arrayOfImagesSources [, options]);
		
		Options:
		fadeTime: time in ms for the fade between images
		stayTime: time between two fades
		
		License:
		MIT-Style-License
		Copyright: Jan Kassens <janATkassens.net>
		*/

var SimpleSlideShow = new Class({
			
	Implements: Options,
	
	options: {
		fadeTime: 1000,
		stayTime: 3000
	},
	
	initialize: function(wrapper, images, options) {
		var params = Array.link(arguments, {wrapper: $defined, images: Array.type, options: Object.type});
		if (!params.images.length) return false;
		this.wrapper = document.id(params.wrapper);
		this.wrapper.getChildren().setStyle('opacity', 0);
		this.setOptions(params.options);
		
		this.images = (params.images || JSON.decode(this.wrapper.get('images'))).map(function(image){
			return new Element('img', {
				src: image.get('src') ? image.get('src') : image,
				width: '100%',
				height: '100%',
				tween: {duration: this.options.fadeTime}
			});
		}, this);
		this.index = 0;
		var image = this.images[0];
		this.topImage = image.inject(this.wrapper);
		image.set('spinner', {message: 'one moment…'}).spin();
		image.onload = image.onabort = image.onerror = function(){
			image.unspin();
			this.wrapper.getChildren().setStyle('opacity', 1);
			this.fade.delay(this.options.stayTime, this);
		}.bind(this);
	},
	
	fade: function() {
		this.index = (this.index + 1) % this.images.length;
		this.bottomImage = this.topImage;
		this.topImage = this.images[this.index]
			.fade('hide')
			.inject(this.wrapper)
			.fade('in');
		this.fade.delay(this.options.stayTime + this.options.fadeTime, this);
	}
	
});