$(document).ready(function(){
	
	//startList();
	
	function highlight(anchor){
		anchor.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;
			}
		}
	}
	$("#top-nav-menu a").each(highlightThisPageLink);
	if(subDirMatch.link) {
		highlight(subDirMatch.link);
		return;
	}
});

/* ----------------------
   Scripts
---------------------- */


function startList() {
  if (document.all&&document.getElementById) {
    navRoot = document.getElementById("nav_dropdown");
    for (i=0; i<navRoot.childNodes.length; i++) {
      node = navRoot.childNodes[i];
      if (node.nodeName == "LI") {
        node.onmouseover = function() {
          this.className += " over";
        }
        node.onmouseout = function() {
          this.className = this.className.replace(" over", "");
        }
      }
    }
  }
}

/* Popups */
function popUp(URL, popWidth, popHeight, popLeft, popTop, autoCenter, fullScreen) {
	day = new Date();
	id = day.getTime();
	if (autoCenter == 1) {
		var popLeft = (screen.width - popWidth) / 2;
		var popTop = (screen.height - popHeight) / 2;
	}
	if (fullScreen == 1) {
		// Open in Full Screen window!
		eval("page"+id+" = window.open(URL, '"+id+"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+(screen.width-10)+",height="+(screen.height-26)+",left=0,top=0');");
	} else {
		// Open in normal window!
		eval("page"+id+" = window.open(URL, '"+id+"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+popWidth+",height="+popHeight+",left="+popLeft+",top="+popTop+"');");
	}
}

/* Toggle Content */
function toggleDiv( element, showhide ) {
	var e = document.getElementById(element);
	if ( e ) {
		switch ( showhide ) {
			case "true":
				e.style.display = 'inline';
				break;
			case "false":
				e.style.display = 'none';
				break;
			case "default":
				e.style.display = ((e.style.display != 'inline') ? 'inline' : 'none');
				break;
		}
	}
}

/* URL parsing / utils */
var absoluteUrlPattern = /^([^:\/?#]+):\/\/(([^@\/?#]*)@)?([^:\/?#]*)(:([0123456789]*))?([^?#]*)(\?([^#]*))?(#(.*))?/g;
function parseAbsoluteURL(urlString) {
    var components = {"scheme":null,"userinfo":null,"hostname":null,"host":null,"port":null,"path":null,"query":null,"fragment":null};
    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;}
