/* vim:ts=4:sts=4:sw=2:noai:noexpandtab
 *
 * JavaScript implementation of an alpha blend filter.
 * Copyright (c) 2005 Steven McCoy <fnjordy@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

function slideshow (containerid,slideid, slides) {
	this.slide = document.getElementById(slideid);
	this.slide.container = document.getElementById(containerid);
	this.slides = slides;

/* delay after pre-loading next image to fading in */
	this.wait_delay = 6000;

/* delay between fade updates */
	this.fade_delay = 120;

/* change in opacity per update */
	this.fade_increment = 8;

	this.next_slide(0);
}

slideshow.prototype.set_opacity = function (element, opacity) {
/* turn off some funky flickering */
	if (navigator.userAgent.indexOf("Firefox") != -1)
		if (opacity >= 100) opacity = 99.999;
	element.style.filter = "alpha(opacity:" + opacity + ")";
	element.style.KhtmlOpacity = opacity / 100;
	element.style.MozOpacity = opacity / 100;
	element.style.opacity = opacity / 100;
}

slideshow.prototype.next_slide = function (i) {
// set this slide to background
	this.slide.container.style.backgroundImage = 'url('+this.slides[i]+')';
	if (++i >= this.slides.length) i = 0;

// start loading next image
	var slideshow = this;
	this.slide.onLoad = slideshow.wait_slide(i);
	this.slide.src = this.slides[i];
}

slideshow.prototype.wait_slide = function (i) {
// set new image 100% transparent above displayed image
	this.set_opacity(this.slide, 0);
// image has loaded, now wait a bit
	var slideshow = this;
	setTimeout(function() { slideshow.fade_slide(i,0); }, this.wait_delay)
}

slideshow.prototype.calculate_opacity = function (opacity) {
	return opacity + this.fade_increment;
}

slideshow.prototype.fade_slide = function (i, opacity) {
	opacity = this.calculate_opacity(opacity);
	if (opacity <= 100) {
		var slideshow = this;
		setTimeout(function() { slideshow.fade_slide(i,opacity); }, this.fade_delay);
	}
	if (opacity > 100) {
		this.next_slide(i);
	} else {
		this.set_opacity(this.slide,opacity);
	}
}
