// (c) copyright 2008 www.softidiom.com

String.prototype.endsWith = function(str) {
	return (this.match(str+"$")==str)
}

window.onload=function(){
	highlightNavigation();
	initScrollDetector();
}

function highlightNavigation() {
	//highlight section menu item defined in global from html called navSection
	var menu = getId('menu');
	if(menu) {
		var menuLinks = menu.getElementsByTagName('a');
		for (var i=0;i<menuLinks.length;i++) {
			//if this link is the current section, change its class for visual feedback
			if(menuLinks[i].title == navSection) {
				menuLinks[i].className = 'selected';
			}
		}
	}
	//highlight any submenus that link to the current url
	var uri = new Object();
	getURI(uri);
	var submenu = getId('submenu');
	if(submenu) {
		var sublinks = submenu.getElementsByTagName('a');
		for (var i=0;i<sublinks.length;i++) {
			//if this link is the current page, change its class for visual feedback
			var href = escape(sublinks[i].href);
			var loc = escape(uri.fileWithArgs);
			if(href.endsWith(loc)) {
				sublinks[i].className = 'selected';
			}
		}
	}
}

function initScrollDetector() {
	//start scroll detection every 1/2 a second
	setInterval("showIfScrolled()", 500);
}

function showIfScrolled() {
	//show the back to top link if we are scrolled
	var scrollTop = 0;
	if (navigator.appName == "Microsoft Internet Explorer") {
		scrollTop = document.documentElement.scrollTop;
	} else {
		scrollTop = window.pageYOffset;
	}
	if(scrollTop < 20) {
		getId('backToTop').style.visibility = 'hidden';
	} else {
		getId('backToTop').style.visibility = 'visible';
	}
}

function getId(id) { 
	//browser independent get element by id
    if (document.getElementById) 
        var returnVar = document.getElementById(id); 
    else if (document.all) 
        var returnVar = document.all[id]; 
    else if (document.layers) 
        var returnVar = document.layers[id]; 
    return returnVar; 
}

function getURI(uri) {
	//answer an object that represents the current url in various ways
	//TODO: this fails if the request values contain / characters
	uri.dir = location.href.substring(0, location.href.lastIndexOf('\/'));
	uri.dom = uri.dir; if (uri.dom.substr(0,7) == 'http:\/\/') uri.dom = uri.dom.substr(7);
	uri.path = ''; var pos = uri.dom.indexOf('\/'); if (pos > -1) {uri.path = uri.dom.substr(pos+1); uri.dom = uri.dom.substr(0,pos);}
	uri.page = location.href.substring(uri.dir.length+1, location.href.length+1);
	pos = uri.page.indexOf('?');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
	pos = uri.page.indexOf('#');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
	uri.ext = ''; pos = uri.page.indexOf('.');if (pos > -1) {uri.ext =uri.page.substring(pos+1); uri.page = uri.page.substr(0,pos);}
	uri.file = uri.page;
	if (uri.ext != '') uri.file += '.' + uri.ext;
	if (uri.file == '') uri.page = 'index';
	uri.args = location.search.substr(1).split("?");
	if(uri.args.toString() == '') {
		uri.fileWithArgs = uri.file
	} else {
		uri.fileWithArgs = uri.file+'?'+uri.args;
	}
	return uri;
}


