// Change the following variable to match the name of the index file for directories.
var indexName = "index.html";

// The following variable should be the appropriate entity character for the chevron(s)
// appearing next to the current item.
var arrowText = "&raquo;";

// This is the main function that finds the current URL and expands the tree.
function expand() {
	if (!document.getElementById) return;
	// Look for the side nav list
	var nav = document.getElementById("sideNav");
	// If there is no side nav, or no list in it, don't try to expand nothing!
	if (!nav) return;
	var link = getLinkFromURL(nav, window.location);
	if (link) {
		// link found -- highlight the link, then expand the tree to expose it.
		var linkParent = link.parentNode;
		highlightLink(link, linkParent);
		expandTree(linkParent);
	}
}
 
// Given a list and a URL, return the link within the list that matches the URL
function getLinkFromURL(nav, url) {
	var host = url.hostname;
	var url = url.pathname;
	var links = nav.getElementsByTagName("A");
	// Loop through all anchors until a match is found
	for (var i = 0; i < links.length; i++) {
		var link = links.item(i);
		var href = link.getAttribute("href");
		var a = href.indexOf("?");
		if (a > 0) {
			href = href.substring(0, a);
		}
		// If the link has a host name, don't parse if different; otherwise, strip it
		a = href.indexOf("//");
		if (a != -1) {
			var b = href.indexOf("/", a + 3);
			var linkHost = href;
			if (b > 0) {
				linkHost = linkHost.substring(0, b);
				// remove host from link, if required
				href = href.substring(b, href.length);
			}
			b = href.indexOf(",");
			if (b > 0){
				href = href.substring(0, b);
			}
			var linkHost = linkHost.substring(a + 2, linkHost.length);
			b = linkHost.indexOf(":");
			if (b >= 0) {
				linkHost = linkHost.substring(0, b);
			}
			if (!linkHost.indexOf("capgroup.com")) {
				if (linkHost != host) continue;
			}
		}
		if (href == url) return link;
		// If the link is a directory (no file extensions), check for variations
		if (href.lastIndexOf(".") < href.lastIndexOf("/")) {
			var testRef = href;
			//	If the link doesn't have a trailing slash, add it and check again
			if (testRef.lastIndexOf("/") != testRef.length - 1) {
				testRef += "/";
				if (testRef == url) return link;
			}
			// Finally, check to see if the default file is explicitly called out in the URL
			testRef += indexName;
			if (testRef == url) return link;
		}
		// Do the same checks, but check the URL for variations instead of the link
		if (url.lastIndexOf(".") < url.lastIndexOf("/")) {
			var testUrl = url;
			if (testUrl.lastIndexOf("/") != testUrl.length - 1) {
				testUrl += "/";
				if (testUrl == href) return link;
			}
			testUrl += indexName;
			if (testUrl == href) return link;
		}
	}
	// no match was found, return null
	return null;
}

// This function changes the link from
//
// <li><a href="blah">Test</a></li>
//
// to
//
// <li><strong><a href="blah">Test</a><span>arrowText</span></strong></li>
function highlightLink(link, linkParent) {
	var strongTag = document.createElement("STRONG");
	linkParent.replaceChild(strongTag, link);
	strongTag.appendChild(link);
	var spanTag = document.createElement("SPAN");
	strongTag.appendChild(spanTag);
	spanTag.innerHTML = arrowText;
}

// This function starts at the given LI and changes the class of the A to "open"
// (if it was closed) and the LI class to "open". This changes the background
// image (on the link) and expands the list. It works all the way up the tree,
// expanding parent list items, so that the entire tree is visible along the
// selected branch.
function expandTree(listItem) {
	listItem.className = "open";
	var listLink = listItem.getElementsByTagName("A");
	if (listLink.length) {
		listLink = listLink.item(0);
		if (listLink.className == "close")
			listLink.className = "open";
	}
	listItem = listItem.parentNode;
	listItem = listItem.parentNode;
	if (listItem.tagName == "LI") expandTree(listItem);
}