
var globalBrowserID = /MSIE/.test(navigator.userAgent) ? 'IE' : 'Mozilla';


function setHeight()
{
	var elements = new Array();
	var maxHeight = 0;
	for (var index = 0; index < arguments.length; ++index)
	{
		var ID = arguments[index];
		var elem = document.getElementById(ID);
		elements.push(elem);
		var height = xHeight(elem);
		maxHeight = height > maxHeight ? height : maxHeight;
	}
	
	for (var index = 0; index < elements.length; ++index)
	{
		xHeight(elements[index], maxHeight);
	}
}

// function setHeight(ID1, ID2)
// {
// 	var one = document.getElementById(ID1);
// 	var two = document.getElementById(ID2);
// 	var max = one.scrollHeight > two.scrollHeight ? one.scrollHeight : two.scrollHeight;
// 	one.style.height = max;
// 	two.style.height = max;
// }
var timerArray = new Array();

///////////////////////////////////////////////////////////////////////////////////////////
//
// TODO:
//
// Add preloader.
//
// Add callback function to execute on run completion.
// Change system to a separate superclass "sequencer" and allow multiple img ID's
// Change CBFunc so that getImageIndex and getImagePrevIndex work correctly and no cleanup.
//
///////////////////////////////////////////////////////////////////////////////////////////

function slideShow (imgID) {
	this.setImgID(imgID);

	this.imageData = new Array();

	this.run = 0;
	this.runMax = 0;
	this.timeoutID = null;
	this.prevPicIndex = null;
	this.nextPicIndex = 0;
	this.interval = 10000;
	this.memoizePics = new Array();
	this.timerArrayIndex = timerArray.length;
	timerArray[this.timerArrayIndex] = this;
}

slideShow.prototype.setRunMax = function (runs) {
	this.runMax = runs;
}

slideShow.prototype.setInterval = function (interval) {
	this.interval = interval;
}

slideShow.prototype.setImgID = function (imgID) {
	this.imgID = imgID;
	this.imgElement = document.getElementById(this.imgID);
}

slideShow.prototype.loadImageData = function () {
	this.imageData = arguments;
}

slideShow.prototype.getImageDataIndex = function () {
	return this.prevPicIndex;
}

slideShow.prototype.getImageDataByIndex = function (picIndex) {
	return picIndex < this.imageData.length ? this.imageData[picIndex] : null;
}

slideShow.prototype.displayImage = function (picIndex) {
	if (this.memoizePics[picIndex] == null) {
		this.memoizePics[picIndex] = new Image();
		this.memoizePics[picIndex].src = this.imageData[picIndex].URL;
	}

	this.imgElement.src = this.memoizePics[picIndex].src;
}

slideShow.prototype.processImage = function (picIndex) {
	if(this.runMax != 0 && picIndex == 0 && ++this.run > this.runMax) {
//		history.back();
		return 0;
	}
	if(this.imageData[picIndex].CBFunc) {
            if(this.prevPicIndex != null) { // Clear out the old stuff
			this.imageData[this.prevPicIndex].CBFunc(1, this.imageData[this.prevPicIndex], this.run, this.prevPicIndex);
		}
        this.displayImage (picIndex);
		this.imageData[picIndex].CBFunc(0, this.imageData[picIndex], this.run, picIndex);
	} else {
	this.displayImage (picIndex);
    }


	this.prevPicIndex = picIndex;
	this.nextPicIndex = picIndex + 1 == this.imageData.length ? 0 : picIndex + 1;

	this.timeoutID = setTimeout(
		'timerExecute(' + this.timerArrayIndex + ',' + this.nextPicIndex + ')',
		this.interval
		);

	return this.timeoutID;
}

slideShow.prototype.pause = function () {
	clearTimeout(this.timeoutID);
	this.timeoutID = null;
}

slideShow.prototype.resume = function () {
	return this.running() ? null : this.processImage(this.nextPicIndex);
}

slideShow.prototype.nextImageImmediate = function () {
	if(this.running()) { this.pause(); }
	return this.resume();
}

slideShow.prototype.running = function () {
	return this.timeoutID != null ? 1 : 0;
}


function timerExecute (index, nextPicIndex)
{
	timerArray[index].processImage(nextPicIndex);
}
