// JavaScript Document
$(document).ready(function () {
	// Hide all of the sub-menus
	$("ul[title='navigation'] li ul").hide();
	
	// Apply the heading styling
	$("ul[title='navigation'] li:has(ul)").attr("class", "nav-heading");
	$("ul[title='navigation'] li:has(ul) a").attr("class", "unexpanded");
	// Apply the normal styling
	$("ul[title='navigation'] li:not(:has(ul))").attr("class", "nav-normal");
	
	// Set the heading expansion behavior
	$("ul[title='navigation'] li:has(ul)").click(function() { 
		// Expand the clicked list.
		$(this).children("ul").slideDown("slow");
		$(this).children("a").attr("class", "expanded");
		
		// Get a list of all previously expanded lists at the same level.
		var idx = $(this).parent().children().index(this);
		var query = "li:not(:eq(" + idx + ")):has(ul)";
		var prevExpList = $(this).parent().children(query);
		// Hide them and their children.
		for (var i = 0; i < prevExpList.length; i++) {
			$("li:eq(" + $("li").index(prevExpList[i]) + ") ul").slideUp("slow");
			$("li:eq(" + $("li").index(prevExpList[i]) + ") a").attr("class", "unexpanded");
		}
	});
	
	// Scan the navigation to see if there is a submenu which should be
	// opened for this page.
	var modPath = window.location.pathname;
	if (modPath[modPath.length - 1] == '/')
		modPath += "index.shtml";
	var results = $("ul[title='navigation'] li a[href$='" + modPath + "']");
	if (results.length > 0) {
		var curParent = results.parent().parent();
		// Iterate through it's parents making them visible.
		while (curParent.attr('title') != 'navigation' || curParent[0].tagName != "UL"){ 
			curParent.show();
			// If the current parent is a list which should be expanded change its style.
			if (curParent.attr('class') == 'nav-heading')
				curParent.children("a").attr("class", "expanded");
			// Go to the next parent up.
			curParent = curParent.parent();
		}
		// Make its children visible and expand this link
		results.attr("class", "expanded");
		results.parent().children("ul").show();
	}
});
