var local_index = -1;
var gallery_list = null;

var box_index = -1;

var regExp = /<\/?[^>]+>/gi;
function replaceTags(xStr){
	xStr = xStr.replace(regExp,"");
	return xStr;
}

function showPhotoBox(linksdiv, detailsdiv)
{
	document.getElementById(linksdiv).style.display = "block";
	document.getElementById(detailsdiv).style.display = "none";
	document.getElementById("mainPhotoBox").style.display = "block";
	
	document.getElementById("buttons_list").style.display = "block";
	document.getElementById("buttons_zoom").style.display = "none";
}

function hidePhotoBox()
{
	document.getElementById("mainPhotoBox").style.display = "none";
}

function init_gallery(current_image)
{
	if (gallery_list == null)
	{
		gallery_list = document.getElementById("gallerylist").innerHTML.split(',');
	}
	
	if (local_index == -1 && current_image != -1) 
	{
		// find the current image in the list
		for (var i = 0; i < gallery_list.length; i++)
		{
			if (gallery_list[i] == current_image) {
				local_index = i;
				break;
			}
		}
	}
}

function gallery_back(myid, current_image)
{
	init_gallery(current_image);
		
	var obj = document.getElementById(myid);
	
	// find the previous image id
	if (local_index == 0) {
		// already at beginning of list
		local_index = gallery_list.length - 1;
	} else {
		local_index--;
	}
	
	document.getElementById("quoteHero").innerHTML = document.getElementById("gallerycaption_" + gallery_list[local_index]).innerHTML;
	obj.src = document.getElementById("galleryimg_" + gallery_list[local_index]).innerHTML;
}

function gallery_next(myid, current_image)
{
	init_gallery(current_image);
	
	var obj = document.getElementById(myid);
	
	// find the next image id
	if (local_index == (gallery_list.length - 1)) {
		// already at end of list
		local_index = 0;
	} else {
		local_index++;
	}
	
	document.getElementById("quoteHero").innerHTML = document.getElementById("gallerycaption_" + gallery_list[local_index]).innerHTML;
	obj.src = document.getElementById("galleryimg_" + gallery_list[local_index]).innerHTML;
}

function directZoom(linksdiv, detailsdiv, current_image)
{
	if (local_index == -1) {
		init_gallery(current_image);
	} else {
		init_gallery(-1);
	}
	showPhotoBox(linksdiv, detailsdiv);
	loadZoom(linksdiv, detailsdiv, "zoomdiv_" + gallery_list[local_index]);
}

function loadZoom(linksdiv, detailsdiv, zoomid)
{
	init_gallery(-1);

	document.getElementById("buttons_list").style.display = "none";
	document.getElementById("buttons_zoom").style.display = "block";
	document.getElementById(linksdiv).style.display = "none";
	
	clearDetails(detailsdiv);
	document.getElementById(detailsdiv).style.display = "block";
	
	var thisImage = zoomid.replace("zoomdiv_", "");
	
	// find the current image in the list
	for (var i = 0; i < gallery_list.length; i++)
	{
		if (gallery_list[i] == thisImage) {
			box_index = i;
			break;
		}
	}
	
	loadLargeImage(zoomid);
	document.getElementById(zoomid).style.display = "block";
}

function loadLargeImage(zoomid)
{
	var imgSpan = document.getElementById(zoomid + "_imgspan");
	var img = document.getElementById(zoomid + "_img");
	var imgSrc = replaceTags(imgSpan.innerHTML);
	if (imgSrc != "") {
		img.src = imgSrc;
		imgSpan.innerHTML = imgSpan.innerHTML.replace(imgSrc, "");
	}
}

function clearDetails(detailsdiv)
{
	// clear out everything in details
	var objP = document.getElementById(detailsdiv);
	var children = objP.childNodes;
	for (var i = 0; i < children.length; i++) {
		if (children[i].nodeType == 1) {
			children[i].style.display = "none";
		}
	}
}

function prevZoom(detailsdiv)
{
	if (box_index == 0) {
		// already at the beginning
		box_index = gallery_list.length - 1;
	} else {
		box_index--;
	}
	clearDetails(detailsdiv);
	
	loadLargeImage("zoomdiv_" + gallery_list[box_index]);
	document.getElementById("zoomdiv_" + gallery_list[box_index]).style.display = "block";
}

function nextZoom(detailsdiv)
{
	if (box_index == (gallery_list.length - 1)) {
		// already at end of list
		box_index = 0;
	} else {
		box_index++;
	}
	clearDetails(detailsdiv);
	
	loadLargeImage("zoomdiv_" + gallery_list[box_index]);
	document.getElementById("zoomdiv_" + gallery_list[box_index]).style.display = "block";
}

function externaliseLinks()
{ 
	if (!document.getElementsByTagName) 
	{	
		return;
	}

	var anchors = document.getElementsByTagName("a"); 
	for ( var i=0; i<anchors.length; i++ ) 
	{ 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
		{
			anchor.target = "_blank"; 
		}
	} 
} 

function PostBack(objField, objButton) {
	if (event.which || event.keyCode) {
		if ((event.which == 13) || (event.keyCode == 13)) {
			objField.blur();
			document.getElementById(objButton).click();
			return false;
		}
	} 
	else {
		return true;
	}
}


function init() {
	externaliseLinks();
}
window.onload = init;