$(document).ready(function() {
	selectNavMenu("#site-nav a");
	
	if($.browser.msie && $.browser.version == "6.0"){
		function addHoverClass(){$(this).addClass("hover");}
		function removeHoverClass(){$(this).removeClass("hover");}
		$("#left-nav li").mouseenter(addHoverClass).mouseleave(removeHoverClass);
	}
});

function selectNavMenu(selector){
	function highlight(anchor){
		anchor.parentNode.className = "selected";
	}
	var subDirMatch = {"length":null};
	function highlightThisPageLink(){
		var linkURL = parseAbsoluteURL(this.href);
		if(!linkURL || location.host != linkURL.host) return; //external link
		
		if(linkURL.path == location.pathname){
			highlight(this);
			return;
		}
		if(location.pathname == "/" && linkURL.path == "/index.htm"){
			highlight(this);
			return;
		}
		if((linkURL.path !="/" || location.pathname == "/index.htm") && isSubDirectory(location.pathname, linkURL.path)){
			if(linkURL.path.length > subDirMatch.length) { //Match longest sub directory path
				subDirMatch.link = this;
				subDirMatch.length = linkURL.path.length;
			}
		}
	}
	$(selector).each(highlightThisPageLink);
	if(subDirMatch.link) {
		highlight(subDirMatch.link);
		return;
	}
}

var absoluteUrlPattern = /^([^:\/?#]+):\/\/(([^@\/?#]*)@)?([^:\/?#]*)(:([0123456789]*))?([^?#]*)(\?([^#]*))?(#(.*))?/g;
function parseAbsoluteURL(urlString) {
    var components = {};
    var a = absoluteUrlPattern.exec(urlString);
    absoluteUrlPattern.lastIndex = 0;
    if(!a) return null;
    components.scheme   = a[1];
    components.userinfo = a[3];
    components.hostname = a[4];
    components.port     = a[6];
    components.path     = a[7];
    components.query    = a[9];
    components.fragment = a[11];
    components.host = components.hostname;
    if(components.port) components.host += ":" + components.port;
    return components;
}
function isSubDirectory(subDir, parentDir){return subDir.indexOf(parentDir) == 0;}

