// Script to trigger collapsible menu behavior in
// standards-based XHTML.
// CWS Inc. 10-2005 - lyle [at] cws [dot] net

window.onload = function() {

	// The navigation element
	var navigation = document.getElementById("navigation");
	
	var page_match = false;
	var index_match = false;
	var listItems = navigation.getElementsByTagName("a");
	
	for (var i=0; i<listItems.length; i++) {
		var listItem = listItems[i];
		
		// Check the link href against page location to determine the "active" section
		if (listItem.href == window.location.href) {
			page_match = true;
			var node = listItem.parentNode;
			node.className = "activeItem";
			while (node.nodeName.toLowerCase() == "ul" || node.nodeName.toLowerCase() == "li" || node.className == "menuGroup" || node.nodeName.toLowerCase() == "h2") {
				node = node.parentNode;
				node.className = "menuGroupExpanded";
			}
		}
	}
	
	// Default to highlighting category index if no match found
	if(!page_match) {
		for (var i=0; i<listItems.length; i++) {
			var listItem = listItems[i];
			var category_index_html = window.location.href.substring(0,window.location.href.lastIndexOf('/'))+'/index.html';
			var category_index_php  = window.location.href.substring(0,window.location.href.lastIndexOf('/'))+'/index.php';

			if (listItem.href == category_index_html || listItem.href == category_index_php) {
				index_match = true;
				var node = listItem.parentNode;
				node.className = "activeItem";
				while (node.nodeName.toLowerCase() == "ul" || node.nodeName.toLowerCase() == "li" || node.className == "menuGroup" || node.nodeName.toLowerCase() == "h2") {
					node = node.parentNode;
					node.className = "menuGroupExpanded";
				}
				break;
			}
		}
	}
	
	// Go up a directory level if we still can't match the page to a nav item
	if(!page_match && !index_match) {
		var base_path = window.location.href;
		var base_match = false;
		var base_level = 0;
		while(!base_match && base_level < 10) {
			var base_path = base_path.substring(0,base_path.lastIndexOf('/'));
			for (var i=0; i<listItems.length; i++) {
				var listItem = listItems[i];
				var category_index_html = base_path.substring(0,base_path.lastIndexOf('/'))+'/index.html';
				var category_index_php  = base_path.substring(0,base_path.lastIndexOf('/'))+'/index.php';
	
				if (listItem.href == category_index_html || listItem.href == category_index_php) {
					base_match = true;
					var node = listItem.parentNode;
					node.className = "activeItem";
					while (node.nodeName.toLowerCase() == "ul" || node.nodeName.toLowerCase() == "li" || node.className == "menuGroup" || node.nodeName.toLowerCase() == "h2") {
						node = node.parentNode;
						node.className = "menuGroupExpanded";
					}
					break;
				}
			}
			if(base_match) { break; }
			base_level++;
		}
	}
	
	// Reposition permission "key" after menu is expanded (fix for IE6)
	var permissions_key = document.getElementById("permissions_key");
	if(permissions_key) {
		permissions_key.style.position = 'absolute';
	}
	
	// Initialize lightbox popup - will overwrite below function when script is included
	initLightbox();
}

function initLightbox() {}