// 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: sep 26 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 id's of the elements containing the menu
mainDiv = new Array("nav");

// the class to apply in place of current page link
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 id
for (var i=0; i<mainDiv.length; i++) {
	if (document.getElementById) var thisDiv = document.getElementById(mainDiv[i]);
    else if (document.all) var thisDiv = document.all[mainDiv[i]];
    // fail silently
    if (thisDiv)  {
		// go through this element
		setPage(thisDiv, 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.match(/^a$/gi)) {
			// 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;
	//alert('about to create node')
	var tempNod = document.createElement('SPAN');
	// apply thisPg class to it
	tempNod.className = thisPg;
	for (var i=0; i<pNode.childNodes.length; i++) {
		tempNod.appendChild(pNode.childNodes[i]);
	}
	//ocNod.insertBefore(tempNod, pNode);
	//alert('created node')
	// remove the url and its contents (this does not work properly in ie mac)
	//ocNod.removeChild(pNode);
	ocNod.replaceChild(tempNod,pNode);
	//alert('deleted node')
}