//Notes 5/22/08 J.U.: Added "toLowerCase" statement  on Line 43 to fix quirks in Operations and Case Services Policy Manuals. Many of their links were capitalized and didn't match with menuATags.href which converts everything to small case.  This fixed the "parentnode.parentnode = null errors I was getting on line 114.

//JavaScript Document

//Preload Images so they don't blink
image1 = new Image(9,9)
image1.src = "/graphics/ca/menuMinus.gif"

image2 = new Image(9,9)
image2.src = "/graphics/ca/menuPlus.gif"

var thisCookie = "";
var linkOn = "";//stores value of link in menu that corresponds to open page

//window.onload = function() {}	
window.onload = function() {setBodyScrollHeight(600);setNavHeight();}
window.onresize = function() {setNavHeight()}
function applyMenuStatus()

	{
	var menuATags = document.getElementById("menu").getElementsByTagName("A");
  var thisURL = document.location.href;
	//finds LI tags with className tOff, assigns className from cookie 
	for(y=0;y<menuATags.length;y++)
		{
		if(menuATags[y].className == "toggleLink")
			{
			menuATags[y].id = "link" + [y];//Sets id for toggleLink A tags
			initializeMenu(menuATags[y].id);//attaches initializeMenu function (below) for onclick event 
			}
		else if(menuATags[y].className == "normal") 
			{
			menuATags[y].onclick = changeColor //attaches onclick event to changeColor function - finds link corresponding to open page and changes color
			}
		if(menuATags[y].parentNode.getElementsByTagName('ul').length > 0)//if parent li contains UL tags
			{
			menuATags[y].parentNode.style.paddingLeft = "0px";
			}
		//Finds link whose url is = to current page and sets className so it can show up as the "on" link

		if(menuATags[y].href == thisURL)
			{
			thisURL = thisURL.toLowerCase()
			menuATags[y].style.color = "red";
			linkOn = menuATags[y];//saves value for later, after ul tags have gotten classNames
			}
		}
		//var re = /\bhttp/
		//if(re.test(linkOn.href))
			//{
			unfold(linkOn)
			//}
	}

function changeColor()
	{
	var menuATags = document.getElementById("menu").getElementsByTagName("A");
	for(x=0;x<menuATags.length;x++)
		{
			menuATags[x].style.color="blue";
		}	
	this.style.color = "red";
	getScrollTop(thisCookie);
	setTimeout("setBodyScrollHeight()",10);//needs setTimeout in order to set scrollTop. Don't remove.
	}	
	
function setBodyScrollHeight(time)/*Sets body scroll height to adjust for fixed header*/
            {
            if(document.maxHeight)//document.documentElementwindow.innerHeight
                        {
                        setTimeout("document.documentElement.scrollTop=document.documentElement.scrollTop-190",time);
                        }
            else
                       {
                       setTimeout("document.body.scrollTop=document.body.scrollTop-190",time);
                       }
		
            }           



//Writes plus and minus graphics into toggleLink anchor tags when link clicked, opens or closes corresponding UL tag.
function initializeMenu(toggleLinkId) 
	{
   var toggleLink = document.getElementById(toggleLinkId);
	toggleLink.onclick = function() 
		{
      ulElement = this.parentNode.getElementsByTagName('ul')[0];
		var ulDisplay = ulElement.className;
      this.innerHTML = (ulDisplay == "open") ? "<img src=\"\/graphics\/ca\/menuPlus.gif\">" : "<img src=\"\/graphics\/ca\/menuMinus.gif\">";
		ulElement.className = (ulElement.className == "open") ? "closed" : "open";
		return false;
    	}
	}

//Writes plus and minus graphics into toggleLink anchor tags onload	
function initializeMenuNow() 
	{
	var menuATags = document.getElementById("menu").getElementsByTagName("A");
	
	for(x=0;x<menuATags.length;x++)
		{
      if(menuATags[x].parentNode.getElementsByTagName('ul').length > 0 && menuATags[x].className == "toggleLink")
			{
			ulElement = menuATags[x].parentNode.getElementsByTagName('ul')[0];
			var ulDisplay = ulElement.className;
      	menuATags[x].innerHTML = (ulDisplay == "open") ? "<img src=\"\/graphics\/ca\/menuMinus.gif\">" : "<img src=\"\/graphics\/ca\/menuPlus.gif\">";
			}
		}
	}	
//Unfolds menu to reveal the link in menu that corresponds to open page
function unfold(thelink)
	{
	var menuULTags = document.getElementById("menu").getElementsByTagName("UL");//gets UL tags in menu div

	linkOn = thelink; //passes the link that is "on" to var linkOn

	if(linkOn.parentNode.parentNode.id!= "menu" )//screens out 1st level menu items
		{
		if (document.all)//for IE and other browsers that support document.all. 
			{
			for (q=0;q<menuULTags.length;q++)
				{
				//Uses contains() which is proprietary to Internet Explorer
				if(menuULTags[q].contains(linkOn))
					{
					menuULTags[q].className = "open"
					}
				}		
			}
		else//for Firefox
			{
				for (q=0;q<menuULTags.length;q++)
				{
				//See http://www.quirksmode.org/blog/archives/2006/01/contains_for_mo.html for an explanation of compareDocumentPosition.It's a way to mimic contains() in Mozilla
				if(menuULTags[q].compareDocumentPosition(linkOn) & 16)
					{
					menuULTags[q].className = "open"
					}
				}
			}
		}

	}
//Sets Everything up. This function is triggered from a function call on the page.
function initMenu(cookieName)
	{
	thisCookie = cookieName;
	applyMenuStatus()
	initializeMenuNow()
	setTimeout("setScrollTop('"+cookieName+"')",10);//needs setTimeout in order to set scrollTop. Don't remove.
	}
//Sets Nav height (menu ul) according to window height
function setNavHeight()
	{
	if(window.innerHeight)
		{document.getElementById('menu').style.height = window.innerHeight - 190 + "px";}
	else
		{
		document.getElementById('menu').style.height = document.body.clientHeight-190;	
		}
	}
//Retrieves scrollTopValue (scroll position) of menu and stores it in the cookie
function getScrollTop(cookieName)
	{
	//Gets scrollTop value of menu div
	scrollSet = document.getElementById('menu').scrollTop;

	//Puts value of scrollTop into cookie
	createCookie(thisCookie,scrollSet,0) 
	}
//Sets scroll position from stored cookie value
function setScrollTop(cookieName)
	{
	//gets scrollTop Value from cookie that was set by getScrollTop()
	document.getElementById('menu').scrollTop = readCookie(cookieName);	

	}	