// Slideshow v0.4.1 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',
			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';

		img = a.cloneNode(true);
		img.id = 'aro-' + this.props.n + 'b';
		img.style.zindex = '100';
		
		div = a.parentNode;
		div.style.display = 'block';
		div.style.position = 'relative';
		div.style.width = a.width + 'px';
		div.style.height = a.height + 'px';
		div.appendChild(img);

		this.a = $('aro-' + this.props.n + 'a');
		this.b = $('aro-' + this.props.n + 'b');

		this.effect = 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 + '/files/pages/header/' + this.images[this.curr];

		this.preload();
	},

	preload: function() {
		if (this.loader.complete && ((new Date).getTime() > this.timer)) {
			this.a.src = this.loader.src;

			this.effect.custom(1, 0);
		}
		else { setTimeout(this.preload.bind(this), 10); }
	},

	loaded: function() {
		this.b.src = this.loader.src;

		this.effect.setOpacity(1);

		(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 + '/files/pages/header/' + this.images[this.curr];

		this.preload();
	}		
}