
function setPage()
{
	var href;
	
	if(document.location.href) href = document.location.href;
	else href = document.location;

	var e = document.getElementById("nav");
	if(! e) return;
	
	var currentPage = getAbsoluteURL(href);
	var links = e.getElementsByTagName("a");
	for(var i = 0; i < links.length; i++)
	{
		var link = links[i];
		
		// skip external URLs
		if((link.href.indexOf(":") != -1) && (getHostname(link.href) != getHostname(href))) continue;
		
		if(compareURLs(getAbsoluteURL(link.href), currentPage))
		{
			link.className = "current";
			link.parentNode.className = "current";
		}
	}
}

/**
 * Compare 2 URLs and see if they are equivalent - accounts for index pages not always being included in the URL
 */
function compareURLs(url1, url2)
{
	if(url1 == url2) return true;
	
	// index pages must be in lower case
	var indexPages = [
		'index.php',
		'Default.htm',
		'index.html',
		'default.asp',
		'default.htm',
		'default.aspx'
	];
	
	// trim any trailing slashes
	if(url1.substring(url1.length - 1, url1.length) == "/") url1 = url1.substring(0, url1.length - 1);
	if(url2.substring(url2.length - 1, url2.length) == "/") url2 = url2.substring(0, url2.length - 1);
	
	for(var i = 0; i < indexPages.length; i++)
	{
		var page = indexPages[i];
		
		var pos = url1.length - (page.length + 1);
		if((url1.substring(pos, url1.length).toLowerCase() == "/" + page) && (url2 == url1.substring(0, pos))) return true;
		pos = url2.length - (page.length + 1);
		if((url2.substring(pos, url2.length).toLowerCase() == "/" + page) && (url1 == url2.substring(0, pos))) return true;
	}
	
	return false;
}

/**
 * Extract the hostname from an absolute URL
 */
function getHostname(url)
{
	var part = removeProtocol(url);
	return part.substring(0, part.indexOf("/"));
}

/**
 * Convert a URL into an absolute URL
 * @param {string} url The URL to convert - if it is already an absolute URL it will be passed through unchanged.
 * @return {string} The absolute URL
 */
function getAbsoluteURL(url)
{
	// remove any leading protocol and host information
	url = removeHostPart(url);

	// is it already an absolute URL?
	if(url.substring(0, 1) == "/") return url;
	
	// work out where we are now
	var currentURL;
	if(document.location.href) currentURL = document.location.href;
	else currentURL = document.location;
	currentURL = removeHostPart(currentURL);
	// work out current directory
	var currentDir = currentURL.substring(0, currentURL.lastIndexOf("/"));

	// combine the relative URL with the current directory
	var combined = currentDir + "/" + url;

	// normalise the URL
	return normaliseURL(combined);
}

/**
 * Remove the host part of the URL - the protocol and hostname
 */
function removeHostPart(url)
{
	// does it have a host part?
	if(url.indexOf(":") == -1) return url;
	
	var newURL = removeProtocol(url);
	// remove hostname
	newURL = newURL.substring(newURL.indexOf("/"), newURL.length);
	
	return newURL;
}

/**
 * Remove the protocol part of a URL but leave the hostname
 */
function removeProtocol(url)
{
	// does it have a host part?
	if(url.indexOf(":") == -1) return url;
	
	// remove protocol and all leading slashes
	var newURL = url.substring(url.indexOf(":") + 1, url.length);
	while(newURL.substring(0, 1) == "/") newURL = newURL.substring(1, newURL.length);

	return newURL;
}

/**
 * Normalise a URL - remove any ".." sections to create a properly-formed URL
 */
function normaliseURL(url)
{
	if(url.indexOf("../") == -1) return url;
	
	var inParts = url.split("/");
	var outParts = [];
	for(var i = 0; i < inParts.length; i++)
	{
		if((inParts[i] == "..") && (outParts.length > 0)) outParts.pop();
		else if(inParts[i] == ".") continue;
		else outParts.push(inParts[i]);
	}
	
	return outParts.join("/");
}
