/*
 * Disables the link to to the current page by changing the className 
 * attribute and removing the href attribute of the anchor.
 */
function deactivateLinksToActivePage() {
	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", deactivateLinksToActivePage, false);
