// The variable menuID is set to the name (the ID) of the menu-containing, outer UL tag. It isn't necessary to use this variable or to have the ID of the UL set for the menu script to work (aside for the case where the menu page contains more ULs than the menu UL - then you need menuID set). Also, the styling rules in the CSS file uses the name of the menu-containing, outer UL tag - so remember to look in that file too, if you wish to change this value:
var menuID = 'siteMenu';
// If one sets the variable noFollowClass to a name, for example 'noFollow', then one can set class="noFollow" on the A tags in the menu that one wished would not be followed (e.g. if one wanted to have heading links for submenus where these heading LI links were to just open the submenu, without taking the user to a URL):
var noFollowClass = '';

function initNavi() {
	if (window.tryCatch) tryCheckingViewedURL();
	var x = document.getElementsByTagName('a');
	var y;
	for (var i=0;i<x.length;i++) {
		y = x[i];
		while (y.parentNode && y.nodeName != 'LI') y = y.parentNode;
		if (y.nodeName == 'LI') {
			x[i].onclick = clickNav;
			// For Explorer 6 (Win) [but not for Explorer 5], we remove the focus rectangle from a clicked link. Unfortunately, just using x[i].setAttribute('hidefocus','true') does not work on Explorer 6 (Win) since "hidefocus" is not a reckognized attribute:
			if (document.createAttribute) {
				var newAttr = document.createAttribute("hidefocus");
				newAttr.value = "true";
				x[i].setAttributeNode(newAttr);
			}
		}
	}
	closeAllBranches();
	if (parent.setNav) setViewedURL(parent.setNav,'currentPage', true);
}

function closeBranch(node) {
	if (node.childNodes && (node.nodeName == 'LI' || node.nodeName == 'UL')) {
		for(var i=0; i<node.childNodes.length; i++) closeBranch(node.childNodes[i]);
		if (node.nodeName == 'UL' && node.parentNode.nodeName == 'LI') node.style.display = 'none';
	}
}

function closeAllBranches() {
	if (window.menuID && window.menuID.length>0) {
		var menuTag = document.getElementById(menuID);
		if (menuTag) closeBranch(menuTag);
	}
	else { // If we don't know if there are more ULs on the page - aside from the menu UL - and we don't know which UL is the root UL for the menu, we simply guess that it's the first UL on the page:
		var x = document.getElementsByTagName('ul');
		if (x.length>0) closeBranch(x[0]);
	}
}

function closeOtherBranches(tg) {
	var branchTop = tg;
	while (tg.parentNode && (tg.parentNode.nodeName == 'LI' || tg.parentNode.nodeName == 'UL')) {
		branchTop = tg;
		tg = tg.parentNode;
	}
	// tg is now the menu-containing, outer UL tag
	// branchTop is a child to tg and the top level of the branch that was clicked
	var x = tg.childNodes;
	for (var i=0;i<x.length;i++) {
		if (x[i] != branchTop) {
			closeBranch(x[i]);
		}
	}
}

function setViewedURL(tg,newID,firstRun) {
	var theAtag = tg;
	// Set the new ID for the A tag:
	tg.id = newID;
	// In the case of an onClick event on the menu, we already know that the A tag will have an LI parent, but we have to check for its presence in the case this is the first time the page loads (and we're not sure if the page in the content frame actually has a link in the menu):
	while (tg.parentNode && tg.nodeName != 'LI') tg = tg.parentNode;
	if (tg.nodeName == 'LI') {
		// Set the new ID for the LI tag:
		tg.id = newID;
		if (newID == 'currentPage') {
			// See to it that all ULs in all other branches, except the currently clicked, are closed:
			closeOtherBranches(tg);
			var liChildren = tg.childNodes;
			for (var i=0;i<liChildren.length;i++) {
				if (liChildren[i].nodeName == 'UL') {
					var ulStatus = 'block';
					// A submenu to an LI tag whose A tag was clicked should only toggle visible/hidden if the noFollowClass is in use (and the clicked A tag has this class set):
					if (noFollowClass && theAtag.className == noFollowClass) { ulStatus = (liChildren[i].style.display == 'none') ? 'block' : 'none'; }
					liChildren[i].style.display = ulStatus;
				}
			}
			// The time that initNavi() [and the script on the content page] is run at startup (and at any subsequent refreshing of the page), we traverse upwards in the tree and see to it that every parent-UL is displayed:
			if (firstRun) {
				while (tg.parentNode && (tg.parentNode.nodeName == 'LI' || tg.parentNode.nodeName == 'UL')) {
					tg = tg.parentNode;
					if (tg.nodeName == 'UL') tg.style.display = 'block';
				}
			}
		}
	}
}

function clickNav(e) {
	window.clickedLink = true;
	if (!e) var e = window.event;
	if (e.target) var tg = e.target;
	else if (e.srcElement) var tg = e.srcElement;
	while (tg.nodeName != 'A') // In the browser "Safari", the target is the actual TEXT that is clicked on - not the A tag
		tg = tg.parentNode;
	if (parent.setNav) { setViewedURL(parent.setNav,''); }
	setViewedURL(tg,'currentPage');
	parent.setNav = tg;
	if (noFollowClass && tg.className == noFollowClass) return false;
	else return true;
}

function getTagFromHREF(page) {
	// If there's a hash-character in the URL, we remove it, since it would confuse our script:
	var test = page.indexOf('#')+1;
	if (test) page = page.substring(0,test-1);
	var x = document.getElementsByTagName('a');
	for (var i=0;i<x.length;i++) {
		if (x[i].href == page) {
			return x[i];
		}
	}
	return '';
}

function tryCheckingViewedURL() {
	try {
		parent.setNav = getTagFromHREF(parent.main.location.href);
	}
	catch(errorObject) {
//		parent.setNav = '';
		return;
	}
}
