/*
 * menuExpandable2.js - implements an expandable menu based on a HTML list
 * Author: Dave Lindquist (http://www.gazingus.org)
 */

if (!document.getElementById)
    document.getElementById = function() { return null; }

var imagesPath = "/images/";
var plus = "plus.gif";
var minus = "minus.gif";

/* This function was modified by Ben Vandenburg to use cookies to store the state of the menu */
function initializeMenu(menuId, actuatorId) {
    var menu = document.getElementById(menuId);
    var actuator = document.getElementById(actuatorId);

    if (menu == null || actuator == null) return;
	
	
	/* MODIFICATION: */
	if(readCookie(menu.id) != null)
	{
		//if the cookie that sotres the state of the menu exists, then set the state to that
		menu.style.display = readCookie(menu.id);
	}

	//menu.style.display = 'none';
	/* END MOD */
	
	
    actuator.onclick = function() {
        var display = menu.style.display;
        this.style.backgroundImage = (display == "block") ? "url(" + imagesPath + plus + ")" : "url(" + imagesPath + minus + ")";
        menu.style.display = (display == "block") ? "none" : "block";

		createCookie(menuId, menu.style.display, 1); // MODIFICATION
		
        return false;
    }
}
/* 
This function was coppied from above and modified to support a menu block being open by default
and to support the state of the menu being saved by cookies
	author: Ben Vandenburg
	*/
function initializeMenuOpen(menuId, actuatorId) {
    var menu = document.getElementById(menuId);
    var actuator = document.getElementById(actuatorId);

    if (menu == null || actuator == null) return;

	menu.style.display = "block"; //MODIFICATION I dont think I need this
	
    actuator.onclick = function() {
        var display = menu.style.display;
        this.style.backgroundImage = (display == "block") ? "url(" + imagesPath + plus + ")" : "url(" + imagesPath + minus + ")";
        menu.style.display = (display == "block") ? "none" : "block";
		
		createCookie(menuId, menu.style.display, 1); //MODIFICATION

        return false;
    }
}

/* Hides all menus instead of doing a css display:none to comply with section 508, and to work without JS turned on:
	author: Ben Vandenburg
	*/
function hideMenus()
{
	var ulArr = document.getElementsByTagName('ul');
	
	for (var i = 0; i < ulArr.length; i++)
	{
		if(ulArr[i].className.match('menuopen'))
		{
			///if its supposed to be open by default, make it so
			//I dont need to specify the display property to be set to block,
			//because it's like that by default
			//this if statement makes it so that the <code>match('menu')</code>
			//doesn't pick up the menuopen string.
		}
		else
		{
			/*	this if is in the else of the above statement so b/c 
				<code>match</code> matches all instinces of 'menu' even if it's part of
				a bigger string */
			if (ulArr[i].className.match('menu')) 
			{
				/*if(readCookie(ulArr[i].id) != null)
				{
					//if the cookie that sotres the state of the menu exists, then set the state to that
					ulArr[i].style.display = readCookie(ulArr[i].id);
					ulArr[i].style.backgroundImage = (ulArr[i].style.display == "none") ? "url(" + imagesPath + plus + ")" : "url(" + imagesPath + minus + ")";
				}
				else
				{*/
					//otherwise set the display to none
					ulArr[i].style.display = 'none';
				//}
			}
		}
	}
}

/**********************************\
These functions were taken from
http://www.quirksmode.org/js/cookies.html
\**********************************/

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

