// JavaScript Document


// ----------------------Sliding Menus-------------------------------------------

// Check whether browser is supported by trying getElementById();
if(document.getElementById)
{
	stdBrowser = true;
	if (window.event != null)
	{
		window.event.cancelBubble = true;
	}
}
else 
{
	stdBrowser = false;
}

function toggleMenu(menuName, submenuName, showIt)
{
	// If possible get handles to menu & submenu
	if(stdBrowser)
	{
		menuObj = document.getElementById(menuName);
		submenuObj = document.getElementById(submenuName);
	}
	else
	{
		alert("Your Browser is not supported by this site");
	}

	// If this is a menu show event...
	if(showIt)
	{
		// Emphasize the menu text
		//menuObj.style.backgroundColor  = "bold";
		menuObj.style.backgroundColor  = "ffffff";
		menuObj.style.color  = "19478A";
		
		// Work out the position of the menu
		var point = getPageXY(menuObj);
		pxLeft = point.x + menuObj.offsetWidth;
		pxTop = point.y;

		// Set the location of the submenu	
		submenuObj.style.top = pxTop + "px";
		submenuObj.style.left = pxLeft + "px";
		
		// Set up the submenu mouse events
		//setupSubmenu(submenuObj);
		//alert("before getElementsByName");
		var comboDivs = document.getElementsByName("combo_div");
		//alert("after getElementsByName - " + comboDivs.length);
		for (i=0; i<comboDivs.length; i++)
		{
			//alert("Found element " + i + " with name combo_div - " + comboDivs[i]);
			comboDivs[i].style.visibility = "hidden";
		}

		// ...and display it		
		//alert("in IE show");
		submenuObj.style.visibility = "visible";
		submenuObj.style.display = "block";		
		
	}
	// Otherwise we are hiding the menu
	else
	{
		//alert("in show");
		// Get the class of the current object
		var menuClass = menuObj.className;
		
		// Iterate through styles until we find the selected
		var requiredRule = null;
		var ruleName = null;
		var sheets = document.styleSheets;
		//alert("How many stylesheets? " + sheets.length);
		for (i=0; i<sheets.length; i++)
		{
			var currentSheet = sheets[i];
			var rules;
			if (currentSheet.cssRules) 
			{ // -- this is for N6+ and Mac IE 5
   				var rules= currentSheet.cssRules;
			}
			else if (currentSheet.rules) 
			{ // -- this is for Win IE
				var rules = currentSheet.rules;
			}
			
			if(rules == null)
				continue;

			for (j=0; j<rules.length; j++)
			{				
				ruleName = rules[j].selectorText;
				if(ruleName == null)
					continue;
				//alert("Trying rule: " + ruleName);
				ruleNameTrimmed = ruleName.substring(1, ruleName.length)
				//alert("Rule Name Trimmed: " + ruleNameTrimmed+ " == Menu class: " + menuClass);				
				if(menuClass.toLowerCase() == ruleNameTrimmed.toLowerCase())
				{
					requiredRule = rules[j];
					//alert("reqd rule: " + requiredRule);
					break;
				}
			}			
		}
		
		//alert("before ie bgcolor");
		menuObj.style.backgroundColor = requiredRule.style.backgroundColor;
		//alert("before ie stylecolor");
		menuObj.style.color = requiredRule.style.color;
		
		//alert("before getElementsByName");			
		var comboDivs = document.getElementsByName("combo_div");
		//alert("after getElementsByName - " + comboDivs.length);
		for (i=0; i<comboDivs.length; i++)
		{
			//alert("Found element " + i + " with name combo_div - " + comboDivs[i]);
			comboDivs[i].style.visibility = "visible";
		}

		//alert("in IE hide");
		submenuObj.style.visibility = "hidden";
		submenuObj.style.display = "block";		
	}	
}

function setupSubmenu(submenu)
{
	// Iterate through children looking for <UL>
	for (i = 0; i < submenu.childNodes.length; i++)
	{
		var tdChild = submenu.childNodes[i];
		//alert(tdChild.nodeName);
		if (tdChild.nodeName == 'UL')
		{
			//alert("Found UL"+tdChild);
			// Iterate through children attaching event function
			for (j = 0; j < tdChild.childNodes.length; j++)
			{
				var liElem = tdChild.childNodes[j];
				alert(liElem.nodeName);
				
				// attach onmouseover function
        		liElem.onmouseover = function (e)
        		{
					alert("onmouseover called");
					//this.style.backgroundColor = "#FFF4FF";
					this.style.backgroundColor = "#000000";
					//this.style.fontWeight = "bold";
					return true;
				}
				// attach onmouseout function
				liElem.onmouseout = function (e)
        		{
					alert("onmouseout called");
					this.style.backgroundColor = "#FFF4FF";
					//this.style.fontWeight = "normal";
					return true;
				}
			}
		}
	}
}



function showPageXY(elem)
{
	elemObj = document.getElementById(elem);
	//alert("x: "+elemObj.offsetLeft+", y: "+elemObj.offsetTop);
	point = getPageXY(elemObj);
	alert("x: "+point.x+", y: "+point.y);
}

function getPageXY(elm)
{
	var point = { x: 0, y: 0 };
	while (elm)
	{
		point.x += elm.offsetLeft;
		point.y += elm.offsetTop;
		elm = elm.offsetParent;
	}
	
  	return point;
}
