﻿function jenaMenu()
{
	this.timer = null;
	this.interval = 25;
	this.contextDelay = 3000;
	this.openMenu = null;
	
	var gMainMenuId = "MainMenu_SubMenu",
		gSubMenuId = gMainMenuId + "_SubMenu";
	
	//clear the existing timer
	this.clearTimer = function()
	{
		if (this.timer != null) 
		{
			clearTimeout(this.timer);
			this.timer = null;
		}
	};
	//close all open menus that should be closed
	this.close = function(pPrimary)
	{
		if (typeof pPrimary == "string")
		{
			var fIndex = pPrimary.indexOf("_SubMenu");
			var lIndex = pPrimary.lastIndexOf("_SubMenu");
			
			jCore("#" + pPrimary).remove();

			if (fIndex > 0 && fIndex == lIndex) 
			{
				jCore("#" + pPrimary + "_SubMenu").remove();
				this.setOpenMenu(null);
			}
		}
		else
		{
			pPrimary = pPrimary || true;
		
			jCore("#" + gMainMenuId).remove();
			if (pPrimary) jCore("#" + gSubMenuId).remove();
			
			this.setOpenMenu(null);
		}
	};
	//create a context menu
	this.context = function(pEvent, pMenu, pContext)
	{
		this.clearTimer();
		
		jCore("#ContextMenu").remove();
		
		var strMenu = this.createContextMenu(pContext),
			Left = jPage.mousePos(pEvent).x,
			Top = pMenu.offsetHeight;

		jCore(body).append("div").id("ContextMenu").text(strMenu).action("onmouseout", "jMenu.hide('ContextMenu');").pos().left(Left + "px").pos().top(Top, pMenu);
			
		this.timer = setTimeout(function(){jMenu.close('ContextMenu');}, this.contextDelay);
		
		return false;
	};
	this.createContextMenu = function(pNode)
	{
		var Menu = jXml(this.wrapXml("Menu", pNode)).nodes("Menu/Item");
		var strMenu = "";
		
		for (iMenu = 0; iMenu < Menu.length; iMenu++)
		{
			var Class = "ContextMenuItem" + (iMenu < (Menu.length - 1) ? " Bordered" : "");
			
			var strCaption = Menu[iMenu].node("Caption").value;
				strLink = this.wrapAttribute("onclick", Menu[iMenu].node("Url").value.replace(/\"/g, "'")),
				strClass = this.wrapAttribute("class", Class),
				strMouseOver = this.wrapAttribute("onmouseover", " jMenu.clearTimer(); this.className+=' Hover';"),
				strMouseOut = this.wrapAttribute("onmouseout", "this.className='" + Class + "'");
				
			strMenu += this.wrapXml("div", strCaption, strClass + strLink + strMouseOver + strMouseOut);
		}
		
		return strMenu;
	};
	//create the submenu "div" element
	this.createSubMenu = function(pNode, pPrimary, pSubClass)
	{
		var SubMenu = pNode.nodes("SubMenu/Item"),
			strSubMenuId = gMainMenuId + (pPrimary ? "" : "_SubMenu"),
			strCaption = pNode.node("Caption").value,
			strUrl = pNode.node("Url").value.replace(/\#ap;/g, "'") + ";jMenu.close();";
			strClass = this.wrapAttribute("class", "MainMenuItem" + pSubClass),
			strLink = this.wrapAttribute("onclick", strUrl),
			strMouseOver = "jMenu.show(this,'",
			strMouseOut = "jMenu.hide('" + strSubMenuId + "');",
			strSubMenu = "";
			
		for (iSubMenu = 0; iSubMenu < SubMenu.length; iSubMenu++)
		{
			strSubMenuItems = 
				this.wrapXml("Caption", SubMenu[iSubMenu].node("Caption").value) +
				this.wrapXml("Url", SubMenu[iSubMenu].node("Url").value);
				
			strSubMenu += this.wrapXml("Item", strSubMenuItems);
		}
		strMouseOver += strSubMenu + "','" + strSubMenuId + "_SubMenu');";
		
		strMouseOver = this.wrapAttribute("onmouseover", "this.className+=' Hover';" + strMouseOver);
		strMouseOut = this.wrapAttribute("onmouseout", "this.className=this.className.replace(' Hover', '');" + strMouseOut);
		
		return this.wrapXml("div", strCaption, strClass + strLink + strMouseOver + strMouseOut);
	};
	//trigger the timeout that will close the sub menu
	this.hide = function(pPrimary)
	{
		this.timer = setTimeout(function(){jMenu.close(pPrimary);}, this.interval);
	};
	this.setOpenMenu = function(pMenu)
	{
		if (this.openMenu != null) this.openMenu.className = "MainMenuItem";
		
		this.openMenu = pMenu;
	};
	this.setOpenMenuClass = function()
	{
		if (this.openMenu.className.indexOf(" Hover") < 0) this.openMenu.className += " Hover";
	};
	//show the sub menu (if there is one)
	this.show = function(pMenu, pSubMenu, pMenuId)
	{
		pMenuId = pMenuId || gMainMenuId;
		var PrimarySub = (pMenuId === gMainMenuId),
			SecondarySub = (pMenuId === gSubMenuId);
			
		this.clearTimer();
		
		if (PrimarySub) 
		{
			jCore("#" + gMainMenuId).remove();
			this.setOpenMenu(pMenu);
		}
		if (PrimarySub || SecondarySub)
		{ 
			jCore("#" + gSubMenuId).remove();
			this.setOpenMenuClass();
		}
		
		if (pSubMenu !== "")
		{
			var Menu = jXml(this.wrapXml("Menu", pSubMenu.replace(/\&/g, "&amp;"))).nodes("Menu/Item"),
				strSubMenu = "",
				Left = (!PrimarySub ? pMenu.offsetWidth - 1 : 0),
				Top = (!PrimarySub ? 0 : pMenu.offsetHeight),
				strSubMenuId = gMainMenuId + (PrimarySub ? "" : "_SubMenu");
				
			for (var iMenu = 0; iMenu < Menu.length; iMenu++)
			{		
				var SubClass = "";
				
				if (iMenu === 0 && iMenu === (Menu.length - 1)) SubClass = " First Last";
				else if (iMenu === 0) SubClass = " First";
				else if (iMenu === (Menu.length - 1)) SubClass = " Last";
				
				strSubMenu += this.createSubMenu(Menu[iMenu], PrimarySub, SubClass);
			}

			jCore(body).append("div").id(pMenuId).text(strSubMenu).pos().left(Left, pMenu).pos().top(Top, pMenu);
		}
	};
	//return a HTML wrapped attribute
	this.wrapAttribute = function(pName, pValue)
	{
		return " " + pName + "=\"" + pValue + "\"";
	};
	//return an XML wrapped element
	this.wrapXml = function(pName, pValue, pAttributes)
	{
		pAttributes = pAttributes || "";
		
		return "<" + pName + pAttributes + ">" + pValue + "</" + pName + ">";
	};
};

var jMenu = new jenaMenu();
