function changePhoto(p_sPhoto, p_sPhotoCaption, alt, width, height) {
	var eDiv = document.getElementById("mainimage");
	if (eDiv){
		arImg = document.getElementsByTagName("img");
		arImg[0].src = p_sPhoto;
		//var newImg = new Image();
		//newImg.src = p_sPhoto;

		arImg[0].setAttribute('width', width);
		arImg[0].setAttribute('height', height);
		arImg[0].setAttribute('title', alt);
		arImg[0].setAttribute('alt', alt);
		arCaption = document.getElementById("caption");
		arCaption.innerHTML = p_sPhotoCaption;
	}
}

function imageResize(imgWidth, imgHeight, targetWidth, targetHeight) {
		var shorter = targetHeight/imgHeight;
		var thinner = targetWidth/imgWidth;
		if( shorter<thinner && shorter<1 ){
			return shorter;
		}else if( thinner<1 ){
			return thinner;
		}else{
			return 1;
		}
}	

var ids = [];

//The array of thumbnail pages is built in photoGalleryAlbumPopupRight.tpl
function switchThumbPage(id){	
	hideallids();
	showdiv(id);
}

//this hides all of the thumnail page divs
function hideallids(){
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length;i++){
		hidediv(ids[i]);
	}		  
}

function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

//Shows the specific thumbnail page
function showdiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

//receives page id
//Launches link maker and div hider
function activatePage(divPre,linkPre,id,totalPages,imgId)
{
	if (imgId) {
		var myImg = document.getElementById('photo' + imgId);
		if (myImg) {
			var parent = myImg.parentNode;
			while (parent && parent.nodeName.toLowerCase() != 'div') {
				parent = parent.parentNode;
			}
			if (parent) thumbDivId = parent.id;
		}
		
		if (thumbDivId) {
			id = thumbDivId.replace(divPre, '');
		}
	}

	makeAllLinks(linkPre,divPre,totalPages);
	unmakeLink(linkPre,id);
	switchThumbPage(divPre + id);
}

function unmakeLink(linkPre,id)
{
	
		
		var justPage = document.createTextNode(id);
		var mySpan = document.getElementById(linkPre+id);
		var current = mySpan.childNodes[0];
		if(current){mySpan.removeChild(current)};
		mySpan.appendChild(justPage);
		

}
function makeAllLinks(linkPre,divPre,totalPages)
{
	for (var x=1; x<=totalPages;x++)
	{
		
		var link = document.createElement('a');
		link.setAttribute("href", "javascript:activatePage('" + divPre+"','"+ linkPre + "','" + x + "','" + totalPages + "')");
		var PageText = document.createTextNode(x);
		var Space = ' ';
		link.appendChild(PageText);
		var current = document.getElementById(linkPre+x).childNodes[0];
		var mySpan = document.getElementById(linkPre+x);
		if (current) {
			if (current.firstChild) {current.removeChild(current.firstChild)}
			
			mySpan.removeChild(current)
			
			};
		mySpan.appendChild(link);
		
		
		
	}
}

function redirectAndClose(url) {
	if (window.opener) {
		window.opener.location = url;
	} else {
		window.open(url);
	}
	window.close();
	return false;
}