function PreLoadImage(url, caption, displayFunction) {
	this.url = url;
	this.caption = caption;
	this.width = null;
	this.height = null;
	this.imageObject = null;
	this.displayFunction = displayFunction;
};

PreLoadImage.prototype.loadImage = function() {
	if (this.imageObject == null) {
		this.imageObject = new Image();
		this.imageObject.src = this.url;
	}
	var width = this.imageObject.width;
	var height = this.imageObject.height;
	if (typeof(width) == 'string') {
		width = parseInt(width);
	}
	if (typeof(width) != 'number') {
		return false;
	}
	if (typeof(height) == 'string') {
		height = parseInt(height);
	}
	if (typeof(height) != 'number') {
		return false;
	}
	if (this.imageObject.width < 1) {
		return false;
	}
	if (this.imageObject.height < 1) {
		return false;
	}
	
	this.displayFunction({width:this.imageObject.width, height:this.imageObject.height}, this.url, this.caption);
	
	return true;
};

function PhotoSlideShow(slides, waitTime) {
	this.slides = slides;
	this.currentSlide = 0;
	this.preLoadedImage = null;
	this.waitTime = typeof(waitTime) != 'number' ? 10000 : waitTime < 3 ? 3000 : waitTime > 20 ? 20000 : waitTime * 1000;
	this.runningVersion = 0;
	this.clickVersion = 0;
	this.firstImage = true;
};

PhotoSlideShow.prototype.setInstance = function() {
		document.slideShow = this;
}

PhotoSlideShow.prototype.nextSlide = function(displayFunction) {
	if (!this.slides) {
		return null;
	}
	var length = this.slides.length;
	if (length < 1) {
		return null;
	}
	this.preLoadedImage = null;
	var caption = "[" + (this.currentSlide + 1) + "&nbsp;of&nbsp;" + length + "] &mdash; " + this.slides[this.currentSlide].caption;
	this.preLoadedImage = new PreLoadImage(
		this.slides[this.currentSlide].url, caption, displayFunction);
	staticLoadImage(0);
	if (this.currentSlide < length - 1) {
		this.currentSlide++;
	}
	else {
		this.currentSlide = 0;
	}
};

PhotoSlideShow.prototype.reSchedule = function() {
	if (this.runningVersion == this.clickVersion) {
		loadOtherImage();
		if (this.firstImage) {
			// Well, this is a fix for an IE rendering quirk
			this.firstImage = false;
			this.currentSlide = 0;
			window.setTimeout(function() { document.slideShow.reSchedule(); }, 100);
		}
		else {
			window.setTimeout(function() { document.slideShow.reSchedule(); }, this.waitTime);
		}
	}
}

PhotoSlideShow.prototype.pause = function() {
	this.clickVersion++;
	var stopstart = document.getElementById("stopstart");
	if (!stopstart) {
		return ;
	}
	stopstart.innerHTML = '<span style="color: green; font-weight: bold;">[>>]</span>';
}

PhotoSlideShow.prototype.resume = function() {
	this.runningVersion = this.clickVersion;
	window.setTimeout(function() { document.slideShow.reSchedule(); }, 250);
	var stopstart = document.getElementById("stopstart");
	if (!stopstart) {
		return ;
	}
	stopstart.innerHTML =  '<span style="color: red; font-weight: bold;">[||]</span>';
}

PhotoSlideShow.prototype.isPaused = function() {
	return this.runningVersion != this.clickVersion;
}

PhotoSlideShow.prototype.skipToNext = function(amount) {
	if (typeof(amount) != 'number') {
		return ;
	}
	this.clickVersion++;
	this.pause();
	if (amount <= 0) {
		this.currentSlide = this.slides.length - 1;
	}
	else {
		newValue = this.currentSlide + amount;
		if (newValue < 0) {
			this.currentSlide = 0;
		}
		else if (newValue >= this.slides.length) {
			this.slides.length - 1;
		}
		else {
			this.currentSlide = newValue;
		}
	}
	loadOtherImage();
}

PhotoSlideShow.prototype.skipToPrevious = function(amount) {
	if (typeof(amount) != 'number') {
		return ;
	}
	this.clickVersion++;
	this.pause();
	if (amount <= 0) {
		this.currentSlide = 0;
	}
	else {
		newValue = this.currentSlide - amount - 1;
		if (newValue < 0) {
			this.currentSlide = 0;
		}
		else if (newValue >= this.slides.length) {
			this.slides.length - 1;
		}
		else {
			this.currentSlide = newValue;
		}
	}
	loadOtherImage();
}

