/*
 * Disables the link to to the current page by changing the className 
 * attribute and removing the href attribute of the anchor.
 */
function disableLinkToCurrentPage() {
	if (!document.getElementsByTagName) {
		return;
	}
	var anchors = document.getElementsByTagName("a");
	var pageHREF = trimURL(location.href);
	for (var i=0; i < anchors.length; i++) {
		var anchor = anchors[i];
		anchorHREF = anchor.getAttribute("href");
		if ((anchorHREF == pageHREF) || (getAbsoluteURL(anchorHREF) == pageHREF)){
			anchor.className = "current";
			anchor.removeAttribute("href");
			return;
		}
	}
}

/*
 * Given a relative URL, determine the absolute URL.
 */
function getAbsoluteURL(relURL) {
	var absURL = location.protocol + "//" + location.hostname;
	var pos = location.pathname.lastIndexOf("/");
	absURL += location.pathname.substring(0, pos + 1) + relURL;
	return absURL;
}

/*
 * Remove internal anchors or query strings from URL.
 */
function trimURL(inURL) {
	var found = false;
	var outURL = "";
	var pos = inURL.lastIndexOf("#");
	if (pos < 0) {
		pos = inURL.lastIndexOf("?");
		if (pos < 0) {
			outURL = inURL;
		} else {
			found = true;
		}
	} else {
		found = true;
	}
	if (found) {
		outURL = inURL.substring(0, pos);
	}
	return outURL;
}

/*
 * Adds an event handler to a DOM object.
 */
function addEvent(obj, eventType, fn, useCapture) {
	if (obj.addEventListener) {
		obj.addEventListener(eventType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on" + eventType, fn);
		return r;
	} else {
		alert("Handler could not be attached");
	}
}

addEvent(window, "load", disableLinkToCurrentPage, false);
