/* window.onload = function() {
	if ( document.getElementById("ajax-news") ) {
		updateNews("news/ajax.php", "ajax-news");
	}
} */


$(document).ready(function() {
	/* Highslide */
	hs.registerOverlay({ overlayId:'closebutton', position:'top right', fade:2 });
	
	// #nav
	// sfHover()
});

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

function sfHover() {
	if (document.all&&document.getElementById) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" over";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" over\\b"), "");
			}
		}
	}
}

/* Popup:
popup(URL, Width, Height, Left, Top, AutoCenter[0=False, 1=True], FullScreen[0=False, 1=True])
<a href="javascript:popUp('domain', 640, 480, 50, 50, 1, 0)">Popup!</a>
*/
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+"');");
	}
}

/* Auto Highlight Main Menu */

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;
    
    // potential TODO -- remove redundant slashes found in the path component
    
    return components;
}

function isSubDirectory(subDir, parentDir) {
    return subDir.indexOf(parentDir) == 0;
}

function debug(msg){document.body.appendChild(document.createTextNode(msg));}

$(document).ready(function(){
	
	function highlight(anchor){
		anchor.className = "active";
	}
	
	var subDirMatch = {"length":null};
	
	$("#navigation a").each(function(){
	
		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
				subDirMatch.link = this;
				subDirMatch.length = linkURL.path.length;
			}
		}
	});
	
	if(subDirMatch.link) {
		highlight(subDirMatch.link);
		return;
	}
});

