// Copyright 2002 Factor of 4, LLC <http://www.factorof4.com/>
// All Rights Reserved.
// Requires JavaScript 1.2 or later.


function SlideShow (program, prefetch) {
	this.pix = new Array();
	this.pixDetails = new Array();
	for ( var i = 0; i < program.length; i++ ) {
		var it = program[i];
		var itsDetails = void 0;
		if ( typeof(it) == 'object' ) {
			itsDetails = it.slice(1);
			if ( itsDetails.length == 0 ) {
				itsDetails = void 0
			}
			it = it[0];
		}
		this.pix[i] = it;
		this.pixDetails[i] = itsDetails;
	}
	this.currentPic = 0;
	if ( prefetch ) {
		this.nextImage = new Image();
	}
}


SlideShow.prototype.initPic = 
	function (imageElement) {
		this.imageElement = imageElement;
		this.imageElement.src = this.url(0);
		if ( this.nextImage ) {
			this.nextImage.src = this.url(this.index(1));
		}
	};

SlideShow.prototype.url = 
	function (index) {
		return this.pix[index];
	};
	
SlideShow.prototype.index =
	function (delta) {
		if ( typeof delta == 'undefined' ) {
			delta = 0
		}
		return (this.currentPic + delta + this.pix.length) % this.pix.length;
	};
	
SlideShow.prototype.details =
	function () {
		return this.pixDetails[this.currentPic];
	};
	
SlideShow.prototype.changeSlide =
	function (delta) {
		this.currentPic = 
			(this.currentPic + delta + this.pix.length) % this.pix.length;
		this.imageElement.src = this.url(this.currentPic);
		
		// preload next or previous, depending on direction
		if ( this.nextImage ) {
			this.nextImage.src = this.url(this.index(delta));
		}
	};


