/*

////////////////////////////////////////////////////
////  Javascript menu system for WMU ISM site  ////
//////////////////////////////////////////////////

////  Variables  ////
MenuOmissions:Array
MenuStatus:Array
MenuImage:Array
MenuMain:Array
MenuSub:Array
image_plus_source:String
image_sub_source:String
menu_cookie_name:String


////  Methods  ////
getCookie:String																(name:string)
MenuSetup:Void																	()
MenuToggle:Void																	(id:number, value:number, doNotSaveCookie:number)
setCookie:Void																	(name:string, value:string)
StatusSetup:Void																()


////  How it works  ////
The function 'MenuSetup' must be called upon document load. The method
looks for an HTML element with 'Menucontainer' assigned as the id. The 
setup function runs through all of the <li> tags contained in the Menucontainer.
Based on the className of the <li> tag, the element is inserted either into the
MenuMain or MenuSub array. If the item is part of the main menu, an <img> tag
is dynamically created and inserted before the <a> menu link. An onClick event
is assigned to the image to expand/contract the menu using the MenuToggle method.
MenuStatus is an array variable that contains the value fetched from the menu
cookie. MenuStatus is refreshed every page load, and if a cookie cannot be found, 
a new one is created.

The variables image_plus_source and image_sub_source contain string values telling
JavaScript where to find the expand/collapse images. menu_cookie_name contains a 
string reference to the name of the cookie used to store the state of the menu
between page loads.

The cookie stored expires at the end of the session.

The status of the main menus is kept in MenuStatus, where a "1" value indicates that
sub menu items are showm and "0" indicates they are hidden. MenuOmissions keeps
boolean values for special cases where a menu's status should not be included into
the cookie when saved. This special case is if a student visits a main menu link
the sub menu items will be shown automatically (without requiring the user to ma-
nually expand the list of sub menus). This case means that menu expansion was not a
product of user interaction and the status of the menu should not be kept on the next
page load.

*/

////  Global variable definitions
var MenuOmissions = new Array();
var MenuStatus = new Array();
var MenuImage = new Array();
var MenuMain = new Array();
var MenuSub = new Array();
var image_plus_source = "/images/box_plus.gif";
var image_sub_source = "/images/box_minus.gif";
var image_gray_source = "/images/box_plus_fade.gif";
var menu_cookie_name = "wmu_ism_menu";











////  Function to get a cookie value from the name
function getCookie (cookiename)  //  String
{
	//  Check if the name is undefined
	if (!cookiename)
	{
		//  Return a blank string
		return "";
		
	};
	
	
	//  Get the cookie string
	var values = document.cookie;
	
	
	//  Split the string at the semicolons
	var pairs = values.split(";");
	
	
	//  Loop through all of the items
	for (var item = 0; item < pairs.length; item++)
	{
		//  Split the string value at the equals sign
		var cvalues = pairs[item].split("=");
				cvalues[0] = (cvalues[0].substr(0, 1) == " ") ? cvalues[0].substr(1) : cvalues[0];
		
		
		//  Check if the value is the one being sought
		if (cvalues[0] == cookiename)
		{
			//  Return the value part
			return cvalues[1];
			
		};
		
	};
	
	
	//  Return a blank string
	return "";
	
};











