
SlideShow = new Class({
	Implements: [Options],
	options: {
		delay: 5000,
		duration: 1000
	},
	initialize: function(container, slides, options) {
		this.container = $(container);
		if(!this.container) return;

		this.container.addEvents({
			mouseover: function() {

			},
			mouseout: function() {

			},
			click: function() {
				if(this.getAttribute('clickto')) document.forward( this.getAttribute('clickto') );
			}
		});

		this.setOptions(options);

		this.slides = [];
		var tmp = $splat(slides);

		for(var i=0; i<tmp.length; ++i) {
			if(typeof(tmp[i]) != 'object') tmp[i] = { src: tmp[i] };
			this.slides.push( tmp[i] );
		}
		if(!this.slides.length) return;

		this.timer = null;
		this.curIndex = 0;

		this.startTimer();
	},

	startTimer: function() {
		clearTimeout(this.timer);

		var n = (this.slides[this.curIndex+1]) ? this.curIndex+1 : 0;
		var i = new Image(); i.src = this.slides[n].src;

		this.timer = setTimeout(function() {
			this.slideNext();
		}.bind(this), this.options.delay);

	},

	slideNext: function() {
		clearTimeout(this.timer);

		++this.curIndex;
		if(this.curIndex >= this.slides.length) this.curIndex = 0;

		var tmp = this.container.clone();
		tmp.setStyles({
			position: 'absolute',
			left: this.container.getLeft(),
			top: this.container.getTop(),
			'z-index': 20
		});
		tmp.inject($(document.body));

		var newslide = this.slides[this.curIndex];
		this.container.src = newslide.src;
		new Fx.Tween(tmp, {
			duration: this.options.duration,
			onComplete: function() {
				this.startTimer();
				tmp.destroy();
			}.bind(this)
		}).start('opacity', 0);

		if(newslide.href) {
			this.container.setStyle('cursor', 'pointer');
			this.container.setAttribute('clickto', newslide.href);
		}
		else {
			this.container.setStyle('cursor', 'default');
			this.container.removeAttribute('clickto');
		}
	}

});












