var images = [], index = 0;

function initThumbnailViewer() {
	var IMGs = document.getElementById("content").getElementsByTagName("img");
	var len = IMGs.length, i = 0;
	while (i < len) {
		var IMG = IMGs[i];
		var anchor = IMG.parentNode;
		if (anchor && anchor.parentNode && anchor.parentNode.className
			.indexOf("thumbnailset") != -1 && IMG.src.indexOf("spacer.gif") == -1) {
			anchor._href = anchor.href;
			anchor.removeAttribute("href");
			anchor.onmouseover = showImage;
			images.push(IMG);
		}
		i++;
	}
	var anchors = document.getElementById("thumbnailviewernavigation")
		.getElementsByTagName("a");
	anchors[0].onclick = function() {
		previousImage();
		return false;
	};
	anchors[1].onclick = function() {
		nextImage();
		return false;
	};
	showImage();
}

function showImage() {
	if (this.nodeName) index = getImageIndex(this.firstChild);
	var anchor = images[index].parentNode;
	document.getElementById("thumbnailviewerimage").style.backgroundImage = "url(" + anchor._href + ")";
	var heading = anchor.parentNode.firstChild;
	if (heading.nodeType == 3) heading = heading.nextSibling;
	document.getElementById("thumbnailviewertext").innerHTML = heading.innerHTML
		.replace(/<br>/gi, " ");
	var description = anchor.nextSibling;
	if (description) {
		if (description.nodeType == 3) description = description.nextSibling;
		if (description) {
			if (description.nodeName == "P") document
				.getElementById("thumbnaildescription").innerHTML = "<p>" + description.innerHTML + "</p>";
		}
	}
}

function getImageIndex(IMG) {
	var len = images.length, i = 0;
	while (i < len) {
		if (images[i] == IMG) {
			index = i;
			break;
		}
		i++;
	}
	return i;
}

function previousImage() {
	if (index > 0) index--;
	else index = images.length - 1;
	showImage();
}

function nextImage() {
	if (index < images.length - 1) index++;
	else index = 0;
	showImage();
}

ELO.functionsToCallOnload.push("initThumbnailViewer()");