/* $Id: SlideShow.js 410 2009-07-19 20:44:20Z felix $ */

function SlideShow(id, interval, fx) {
	this.id = id;
	this.pos = 0;
	var i, imgs = document.getElementById(id).getElementsByTagName('img');
	this.imgs = [];
	for (i = 0; i < imgs.length; i++) {
		if (imgs[i].className.match(/\bslideShowPic\b/)) {
			this.imgs.push(imgs[i]);
		}
	}
	this.interval = interval;
	this.caption = null;
	var spans = document.getElementById(id).getElementsByTagName('span');
	for (i = 0; i < spans.length; i++) {
		if (spans[i].className.match(/\bcaption\b/)) {
			this.caption = spans[i].lastChild;
		}
	}
	this.first = {src: this.imgs[0].src, alt: this.imgs[0].alt};
	this.fx = (typeof fx == 'object') ? {
		th : new fx.fx(this.imgs[0]),
		hide : fx.hide, 
		show : fx.show, 
		params : fx.params,
		dur : (typeof fx.dur != 'undefined') ? fx.dur : 0
	} : {
		th : new Fader(this.imgs[0]),
		hide : Fader.prototype.fade,
		show : Fader.prototype.appear,
		params : {},
		dur : SlideShow.calcDuration
	};
	this.set();
}

SlideShow.prototype.set = function(d) {
	if (!this.timer) {
		var t = this;
		this.timer = setInterval(function() {t.change();}, this.interval);
//		this.timer = setInterval(new Function("slideShows['" + this.id + "'].change.call(slideShows['" + this.id + "']);"), this.interval);
		this.vel = (typeof this.fx.dur == 'function') ? this.fx.dur.call(this.fx.th, this.interval) : this.fx.dur;
//		this.vel = (this.fx.dur == 0) ? 0 : (this.interval > 9000) ? 1500 : this.interval / 6.0;
	}
};

SlideShow.prototype.unset = function(d) {
	clearInterval(this.timer);
	this.timer = null;
};

SlideShow.prototype.stop = function(d) {
	this.unset();
	d.className = d.className.replace(/\brunning\b/, "stopped");
};

SlideShow.prototype.start = function(d) {
	this.set();
	this.change();
	d.className = d.className.replace(/\bstopped\b/, "running");
};

SlideShow.prototype.prev = function() {
	if (this.timer) {
		this.unset();
		this.set();
	}
	this.pos = (this.pos + this.imgs.length - 2) % this.imgs.length;
	this.change();
};

SlideShow.prototype.next = function() {
	if (this.timer) {
		this.unset();
		this.set();
	}
	this.change();
};

SlideShow.prototype.change = function() {
	this.pos = (this.pos + 1) % this.imgs.length;
	this.inc = -0.09;
	// closure messing...
	var t = this;
	var image = t.imgs[0];
	t.fx.hide.call(t.fx.th, this.vel, this.fx.params);
	
	function _ch() {
		image.src = t.pos ? t.imgs[t.pos].src : t.first.src;
		image.title = image.alt = t.pos ? t.imgs[t.pos].alt : t.first.alt;
		if (t.caption) {
			t.caption.innerHTML = image.title;
		}
		t.fx.show.call(t.fx.th, t.vel, t.fx.params);
	}
	setTimeout(_ch, t.vel, t.fx.params);
};

SlideShow.calcDuration = function(interval) {
	return (interval > 9000) ? 1500 : interval / 6.0;
};


function Flipper(img) {}

Flipper.hide = Flipper.show = function(v) {};