PhotoSlideShow.prototype.loadImage = function () {
	return (this.preLoadedImage) && this.preLoadedImage.loadImage();
};

function staticLoadImage(counter) {
	if (!counter) {
		counter = 1;
	}
	if (document.slideShow) {
		if (!document.slideShow.loadImage()) {
		  if (counter > 25) {
		  	alert("Loading image takes long...");
		  	counter = 0;
		  }
			window.setTimeout("staticLoadImage(" + (counter + 1) + ")", 200);
		}
	}
}
	
function removeAllChildren(targetElement) {
		if (targetElement && targetElement.childNodes) {
				for (var rloop = targetElement.childNodes.length -1; rloop >= 0 ; rloop--) {
						targetElement.removeChild(targetElement.childNodes[rloop]);
				}
		}
}

function loadResizedImage(imageSize, imageUrl,caption) {
	var screenWidth = getInsideWindowWidth();
	var screenHeight = getInsideWindowHeight();

	if (imageSize == null) {
		return false;
	}
	var imageWidth = imageSize.width;
	var imageHeight = imageSize.height;

	//alert("Image size is " + imageWidth + " x " + imageHeight);

	if (imageWidth < 1) {
		imageWidth = 1;
	}
	if (imageHeight < 1) {
		imageHeight = 1;
	}
	var headerBottom = 0;//getStyleTop("contentHeader") + getStyleHeight("contentHeader") + 3;
	var footerHeight = 0;//getStyleHeight("contentFooter");
	var maxImageHeight = screenHeight - headerBottom - footerHeight;

	if (maxImageHeight < 100) {
		maxImageHeight = screenHeight;
	}

	var horScale = screenWidth / imageWidth;
	var verScale = maxImageHeight / imageHeight;
	var minScale = horScale > verScale ? verScale : horScale;


	var resizedWidth = Math.round(imageWidth * minScale);
	var resizedHeight = Math.round(imageHeight * minScale);
	var resizedLeft = Math.round((screenWidth - resizedWidth - 1) / 2);
	var resizedTop = headerBottom + Math.round((maxImageHeight - resizedHeight - 1) / 2);

	var image = document.getElementById("imageObject");
	if (image) {
		image.parentNode.removeChild(image);
		image = null;
	}

	var imageCreate =  "<img" +
			" id=\"imageObject\"" +
			" style=\"position:absolute; left:" + resizedLeft + "px; top:" + resizedTop + "px; width:" + resizedWidth + "px; height:" + resizedHeight + "px; z-index:1;\"" +
			" src=\"" + encodeURI(imageUrl) + "\" />";

	setDescription(caption)
	var canvas = document.getElementById("imageCanvas");
	if (canvas) {
		canvas.innerHTML = imageCreate;
	}

	var image = document.getElementById("imageObject");
	if (image) {
		image.originalWidth = imageWidth;
		image.originalHeight = imageHeight;
	}
}
	
function loadOtherImage() {
	var canvas = document.getElementById("imageCanvas");
	if (!canvas) {
		return ;
	}
	if (canvas.stopSlideShow && canvas.stopSlideShow == true) {
		return;
	}
	if (!document.slideShow) {
		return ;
	}
	document.slideShow.nextSlide(function(imageSize, imageUrl, caption) { loadResizedImage(imageSize, imageUrl, caption); } );
}



function setDescription(message) {
	var label = document.getElementById("descriptionLabel");
	if (label) {
		label.innerHTML = "" + message;
	}
}

function pauseOrResumeShow() {
	if (document.slideShow.isPaused() == true) {
		document.slideShow.resume();
	}
	else {
		document.slideShow.pause();
	}
}

function skipToNext(amount) {
	document.slideShow.skipToNext(amount);
}

function skipToPrevious(amount) {
	document.slideShow.skipToPrevious(amount);
}

function startShow() {
	if (document.slideShow) {
		document.slideShow.reSchedule();
	}
	var element = document.getElementById("contentHeader");
	if (element.attachEvent) {
	  element.setAttribute("className", "headerfooter_ie");
	}
}
