
function Slide (file, caption, link) {
	this.file    = file;
	this.caption = caption;
	this.link    = link;
	this.loaded  = false;

	if (document.images) {
		this.image = new Image();
	}
	
	this.load = function () {
		if (!document.images) { 
			return; 
		}

		if (!this.loaded) {
			this.image.src = this.file;
			this.loaded    = true;
		}
	}
	
	this.getLink = function () {
		document.location = this.link;
	}
	
}

function SlideShow (myname, el) {
	this.whoami    = myname;
	this.elementid = el;
	this.slides    = new Array();
	this.repeat    = true;
	this.duration  = 4000;
	
	this.cur       = 0;
	this.timeoutid = 0;
	
	this.Add = function (file, caption, link) {
		var idx = this.slides.length;
		this.slides[idx] = new Slide(file, caption, link);
		this.slides[idx].load();
	}
	
	this.Advance = function () {
		if (this.cur < (this.slides.length - 1) ) {
			// increment
			this.cur++; 
		}
		else {
			//start over
			this.cur = 0;
		}
		
		if (this.timeoutid == 0) {
			// Show not "playing"
			this.setSlide(this.cur);
		}
		else {
			this.Play();
		}
	}
	
	this.Back = function () {
		this.Pause();
		
		if (this.cur > 0 ) {
			this.cur--;
		}
		else {
			this.cur = this.slides.length - 1;
		}
		
		this.setSlide(this.cur);
	}
	
	this.Play = function () {
		this.setSlide(this.cur);
		
		if (this.timeoutid != 0) {
			window.clearTimeout(this.timeoutid);
		}
		this.timeoutid = window.setTimeout(
			this.whoami + '.Advance()', 
			this.duration
		);
	}
	
	this.Pause = function () {
		window.clearTimeout(this.timeoutid);
		this.timeoutid = 0;
	}
	
	this.togglePlay = function () {
		var aout = 0;
		
		if (this.timeoutid == 0) {
			// not playing, start it
			this.Play();
			aout = 1;
		}
		else {
			// playing, stop it
			this.Play();
		}
		
		return aout;
	}
	
	this.goTo = function (idx) {
		this.Pause();
		this.setSlide(idx);
	}
	
	this.setSlide = function (idx) {
		this.cur = idx;
		el = document.getElementById(this.elementid);
		el.style.backgroundImage = 'url(' + 
			this.slides[idx].file + ')';
		
	}
	
	this.getLink = function () {
		this.slides[this.cur].getLink();
	}
	
}

