var imageLoaded = true;
var br = navigator.userAgent.toLowerCase();
var compatible = (document.getElementById && br.indexOf("opera") == -1 && br.indexOf("netscape6") == -1);

function switchDescription(imageDescription)
{
	var oldDesc = document.getElementById("description");
	var newDesc = document.createElement(oldDesc.nodeName);
	newDesc.id = "description";
	
	var lastFoundBr = 0;
	for (i = 0; i < imageDescription.length; i++)
	{
		if (imageDescription.substr(i, 4) == "<br>")
		{
			newDesc.appendChild(document.createTextNode(imageDescription.substr(lastFoundBr, (i - lastFoundBr))));
			lastFoundBr = i + 4;
			newDesc.appendChild(document.createElement("BR"));
			i = i + 3;
		}
	}
	newDesc.appendChild(document.createTextNode(imageDescription.substr(lastFoundBr, (i - lastFoundBr))));
	
	oldDesc.parentNode.replaceChild(newDesc, oldDesc);
}

function hideImage()
{
	if (!imageLoaded)
	{
		document.getElementById("slideshow").style.visibility = "hidden";
		document.getElementById("loading").style.visibility = "visible";
	}
}

function updateImage()
{
	if (document.getElementById)
	{
		if (image == 1) document.getElementById("prevImage").style.visibility = "hidden";
		else document.getElementById("prevImage").style.visibility = "visible";
		
		if (image == numberOfImages) document.getElementById("nextImage").style.visibility = "hidden";
		else document.getElementById("nextImage").style.visibility = "visible";
		
		
		
		var descriptionSpan = document.getElementById("description");
		var pageSpan = document.getElementById("page");
		
		tempDescription = imageDescription[image - 1];
		switchDescription("");
		
		imageLoaded = false;
		document.getElementById("slideshow").src = "img/" + subalbumNumber + "/" + image + "_slideshow.jpg";
		
		
		if (compatible) setTimeout("hideImage()", 500);
		else photoLoaded();
		
		if (!dontShowLink) document.getElementById("large").href = "img/" + subalbumNumber + "/" + image + ".jpg";
		document.getElementById("thumbnails").href = "subalbum_" + subalbumNumber + ".html#img" + image;
		pageSpan.replaceChild(document.createTextNode(image), pageSpan.childNodes[0]);
	}
	else
	{
		self.location.href = "slideshow_" + subalbumNumber + ".html?" + image;
	}
}

function photoLoaded()
{
	imageLoaded = true;
	
	document.getElementById("loading").style.visibility = "hidden";
	document.getElementById("slideshow").style.visibility = "visible";
	
	switchDescription(tempDescription);
}

function pImage()
{
	image = (image > 1) ? image - 1 : numberOfImages;
	if (numberOfImages > 0) updateImage();
}

function nImage()
{
	image = (image < numberOfImages) ? image + 1 : 1;
	if (numberOfImages > 0) updateImage();
}
