// JavaScript Document
var temp, temp2, arrCookie, arrCookie2, intCookie;

var bolSmartMenuCollapseParents = 'false';
var bolSmartMenuCascadeChildren = 'true';
var bolSmartMenuRemember        = 'true';
var strSmartMenuCurrentOpenGen     = '';
var strSmartMenuPlusImage       = '';
var strSmartMenuMinusImage      = '';
var strSmartMenuPageImage       = '';
var arrSmartMenuParents         = new Array();



function SmartMenuInitiate(bolCollapse, bolCascade, bolRemember, strPlus, strMinus, strPage)
{
	if(bolCollapse == "true")
	{
		bolSmartMenuCollapseParents = "true";
	}
	
	if(bolCascade == "false")
	{
		bolSmartMenuCascadeChildren = "false";
	}
	
	if(bolRemember == "false")
	{
		bolSmartMenuRemember = "false";
	}
	
	if(strPlus != "")
	{
		strSmartMenuPlusImage = strPlus;
	}
	
	if(strMinus != "")
	{
		strSmartMenuMinusImage = strMinus;
	}
	
	if(strPage != "")
	{
		strSmartMenuPageImage = strPage;
	}
	
	if(bolSmartMenuRemember == "true")
	{
		intCookie=0;
	
		if(document.cookie)
		{
			arrCookie=document.cookie.split(";");
			arrCookie2=new Array();
			for(i in arrCookie)
			{
				arrCookie2[arrCookie[i].split("=")[0].replace(/ /g,"")]=arrCookie[i].split("=")[1].replace(/ /g,"");
			}
		}

		arrCookie=(document.cookie.indexOf("SmarMenuStateGen=")>=0)?arrCookie2["SmarMenuStateGen"].split(","):new Array();
	}
	
	//strSmartMenuCurrentOpenGen = arrCookie2['SmartMenuCurrentOpenGen'];

	objList = document.getElementById("containerul");

	for(var intElement=0; intElement<objList.getElementsByTagName("li").length; intElement++)
	{
		objElement = objList.getElementsByTagName("li")[intElement];
		
		// Are we a top level li element, if so set that to be the current parent for this branch
		if(objElement.parentNode.id == 'containerul')
		{
			strCurrentParent                   = objElement.id;
			arrSmartMenuParents[objElement.id] = 'containerul';
		}
		else
		{
			// Store our parent for reference later
			arrSmartMenuParents[objElement.id] = strCurrentParent;
		}
		

		if(objElement.getElementsByTagName("ul").length>0)
		{
			// Try and get the top text

			arrFirstElements = objElement.getElementsByTagName("TT");
			arrFirstElements[0].onclick = function()
			{
				SmartMenuShowHide(this.parentNode, "true");
				SmartMenuWriteCookie();
			}


			// Default the image to the plus sign, and revert to minus ONLY if the cookie says the menu is open
			var strImage = "url("+strSmartMenuPlusImage+")";
			if(arrCookie.length >0)
			{
				if(arrCookie[intCookie]=="true")
				{
					strImage = "url("+strSmartMenuMinusImage+")";
				}
			}

			objSpanTag                       = document.createElement("span");
			objSpanTag.className             = "symbols";
			objSpanTag.style.backgroundImage = strImage;

			objSpanTag.onclick=function()
			{
				SmartMenuShowHide(this.parentNode, "true");
				SmartMenuWriteCookie();
			}

			objElement.insertBefore(objSpanTag, objElement.firstChild)

			// Ensure that the first list under this element is 'closed'
			objElement.getElementsByTagName("ul")[0].style.display = "none";


			// If the cookie value for this element is set, toggle my displays
			if(arrCookie[intCookie]=="true")
			{
				SmartMenuShowHide(objElement);
			}
			intCookie++;
		}
		else
		{
			objSpanTag                       = document.createElement("span");
			objSpanTag.className             = "symbols";
			objSpanTag.style.backgroundImage = "url("+strSmartMenuPageImage+")";

			objElement.insertBefore(objSpanTag,objElement.firstChild);
		}
	}
}


function SmartMenuShowHide(objElement)
{
	objListNode = objElement.getElementsByTagName("ul")[0];
	strPrevious = objListNode.style.display;
	
	// Need to open/close other menu option if we are in Collapsable mode
	if(bolSmartMenuCollapseParents == "true")
	{
	
		var strMyID     = objElement.id;
		var strMyParent = arrSmartMenuParents[strMyID];
		
		//if(strMyParent != strCurrentParent)
		//{
			SmartMenuCloseChildren(strMyParent);
		//}
	}


	objSpanTag  = objElement.getElementsByTagName("span")[0];

	if(strPrevious == "block")
	{
		objListNode.style.display        = "none";
		objSpanTag.style.backgroundImage = "url("+strSmartMenuPlusImage+")";
	}
	else
	{
		objListNode.style.display        = "block";
		objSpanTag.style.backgroundImage = "url("+strSmartMenuMinusImage+")";

		document.cookie="SmartMenuCurrentOpenGen="+objElement.id+";expires="+new Date(new Date().getTime() + 365*24*60*60*1000).toGMTString()+";path=/";
	}
}



function SmartMenuCloseMenu(strElementID)
{
	var objListNode   = document.getElementById(strElementID);
	var arrChildLists = objListNode.getElementsByTagName("ul");

	if(arrChildLists.length > 0)
	{
		arrChildLists[0].style.display = "none";
	}
}




function SmartMenuCloseChildren(strParentID)
{
	// This function is recursive, and will trawl down each level of the tree until there are no children sharing the initial parent
	for(var strElementID in arrSmartMenuParents)
	{
		if(arrSmartMenuParents[strElementID] == strParentID)
		{
			SmartMenuCloseMenu(strElementID);
			
			if(bolSmartMenuCascadeChildren == "true")
			{
				SmartMenuCloseChildren(strElementID);
			}
		}
	}
}


function SmartMenuWriteCookie()
{ // Runs through the menu and puts the "states" of each nested list into an array, the array is then joined together and assigned to a cookie.


	if(bolSmartMenuRemember == "true")
	{
		arrCookie=new Array()
		for(var intLoop=0; intLoop<objList.getElementsByTagName("li").length; intLoop++)
		{
			objElement = objList.getElementsByTagName("li")[intLoop];
	
			if(objElement.childNodes.length>0)
			{
				if(objElement.childNodes[0].nodeName=="SPAN" && objElement.getElementsByTagName("ul").length>0)
				{
					bolShow                     = objElement.getElementsByTagName("ul")[0].style.display=="block";
					arrCookie[arrCookie.length] = bolShow;
				}
			}
		}
	}

	document.cookie="SmarMenuStateGen="+arrCookie.join(",")+";expires="+new Date(new Date().getTime() + 365*24*60*60*1000).toGMTString()+";path=/";
}