////  Function to set up the menu initially
function MenuSetup ()  //  Void
{
	//  Store the main menu container into a variable
	var container = document.getElementById("Menucontainer");
	
	
	//  Check for the existence of the menu div
	if (!container)
	{
		//  This should never happen
		//  Display a message to let the person working on the site know that something is not configured
		alert("The menu could not be referenced. Please make sure that the menu container exists on the page.");
		
	};	
	
	
	//  Set an increment for use in keeping track of the main menus
	//  Since the sub menus only appear after the main menu items and
	//  are not contained in a div or span, the counter is incremented
	//  when a main menu is encountered
	var menucount = -1;
	
	
	//  Get the current locale -- the file being viewed
	var locale = location.pathname+location.search;
	
	
	//  Get all of the li tags within the container
	var links = container.getElementsByTagName("li");
	
	
	//  Loop theough the list elements
	for (var i = 0; i < links.length; i++)
	{
		//  Get the class name and the link from the list entry
		var tClass = links[i].className;
		
		
		//  Check against the classname in a switch
		switch (tClass)
		{
			
			
			case "main":
			//  Increment the menu counter
			menucount++;
			
			
			//  Get the image source
			//  CHeck if the next item int he iteration is a main menu or null
			if (
					(
						links[i + 1] &&
						links[i + 1].className &&
						links[i + 1].className == "main"
					)
					 ||
					(
						!links[i + i] &&
						links[i].className == "main"
					)
				 )
			{
				//  Set the source to be grayed out
				var the_image_source = image_gray_source;
				
			} else
			{
				//  Set the source to be the plus symbol
				var the_image_source = image_plus_source;
				
			};
			
			
			//  Create an image object to insert before the link in the li entry
			var image = document.createElement("img");
					image.setAttribute("src", the_image_source);
					image.setAttribute("id", "menu_image_number_"+menucount);
					
					
					//  Check if the IE attribute must be used
					if (navigator.userAgent.indexOf("MSIE") != -1)
					{
						//  Use IE's system
						image.onclick = function ()
						{
							//  Parse the integer out of the element's id
							var the_id = this.id.split("_");
									the_id = the_id[the_id.length - 1];
							
							
							//  Call the toggle function
							MenuToggle(the_id);
							
						};
						
					} else
					{
						//  Apply the attribute
						image.setAttribute("onclick", "javascript:MenuToggle("+menucount+");");
						
					};
					
			//  Get a reference to the A element in the li entry
			//  There should only be one, so take the [0] offset
			//  in the array returned
			var link = links[i].getElementsByTagName("a")[0];
			
			
			//  Check if the link exists to avoid an error
			if (link)
			{
				//  Apply the image before the link
				links[i].insertBefore(image, link);
				
				
				//  Save the image entry into the array
				MenuImage.push(image);
				
			};
			
			
			//  Save the menu item into the menu array
			MenuMain.push(links[i]);
			break;
			
			
			
			
			
			case "sub":
			//  Check if the offset at the array exists
			if (!MenuSub[menucount])
			{
				//  Create a new array at the count
				MenuSub[menucount] = new Array();
				
			};
			
			
			//  Insert the menu entry into the array
			MenuSub[menucount].push(links[i]);
			break;
			
			
			
			
			
			default:
			//  Hide the element from view
			links[i].style.display = "none";
			break;
			
			
		};
		
	};
	
	
	//  Call the status setup function
	StatusSetup();
	
	
	//  Get the document name and concatenate it with the query string
	var locale = location.pathname+location.search;
	
	
	//  Get all of the list item entries in the menu container
	var elements_a = container.getElementsByTagName("li");
	
	
	//  Create a local counter variable to keep track of which main menu the loop is currently on
	var main_menu_count = -1;
	
	
	//  Loop through each element
	for (var a = 0; a < elements_a.length; a++)
	{
		//  Check if the class name warrants an update on the menu counter variable
		if (elements_a[a] && elements_a[a].className && elements_a[a].className == "main")
		{
			//  Increment the menu counter
			main_menu_count += 1;
			
		};
		
		
		//  Get the link in the LI entry
		var the_link = elements_a[a].getElementsByTagName("a");
				the_link = the_link[0];
		
		
		//  Get the href of the element
		var alink = the_link.getAttribute("href");
		
		
		//  Get the last entity of the array after splitting the alink at '/'
		var alocale = locale.split("/");
				alocale = (alocale.length > 0) ? alocale[alocale.length - 1] : "";
				
				
		//  Get the last element in the link
		var alinks = alink.split("/");
				alinks = (alinks.length > 0) ? alinks[alinks.length - 1] : "";
				
				
		//  Check if the locale is empty
		alocale = (alocale == "") ? "index.html" : alocale;
		
		
		//  Force both to be lowercase
		alocale = alocale.toLowerCase();
		alinks = alinks.toLowerCase();
		
		
		//  Check if the links are similar
		if (alocale == alinks)
		{
			//  Check if the entry has any sub-items
			if (elements_a[a] && elements_a[a].className && elements_a[a].className == "main" && elements_a[a + 1] && elements_a[a + 1].className == "sub")
			{
				///  There is a sub menu entry after this one
				//  Toggle the main menu
				MenuToggle(main_menu_count, 1, 1);
				
			};
			
			
			//  Check if the menu item is a sub menu
			//  This case would warrant the retrieval of the parent id for toggling
			if (elements_a[a] && elements_a[a].className && elements_a[a].className == "sub")
			{
				///  The active item is a sub menu and requires the main menu to be visible
				//  Toggle the main menu
				MenuToggle(main_menu_count, 1, 1);
				
			};
			
			
			//  Change the class name of the link
			elements_a[a].className += " active";
			
		};
		
	};
	
};











