var objCurrentItem;

function setCurrentItem(objItem)
{
	objCurrentItem = objItem;
	
	document.body.style.cursor = "wait";
}

function _internal_selectMenu(strType, strAddress, strCacheName)
{
	var objAjax;
	var objElement, objFrame;
	var nPageHeight, objContainer, objMenuTitle;
	
	var objBody = document.body;
	objElement = document.getElementById("div_" + strType);
	objFrame = document.getElementById("iframe_" + strType);
	
	if (objElement)
	{
		objBody.removeChild(objElement);
		objBody.removeChild(objFrame);
	}

	objFrame = document.createElement("iframe");
	objFrame.id = "iframe_" + strType;
	objFrame.setAttribute("menu", "true");
	objFrame.className = "menuItem";
	objFrame.scrolling = "no";
	objFrame.frameBorder = "0";
	objBody.appendChild(objFrame);
	
	objElement = document.createElement("div");
	objElement.id = "div_" + strType;	
	objElement.setAttribute("menu", "true");

	objElement.className = "menuItem";
	objBody.appendChild(objElement);

	objAjax = new CAjax();
	objAjax.create();

	if (objAjax.open(strAddress, true, strCacheName))
	{
		if (!document.getElementById("closebar_" + strType))
		{
			var objClose;
			
			objMenuTitle = document.createElement("div");
			objMenuTitle.id = "closebar_" + strType;
			objMenuTitle.className = "menuOptionTitle";
			
			objClose = document.createElement("img");
			
			objClose.src = "images/closebutton.png";
			objClose.className = "closeButton";
			objMenuTitle.appendChild(objClose);
			
			try
			{
				objClose.attachEvent("onclick", function() { hideMenus();});
			}
			catch (e)
			{
				objClose.addEventListener("click", function() { hideMenus();}, false);
				
			}
			
			objContainer = document.createElement("div");
			objContainer.id = "container_" + strType;
			objContainer.className = "menuItemContainer";

			objElement.appendChild(objMenuTitle);
			objElement.appendChild(objContainer);
		}
		
		objMenuTitle = document.getElementById("closebar_" + strType);
		objContainer = document.getElementById("container_" + strType);
		objContainer.innerHTML = objAjax.getResponse();

	}
	else
	{
		alert(objAjax.getError());
		return;
	}
	
	
	objElement.style.left = findRight(objCurrentItem) - 40 + document.body.scrollLeft;
	objElement.style.top = findTop(objCurrentItem);
	
	objFrame.style.left = findLeft(objElement);
	objFrame.style.top = findTop(objElement);
	
	nPageHeight = getHeight();

	objContainer.style.height = null;
	
	if (findBottom(objElement)- findTop(objElement) > nPageHeight)
	{
		objContainer.style.height = nPageHeight + findTop(objMenuTitle) - findBottom(objMenuTitle) - 10;
	}

	if (findBottom(objElement) > nPageHeight + document.body.scrollTop)
	{
	
		objElement.style.top = findTop(objElement) - findBottom(objElement) + nPageHeight + document.body.scrollTop;
		objFrame.style.top  = findTop(objElement) - findBottom(objElement) + nPageHeight + document.body.scrollTop;
	}

	objFrame.style.height = findBottom(objElement) - findTop(objElement);
	objFrame.style.width = findRight(objElement) - findLeft(objElement);
	
	objElement.style.visibility = "visible";
	objElement.style.display = "block";
	objFrame.style.visibility = "visible";
	objFrame.style.display = "block";
	
	document.body.style.cursor = "auto";
	
	endLoading();
	
	return objElement;
}

function selectMenu(strType, strAddress, strCacheName)
{
	var objElement;
	
	objElement = _internal_selectMenu(strType, strAddress, strCacheName);
	
	addMenuCheck();
	
	return objElement;
}

function resetMenuItem(strType, strText)
{
	var objItem, objFrame, objBody;
	
	objBody = document.body;
	objMenu = document.getElementById("div_" + strType);
	objFrame = document.getElementById("iframe_" + strType);
	
	if (!strText)
	{
		strText = "&nbsp;";
	}
	
	if (objMenu)
	{
		objBody.removeChild(objMenu);
		objBody.removeChild(objFrame);
	}
	
	objItem = document.getElementById("selected_" + strType);
	objItem.className = "menuOptionLauncher_disabled";
	objItem.innerHTML = strText;
	eval("options." + strType + ".value = ''");
}

function hideMenus()
{
	_hideMenus();
	
	removeMenuCheck();
}

function _hideMenus()
{
	var objMenus = document.getElementsByTagName("div");
	
	for (i = 0; i < objMenus.length; i++)
	{
		if (objMenus[i].getAttribute("menu") == "true")
		{
			
			objMenus[i].style.visibility = "hidden";
			objMenus[i].style.display = "none";
			objMenus[i].style.left = 0;
			objMenus[i].style.top = 0;
		}
	}
	
	objMenus = document.getElementsByTagName("iframe");
	
	for (i = 0; i < objMenus.length; i++)
	{
		if (objMenus[i].getAttribute("menu") == "true")
		{
			objMenus[i].style.visibility = "hidden";
			objMenus[i].style.display = "none";
			objMenus[i].style.left = 0;
			objMenus[i].style.top = 0;
		}
	}
}

function checkMenu(e)
{
	if (_checkMenu(e))
	{
		removeMenuCheck();
	}
}

function _checkMenu(e)
{
	var objElement, objMenu, bReturnVal;
	var j, bFound;
	
	bReturnVal = false;
	
	objElement = (e.target) ? e.target : e.srcElement;
	objMenu = document.getElementsByTagName("div");

	if (objElement.getAttribute("menu") != "true")
	{
		for (var i = 0; i < objMenu.length; i++)
		{
			if (objMenu[i].getAttribute("menu") == "true")
			{
				while (objElement != null && !bFound)
				{
					objElement = objElement.parentNode;
					
					for (j = 0; j < objMenu.length; j++)
					{
						if (objElement == objMenu[j])
						{
							bFound = true;
							break;
						}
					}
				}
			
				if (objElement != null)
				{
					break;
				}
			}
		}
	}
	
	if (objElement == null)
	{
		_hideMenus();
		bReturnVal = true;
	}
	
	return bReturnVal;
}

function addMenuCheck()
{
	try
	{
		document.attachEvent("onmousedown", checkMenu);
	}
	catch (e)
	{
		document.addEventListener("mousedown", checkMenu, false);
		
	}
}

function removeMenuCheck()
{
	try
	{
		document.detachEvent("onmousedown", checkMenu);
	}
	catch (e)
	{
		document.removeEventListener("mousedown", checkMenu, false);
	}
}