// =============================================================================
//
// NOTE: Needs jQuery library as it uses it for node manipulation
//
// =============================================================================

var FlyOutMenu = function(_id, _leftAdjust, rootPath)
{
	// === initialize elements ===
	var menu_id = _id;
	var menu_selector = "#" + _id;
	var leftAdjust = _leftAdjust;
	var primaryLevel = document.getElementById(menu_id);
	var browser = navigator.userAgent;

	AttachLIEvents();

	if(browser.indexOf('MSIE 6') >= 0) {
		FixIE6RoundedCorners();
	}

	// === private methods ===

	// Goes through all LIs and attach mouseover events to them
	function AttachLIEvents()
	{
		jQuery('li', primaryLevel).each(function() {
			AttachMouseOvers(this);
			AttachMouseOuts(this);
		});
	}

	function AttachMouseOvers(elem)
	{
		elem.onmouseover = function(event)
		{
			ShowChildMenu(this, event);
			SimulateCssLIHover(this, "visible");
		}
	}

	function AttachMouseOuts(elem)
	{
		// if mouse out of LIs parent UL, then hide the UL
		elem.parentNode.onmouseout = function(event)
		{
			HideULs(this);

			// to stop event bubbling that causes flickering in FF2 MAC
			if(IsMacFirefox2())
			{
				e = event || window.event;
				if(e.stopPropagation) {e .stopPropagation() };
			}
		}

		// simulate the LI hover in IE6 since it doesnt support hover on LI elements
		elem.onmouseout = function()
		{
			SimulateCssLIHover(this, "hidden");
		}
	}

	// shows an item's submenu if it has one. currentElem should be an LI.
	function ShowChildMenu(currentElem)
	{
		var hasChildMenu = HasChildMenu(currentElem);
		var parentElem = currentElem.parentNode;
		var childElem = currentElem.getElementsByTagName('ul')[0];

		if(hasChildMenu)
		{
			PositionShadow(childElem, "#shadowDIV");

			if(parentElem.id != menu_id)
			{
				// level2's and level3's height has to match.
				// this figures out which is taller and sets height accordingly
				if(parentElem.offsetHeight > childElem.offsetHeight)
				{
					childElem.style.height = parentElem.offsetHeight + "px";
				}
				else
				{
					parentElem.style.height = childElem.offsetHeight + "px";
				}

				// positions level3 to the right of level2
				childElem.style.left = (parentElem.offsetWidth - leftAdjust) + "px";
				PositionShadow(childElem, "#shadowDIV2");
			}
			childElem.style.visibility = "visible";
		}
	}

	// find position of flyout menu in relation to container_id argument (or window if container not passed in)
	function FindPos(obj, container_id)
	{
		var curleft = curtop = 0;

		if (obj.offsetParent)
		{
			do
			{
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
				obj = obj.offsetParent;

				// modified so we only return position in relation to this container
				if ((typeof container_id == "string") && (obj.id == container_id))
				{
					break;
				}
			} while (obj);
		}

		return [curleft,curtop];
	}

	// place shadow position in same area as child menu w/ helper function FindPos()
	function PositionShadow(childElem, id)
	{
		var childElemPos = FindPos(childElem, "center");
		var shadowLeft = childElemPos[0] - 3;
		var shadowTop = childElemPos[1] + 6;
		var shadowWidth = childElem.offsetWidth + 6;
		var shadowHeight =  childElem.offsetHeight;

		jQuery(id).css("left", shadowLeft + "px");
		jQuery(id).css("top", shadowTop + "px");
		jQuery(id).css("width", shadowWidth + "px");
		jQuery(id).css("height", shadowHeight + "px");
		jQuery(id).css("display", "block");
		jQuery(id + "bottom").css("width", shadowWidth + "px");
		jQuery(id + "bottom").css("left", shadowLeft + "px");
		jQuery(id + "bottom").css("top", shadowTop + shadowHeight + "px");
		jQuery(id + "bottom").css("display", "block");
	}

	function HasChildMenu(currentElem)
	{
		var elems = jQuery("ul", currentElem);
		return (elems.length > 0);
	}

	// Hides all ULs under given element
	function HideULs(elem)
	{
		var ULs = jQuery("ul", elem);
		ULs.css({'visibility':'hidden', 'height':'auto'});
		jQuery("#shadowDIV").css("display", "none");
		jQuery("#shadowDIV2").css("display", "none");
		jQuery("#shadowDIVbottom").css("display", "none");
		jQuery("#shadowDIV2bottom").css("display", "none");
	}

	// Since IE6 only supports hover on <a> elements and does not support child
	// selectors, we have to simulate the li:hover for maintaining on states
	function SimulateCssLIHover(elem, toggleStr)
	{
		if(browser.indexOf('MSIE 6') >= 0)
		{
			KeepLevel1OnState(elem, toggleStr);
			KeepLevel2OnState(elem, toggleStr);
		}
	}

	// Keeps the on state for Level 1 under all subnodes
	function KeepLevel1OnState(elem, toggleStr)
	{
		var anchorElems = elem.getElementsByTagName("a");

		if( (anchorElems.length > 0) && (jQuery(anchorElems[0]).hasClass('level1')) )
		{
			if(toggleStr == "visible") {
				anchorElems[0].parentNode.style.backgroundPosition = "0px -25px";
				jQuery(elem).css('z-index','1');
			} else {
				anchorElems[0].parentNode.style.backgroundPosition = "top";
				jQuery(elem).css('z-index','-1');
			}
		}
	}

	// Keeps the on state for Level 2 under all subnodes
	function KeepLevel2OnState(elem, toggleStr)
	{
		if ( !KeepLevel2OnState.tertiaryLIs ) {
			KeepLevel2OnState.tertiaryLIs = jQuery(menu_selector + " ul ul li");
		}

		for(var i=0; i < KeepLevel2OnState.tertiaryLIs.length; ++i)
		{
			if(KeepLevel2OnState.tertiaryLIs[i] == elem)
			{
				if(toggleStr == "visible")
				{
					jQuery(elem.parentNode.parentNode).css("backgroundColor", "white");
					jQuery("> a", elem.parentNode.parentNode).css("color", "#4B0162");
				}
				else
				{
					jQuery(elem.parentNode.parentNode).css("backgroundColor", "");
					jQuery("> a", elem.parentNode.parentNode).css("color", "");
				}
			}
		}
	}

	// IE6 can't have LIs absolutely positioned, so we have to use a DIV for IE6
	// for the bottom rounded corner and remove the original LI node
	function FixIE6RoundedCorners()
	{
		// fixes top
		jQuery(menu_selector + " ul .level2top div").css("backgroundPosition", "top right");


		// fixes bottom
		jQuery('li.level2bottom', primaryLevel).each(function(i) {
			if( jQuery(this).hasClass("level2bottom") ) {
				this.removeNode();
			}
		});
	}

	function IsMacFirefox2()
	{
		return ((browser.indexOf("Macintosh") >= 0) && (browser.indexOf("Firefox/2") >= 0));
	}
}

