// TOP MENUS
// Number of menus in main menu system
//   (change this number if menus are added or removed)
var numMenus = 8;

// Static identifier for current menu
// Used to prevent jQuery toggling of menu when moving from menu items to heading
var currentMenu = -1;


// Loop through menus, displaying submenu 
// for the selected main menu, hiding all others
function ShowMenu(which) {
	for (var i = 0; i <= numMenus; i++) {
		if (i != which) document.getElementById('header-menuMain-dropMenu' + i).style.display = 'none';
		document.getElementById('header-menuMain' + i).style.backgroundPosition = ((i == which) ? '0 -36px' : '0 0px');

		if (i == which && currentMenu != which) {
			$('#header-menuMain-dropMenu' + which).slideToggle(200);
			currentMenu = which;
		}
		else if (i == which && currentMenu == which) 
			document.getElementById('header-menuMain-dropMenu' + i).style.display = 'block'
	}
}

// Loop through menus, hiding all
function HideMenus() {
	ShowMenu(-1);
}


// LEFT MENUS
// Function to show and hide left-navigation menus
function ShowHideLeftNav(object) {
	var imageSrc = document.getElementById('img-' + object).getAttribute('src');
	imageSrc = imageSrc.replace('collapsed.gif', '').replace('expanded.gif', '');

	if (document.getElementById('menu-' + object).style.display == 'block') {
		document.getElementById('img-' + object).setAttribute('src', imageSrc + 'collapsed.gif');
		document.getElementById('img-' + object).setAttribute('alt', 'Expand Menu');
		//document.getElementById('menu-' + object).style.display = 'none';
		$('#menu-' + object).slideToggle(200);
		StoreNavCookie(object, "collapse");
	}

	else {
		document.getElementById('img-' + object).setAttribute('src', imageSrc + 'expanded.gif');
		document.getElementById('img-' + object).setAttribute('alt', 'Collapse Menu');
		//document.getElementById('menu-' + object).style.display = 'block';
		$('#menu-' + object).slideToggle(200);
		StoreNavCookie(object, "expand");
	}
}

// Set the cookie subkey value
// To store subkey values in a parent cookie, the cookie must first be disassembled 
// to identify if the subkey already exists, updating/appending where necessary.
function StoreNavCookie(subkey, value) {
	var cookieCollection = document.cookie;
	var cookieContainer = "";

	// Look for navigation cookie by first checking if the keyword occurs after 
	// an already stored cookie.  If not, see if it's the first cookie.  This is done to 
	// prevent the off chance that another cookie would exist ending with our cookie name.
	var cookieContainerStart = cookieCollection.indexOf("; drakeLawNav=") + 14;
	if (cookieContainerStart = -1) cookieContainerStart = cookieCollection.indexOf("drakeLawNav=") + 12;

	// Parse cookie container from entire collection
	if (cookieContainerStart >= 0) {
		var cookieContainerEnd = cookieCollection.indexOf(";", cookieContainerStart);
		if (cookieContainerEnd = -1) cookieContainerEnd = cookieCollection.length;
		cookieContainer = cookieCollection.substring(cookieContainerStart, cookieContainerEnd);
	}

	var currentSubkeyValue = "";
	var cookieKeys = cookieContainer.split("&");

	// Enumerate the cookie container to see if the passed subkey already existed
	for (i = 0; i < cookieKeys.length; i++) {
		if (cookieKeys[i].split("=")[0] == subkey) {
			currentSubkeyValue = cookieKeys[i].split("=")[1];
			break;
		}
	}

	// If a subkey already existed, update its value; otherwise append a new subkey
	if (currentSubkeyValue != "") 
		cookieContainer = cookieContainer.replace(subkey + "=" + currentSubkeyValue, subkey + "=" + value);
	else cookieContainer = cookieContainer + ((cookieContainer.length > 0) ? "&" : "") + subkey + "=" + value;

	// Store the new cookie
	document.cookie = "drakeLawNav=" + cookieContainer + "; path=;";
}
