// left_nav.js
// This script assumes you have included basic.js, which has 
// get_cookie and set_cookie functions.  It also assumes that 
// the container div is named "siteNav", and that each html page
// has the js variables "page" and "section" defined in the 
// header and calls the initMenu(section, page) function in the 
// body declaration (onload).
// This is written for browsers 5 and up.

var SUBMENU_COUNT = 7;

// Initialize cookie / nav states array
var navStates = new Array();
if (get_cookie("navMenus")) {
	navStates = get_cookie("navMenus").split(",");
	}
else {
	for(i=0;i<SUBMENU_COUNT;i++){
		navStates[i] = "off";
	}
}

// Initialize actual submenu visibility
function initMenu(section, page){
	if(document.getElementById){
	
		// hide appropriate submenus
		for(i=0;i<SUBMENU_COUNT;i++){
			if (navStates[i] == "off") {
  				switchDisplay('subMenu'+i, 'topMenu'+i);	
			}
			// we didn't set an href on parent in html in the event that the browser didn't 
			// understand js or getElementById, in which case the menus will all be expanded and 
			// parents not clickable; so, now that we KNOW that this browser understands js, 
			// let's set the parent link to a link so people will know they can click it
			linkParent('topMenu' + i); 
		}
	
		// for bookmarks, it's possible that no cookies are set, 
		// but we want to make sure that at least the submenu for this
		// current page is expanded and highlighted properly
		if ((page) && (section)){
			p=document.getElementById(page);
			// we have to go up two parent levels, one for the unnamed
			// list element (<li>), and another for the list itself (<ul>)
			// to display this submenu  (parentNode is supported in all
			// browsers that support getElementById)
			p.parentNode.parentNode.style.display="block";
	
			// colorize this page and section
			selectSub(page, section);
		}
	
		// once initialized, now show entire menu div (this is so that 
		// the browser doesn't show every menu open first then collapsed 
		// second)
		d=document.getElementById('siteNav');
		d.style.display="block"
	}
	
}

// Create an href on parent links;  this function assumes document.getElementById, 
// and therefore should only be called in initialization
function linkParent(parentLnk){
	p = document.getElementById(parentLnk);
	p.href="#";
}


// Show or hide the submenu
function switchDisplay(subMenuID, parentLnk){
	if(document.getElementById){
	
		// get index to set cookie
		i = subMenuID.replace("subMenu", "");
	
		// show submenu 
		s=document.getElementById(subMenuID);
		s.style.display=(s.style.display!="none")?"none":"block";
			
		// colorize parent link if we're not just initializing, 
		// and set cookie
		if (parentLnk) {
			p = document.getElementById(parentLnk);
			if (s.style.display == "none") {
				p.className="closed";
				navStates[i] = "off";
			}
			else {
				p.className="open";
				navStates[i] = "on";
			}
			set_cookie("navMenus", navStates.toString(), null, "/");
		}
	}
}



// Change hrefs and text colors
function selectSub(thisLnk, parentLnk) {
	if(document.getElementById){
	
			//  remove the actual href link on this element
			l=document.getElementById(thisLnk);
			l.removeAttribute("href");
			// IE 5 Mac workaround: does not support removeAttribute nor assign to l.href 
			// properly, so the sub highlight is never the right color without it
			l.className = "active";

			// remove the actual href link on the parent link 
			// and colorize it
			p=document.getElementById(parentLnk);
			p.removeAttribute("href");
			p.onclick = null;
			p.className = "selected";
			
			// we won't reset the last highlighted href back because, 
			// presumably, you are loading a new page with
			// each href and therefore it is being reset 
			// upon the page load / init menu		
	}
}
