// Name of indexed page on webserver
NAV_INDEX = "index.html";
// CCS class of active link
CSS_NAV_ACTIVE = "active";
// CSS class of unclickable links
CSS_NAV_NOCLICK = "no_click";

function init(e) {
	var url = /^((http|https):\/\/([A-Za-z0-9\-\.]*)\/(.*\/)?)([^\/\?#\:]*)(\:[0-9]+)?(\?(.*))?(#(.*))?$/.exec(window.location.href);
	var prefix = url[1];
	var script_name = url[5];
	var url = url[0];
	
	// Set approprate nav link active
	( function() {
		function add_class(a, new_class) {
			var classes = a.className.split(" ");
			classes.push(new_class);
			a.className = classes.join(" ").replace(/\s+/, " ").replace(/^\s+|\s+$/, "");
		}
			  
		var nav = document.getElementById("nav");
		if ( !nav ) return;
		
		var ul = document.getElementsByTagName("UL");
		if ( ul.length == 0 ) return;
		ul = ul[0];
		
		var li = ul.getElementsByTagName("LI");
		
		var homepage = prefix+NAV_INDEX;
		
		var i;
		var a;
		
		
		if ( script_name == "" ) for ( i = 0; i < li.length; i++ ) {
			a = li[i].getElementsByTagName("a");
			if ( a.length > 0 && a[0].href == homepage ) {
				add_class(li[i], CSS_NAV_ACTIVE);
				add_class(li[i], CSS_NAV_NOCLICK);
				a[0].onclick = function(e) { return false; };
				return;
			}
		}
		else {
			var section_end = script_name.indexOf("__");
			var section;
			if ( section_end == -1 ) section = /(.*)\.(?:[^\.]*)$/.exec(script_name)[1];
			else section = script_name.substr(0, section_end);
			
			var a_section;
			for ( i = 0; i < li.length; i++ ) {
				a = li[i].getElementsByTagName("A");
				if ( a.length > 0 ) {
					a_section = /\/(([^\/]*)\.[^\.]*)$/.exec(a[0].href);
					if ( section == a_section[2] ) {
						add_class(li[i], CSS_NAV_ACTIVE);
						if ( script_name == a_section[1] ) {
							add_class(li[i], CSS_NAV_NOCLICK);
							a[0].onclick = function(e) { return false; };
						}
						return;
					}
				}
			}
		}
	} )();
}

if ( window.addEventListener ) window.addEventListener("load", init, false);
else window.attachEvent("onload", init);
