// remove link to current page in nav
// (c) 2006 eightize
// add call to this script at the end of the page
//   after all elements containing the menus
// last modified: aug 22 2006	 by: mb
//
// ** bug **
// because of explorer issue, there must be a space
// or another element before the closing tag of the
// container mainDiv
//  </a> </div>, not </a></div>

// the classes of the elements containing the menu
mainDiv = new Array("navtop", "navbottom");

// the class to apply to current page
thisPg = "currpg";

// default page url if root of site is called
//  script will also check for this link on page
//  leave blank to ignore
// i.e. index.html
defaultUrl = "index.html";

// the url of the current page
var RE = /^(.+)\?.*$/;
currPg = (document.location.href).replace( RE , '$1');

// go through document tree and find element with mainDiv class
for (var i=0; i<mainDiv.length; i++) {
	var thisDiv = getElementsByClass(mainDiv[i]);
	for (var j in thisDiv) {
			// go through this element
			setPage(thisDiv[j], currPg);
	}
}

// go through tree of elParnt looking for an element with 
//	href == aURL
function setPage(elParnt, aURL) {
	var nTree = elParnt.childNodes;
	for (var j=0; j<nTree.length; j++) {
		if (nTree[j].nodeName == 'A') {
			// only work on links, now check for match
			if (aURL == nTree[j].href || (aURL.match(/\/$/) && ((aURL+defaultUrl) == nTree[j].href))) {
				// this page is referenced, so change it
				clearLink(nTree[j]);
			}
		} else {
			if (nTree[j].childNodes) {
				// recurse through children
				setPage(nTree[j], aURL);
			}
		}
	}
}

// change the current page reference
function clearLink(pNode) {
	// copy all child nodes of pNode before pNode
	var ocNod = pNode.parentNode;
	for (var i=0; i<pNode.childNodes.length; i++) {
		var tempNod = document.createElement('SPAN');
		// apply thisPg class to it
		tempNod.className = thisPg;
		tempNod.appendChild(pNode.childNodes[i]);
		ocNod.insertBefore(tempNod, pNode);
	}
	// remove the url and its contents
	ocNod.removeChild(pNode);
}

// get elements by class name
// from http://www.dustindiaz.com/top-ten-javascript/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
	var j = 0;
	for (var i = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
