// Slideshow by Aeron Glemann (http://electricprism.com/aeron) MIT-style LICENSE

fx.Slideshow = Class.create();
fx.Slideshow.prototype = {
	initialize: function(images, props) {
		this.images = images.split(',');
		if (this.images.length == 1) return; // only cycle multiple images

		this.props = {
			fade: 4000,
			hu: '',
			n: '0',
			overlap: true,
			wait: 1000
		}
		Object.extend(this.props, props || {});

		a = $('aro-' + this.props.n + 'a');
		a.style.position = 'absolute';
		a.style.left = '0px';
		a.style.top = '0px';

		b = a.cloneNode(true);
		b.id = 'aro-' + this.props.n + 'b';
		b.style.zindex = '100';

		div = a.parentNode;
		while (div.tagName != 'DIV') div = div.parentNode;
		div.style.display = 'block';
		div.style.position = 'relative';
		div.style.width = a.width + 'px';
		div.style.height = a.height + 'px';
		div.appendChild(b);

		this.a = $('aro-' + this.props.n + 'a');
		this.b = $('aro-' + this.props.n + 'b');
		
		if (!this.props.overlap) this.a.style.display = 'none';

		this.fadein = new fx.Opacity(this.b.id, { duration: this.props.fade, onComplete: this.preload.bind(this) });
		this.fadeout = new fx.Opacity(this.b.id, { duration: this.props.fade, onComplete: this.loaded.bind(this) });

		this.curr = 1;
		this.timer = (new Date).getTime() + this.props.wait;

		this.loader = new Image();
		this.loader.src = this.props.hu + 'images/' + this.images[this.curr];

		this.preload();
	},

	preload: function() {
		if (this.loader.complete && ((new Date).getTime() > this.timer)) {
			this.a.src = this.loader.src;

			this.fadeout.custom(1, 0);
		}
		else setTimeout(this.preload.bind(this), 10);
	},

	loaded: function() {
		this.b.src = this.loader.src;

		(this.curr == this.images.length - 1) ? this.curr = 0 : this.curr++;
		this.timer = (new Date).getTime() + this.props.wait;

		this.loader = new Image();
		this.loader.src = this.props.hu + 'images/' + this.images[this.curr];

		if (this.props.overlap) {
			this.fadein.setOpacity(1);

			this.preload();
		}
		else this.fadein.custom(0, 1);	
	}
}