var paramparent;
var paramtop;
// Change the "back buttons" on a linked index page, to return to its parent album
function pageInit(pTopURL)
{	getParams();
	paramparent = params[paramparentname];
	paramtop = params[paramtopname];
	if (params[paramgotopname] == 'y')
	{	location.replace(pTopURL);
	}
}

function enableIndexParent(pShowParent, pShowTop, pBack, pTop)
{	var vParentLink = document.getElementById('parentLink');
	var vParentButton = document.getElementById('parentButton');
	var vTopLink = document.getElementById('topLink');
	var vTopButton = document.getElementById('topButton');
	if (paramparent != null)
	{	var vDocURL = fDocURL();
		if (paramtop == null)
		{	paramtop = vDocURL;
		}
		vParentButton.style.visibility = 'visible';
		if (vParentLink.href == '' && vTopLink != null && vTopLink.href != '')
		{	vParentLink.href = vTopLink.href;
		}
		if (vParentLink.href == ''
			|| paramtop == vDocURL
			|| (paramtop.substring(paramtop.length - 1) == '/'
				&& paramtop == vDocURL.substring(0, vDocURL.lastIndexOf('/', vDocURL) + 1)
				)
			|| (paramtop.substring(paramtop.length - 1) != '/' 
				&& paramtop.substring(paramtop.length - 4) != '.htm'
				&& paramtop.substring(paramtop.length - 5) != '.html'
				&& paramtop == vDocURL.substring(0, vDocURL.lastIndexOf('/', vDocURL))
				)
			)
		{	vParentLink.href = paramparent;
			vParentButton.title = pBack;
		}
		else
		{	var vParamPosition = vParentLink.href.indexOf('?');
			if (vParamPosition > 0)
			{	vParentLink.href = vParentLink.href.substr(0, vParamPosition);
			}
			vParentLink.href += '?' + paramparentname + '=' + paramparent + '&' + paramtopname + '=' + paramtop;
		}
		if (vTopLink != null)
		{	vTopLink.href = paramparent + '?' + paramgotopname + '=y';
			vTopButton.style.visibility = 'visible'
			vTopButton.title = pTop;
		}
	}
	if (pShowParent == 'Y')
	{	vParentButton.style.visibility = 'visible'
	}
	if (pShowTop == 'Y')
	{	vTopButton.style.visibility = 'visible'
	}
}

// If the page is in a linked album, or is about to link to another album, change the address in the href
function makeHref(pObj, pIsLink)
{
//	If opening a url, and "new_window" was specified, we should not be here.
//	If opening a remote url from a local web page, open in a new window without parameters.
//	If not opening a url, pass the incoming parameters
//	If opening a url, and there are no incoming parameters, build the parent parameter & attach to the href.
//	If opening a url, and there are incoming parameters, open in a new window without parameters.
	var vhref = pObj.href;
	var vLinked = (paramparent != null);

	if (pIsLink)
	{	if (vLinked || (location.href.substr(0,5) == "file:" && pObj.href.substr(0,5) == "http:"))
		{	pObj.target="_blank";
		}
		else
		{	vhref += '?' + paramparentname + '=' + fDocURL();
		}
	}
	else if (vLinked)
	{	vhref += '?' + paramparentname + '=' + paramparent
					 + '&' + paramtopname + '=' + (paramtop == null ? fDocURL() : paramtop);
	}

	pObj.href = vhref;
}

var vParentIndexPath; // The path to the parent index page
var vRootIndexPath;   // The path to the root index page

// Keyboard navigation
document.onkeydown =
	function fKeyDown(pEvent)
	{ if (typeof _jaWidgetFocus != 'undefined' && _jaWidgetFocus)
		{	return;
		}

		var vEvent = (pEvent ? pEvent : window.event);
		var vKeyCode = (window.Event ? vEvent.which : vEvent.keyCode);
		var vTarget = null;
		var vButtonId = null;
		if (vEvent.shiftKey)
		{	switch (vKeyCode)
			{	case(37): vButtonId = (document.getElementById('pfirst') == null ? 'pprev' : 'pfirst'); break; // left - first index page on this level
				case(38): vTarget = vRootIndexPath; break;   // up - root index page
				case(39): vButtonId = (document.getElementById('plast') == null ? 'pnext' : 'plast'); break; // right - last index page on this level
			}
		}
		else if (!vEvent.ctrlKey && !vEvent.altKey)
		{	switch (vKeyCode)
			{	case(37): vButtonId = 'pprev'; break;        // left - previous index page on this level
				case(38): vTarget = vParentIndexPath; break; // up - parent index page
				case(39): vButtonId = 'pnext'; break;        // right - next index page on this level
			}
		}
		if (vButtonId != null)
		{	vTarget = document.getElementById(vButtonId);
		}
	  if (vTarget != null)
	  { document.location = vTarget;
	  }
	}

