var WM_acceptsCookies = false;
if(document.cookie == '') {
    document.cookie = 'WM_acceptsCookies=yes'; 
    if(document.cookie.indexOf('WM_acceptsCookies=yes') != -1) {
		WM_acceptsCookies = true; 
    }
} else {
	WM_acceptsCookies = true;
}

function WM_setCookie (name, value, hours, path, domain, secure) {
    if (WM_acceptsCookies) { 
	if (hours) {
	    if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
			var numHours = hours;
	    } else if (typeof(hours) == 'number') { // calculate Date from number of hours
			var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
	    }
	}
	document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':'');
    }
}

function WM_readCookie(name) {
    if(document.cookie == '') {
		return false; 
    } else {
		var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(name);
		var NN2Hack = firstChar + name.length;
		if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
		    firstChar += name.length + 1; // skip 'name' and '='
		    lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
		    if(lastChar == -1) lastChar = theBigCookie.length;
		    return unescape(theBigCookie.substring(firstChar, lastChar));
		} else { // If there was no cookie of that name, return false.
		    return false;
		}
    }	
}

function WM_killCookie(name, path, domain) {
  var theValue = WM_readCookie(name);
  if(theValue) {
      document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
  }
}
/*
Functions to manage left hand navigation menu.
*/

var listItems = new Array();
var defaultMenuState = "0 1 3"; // space separated list of menus to open by default

function saveMenuState() {
	var menuState = "";
	for (var i=0; i<listItems.length; i++) {
		if ((/\b ?open\b/).test(listItems[i].className) == true) {
			menuState += i + " ";
		}
	}
	WM_setCookie("menuState", menuState, 4, "/");
}

initMenu = function() {
	// build list of proper list items
	var listChildren = document.getElementById("navList").childNodes;
	for (var i=0; i<listChildren.length; i++) {
		if (listChildren[i].childNodes.length > 2) {
			listItems[listItems.length] = listChildren[i];
		}
	}
	
	// load state of menu from cookie
	var menuState = WM_readCookie("menuState");
	if (!menuState) {
		menuState = defaultMenuState;
	}
	
	// initialise menu setting state, and applying interactivity
	for (var i=0; i<listItems.length; i++) {
		if (new RegExp("\\b" + i + "\\b").test(menuState)) {
			listItems[i].className = "open";
		} else {
			listItems[i].className = "closed";
		}
		listItems[i].childNodes[0].onclick = function() {
			parentItem = this.parentNode;
			if (parentItem.className == "open" ) {
				parentItem.className = "closed";
			} else {
				parentItem.className = "open";
			}
			saveMenuState();
			return false;
		}
	}
}

/*
Initialise everything on load. This needs to be improved, but will work for now...
*/

if (window.addEventListener) { 
	window.addEventListener("load", initMenu, false); 

} else if (window.attachEvent) { 

	var r = window.attachEvent("onload", initMenu); 

} else { 
	window.onload = function () { 
		initMenu();
	}
}