////  Function to toggle the menu object
function MenuToggle (id, value, doNotSaveCookie)  //  Void
{
	//  Get the menu element
	var element = MenuMain[id];
	
	
	//  Check if the element exists
	//  Also check if the element's source is the gray version
	if (!element || !MenuSub[id] || MenuSub[id].length <= 0)
	{
		//  Return from the function
		return;
		
	};
	
	
	//  Determine the value and if it was passed
	value = (value != undefined) ? value : ((MenuStatus[id] == "0") ? 1 : 0);
	
	
	//  Change the source of the image
	MenuImage[id].src = (value == "1") ? image_sub_source : image_plus_source;
	
	
	//  Change the status at ID to value
	MenuStatus[id] = value.toString();
	
	
	//  Check if the script is to not save the cookie
	if (!doNotSaveCookie)
	{
		//  Create a temp array
		var MenuStatusTemp = new Array();
		
		
		//  Remove the id from the omissions array
		MenuOmissions[id] = null;
		
		
		//  Loop through the status options
		for (var temp_loop = 0; temp_loop < MenuStatus.length; temp_loop++)
		{
			//  Check if the id is in the omissions table
			if (MenuOmissions[temp_loop] == true)
			{
				//  Set a 0 for this increment
				MenuStatusTemp[temp_loop] = "0";
				
			} else
			{
				//  Add the menu status setting
				MenuStatusTemp[temp_loop] = MenuStatus[temp_loop];
				
			};
			
		};
		
		
		//  Save the cookie
		setCookie(menu_cookie_name, MenuStatusTemp.join("|"));
		
	} else
	{
		// Include the element into the omissions array
		MenuOmissions[id] = true;
		
	}
	
	
	//  Get the display value
	var displayValue = (value == 1) ? "block" : "none";
	
	
	//  Loop through all the sub menu elements
	for (var i = 0; i < MenuSub[id].length; i++)
	{
		//  Update the display value
		MenuSub[id][i].style.display = displayValue;
		
	};
	
};











////  Function to set a cookie value
function setCookie (name, value)  //  Void
{
	//  Set the string
	document.cookie = name+"="+value+"; path=/";
	
};











////  Function to set up the status array
////  This is just to limit the menu function scope
////  since this function interacts and sets a cookie...
function StatusSetup ()  //  Void
{
	//  Get the cookie value
	var saved = getCookie(menu_cookie_name);
	
	
	//  Check if the cookie existed
	if (saved)
	{
		//  Split the cookie string at the pipes
		MenuStatus = saved.split("|");
		
	} else
	{
		//  Create an empty array
		MenuStatus = new Array();
		
	};
	
	
	//  Check if the status array differs from the menu array length
	if (MenuStatus.length != MenuMain.length)
	{
		//  Just to be safe, re-establish the blank array
		MenuStatus = new Array();
		
		
		//  Loop through the menu array
		for (var i = 0; i < MenuMain.length; i++)
		{
			//  Add a 0 value to the array
			MenuStatus[i] = "0";
			
		};
		
		
		//  Establish the cookie from the initial array
		setCookie(menu_cookie_name, MenuStatus.join("|"));
		
	};
	
	
	//  Loop through the status array
	for (var i = 0; i < MenuStatus.length; i++)
	{
		//  Call the toggle function
		MenuToggle(i, MenuStatus[i]);
		
	};
	
};
