/**
 * slideshow.js
 *
 * Reusable JavaScript code for slideshows, originally developed
 * for DynaFlair.
 */

DF = window.DF || {}

if (DF.slideshow == undefined) {
	DF.slideshow = {};
}

DF.slideshow.Image = function(src, alt) {
	this.src = src;
	this.alt = alt;
}


DF.slideshow.Slideshow = function(id) {
	this.id = id;
	this.imageElement = document.getElementById(id);	
	this.images = new Array();
	this.position = 0;
	this.waitInterval = 3;
}

DF.slideshow.Slideshow.prototype.addImage = function(src, alt) {
	this.images.push(new DF.slideshow.Image(src, alt));
}

DF.slideshow.Slideshow.prototype.nextImage = function() {
	if (this.images.length == 0) {
		return; // Nothing to do
	}
	
	this.position++;
	if (this.position >= this.images.length) {
		this.position = 0;
	}
	
	this._applyCurrent();	
}

DF.slideshow.Slideshow.prototype._applyCurrent = function() {
	var image = this.images[this.position];
	this.imageElement.src = image.src;
	this.imageElement.alt = image.alt;
}

DF.slideshow.Slideshow.prototype.showFirst = function() {
	this.position = 0;
	this._applyCurrent();
}
DF.slideshow.Slideshow.prototype.start = function() {
	this.showFirst();
	this._initTimeBomb();
}

DF.slideshow.Slideshow.prototype._initTimeBomb = function() {
	var handler = function() {		
		this.slideshow.nextImage();
		this.slideshow._initTimeBomb();
	}
	handler.slideshow = this;
	
	window.setTimeout(handler, this.waitInterval * 1000);
}