function MM_GetMenuParent(e)
{
	if (!e) return null;
	while (e.tagName!="BODY" && !e.bMenuDiv)
		e = e.parentNode;
	return (e.bMenuDiv)?e:null;
}

function MM_MouseOut(e)
{
	if (this.bMenuDiv && this.bAllowHide)
	{
		this.style.display = "none";
		this.bAllowHide = false;
	}
}

function MMP_MouseOver() { if (this.className=="") this.className = "hover"; }
function MMP_MouseOut(e)
{
	if (this.className=="hover")
		this.className = "";

	if (!e) e = window.event
	var eFrom = e.srcElement?e.srcElement:e.target;
	var eTo = e.toElement?e.toElement:e.relatedTarget;

	var eF = MM_GetMenuParent(eFrom);
	var eT = MM_GetMenuParent(eTo);

	if (eF)
		eF.bAllowHide = (eF!=eT);
}

function MMP_Click()
{
	var oMM = this.oMM;
	oMM.showMenu(false);
	oMM.setSel(this);
	oMM.oMO.setCurLayer(this.sLayer);
}


function MMI_MouseOver() { this.style.backgroundColor = "#FFF0F0"; }
function MMI_MouseOut() { this.style.backgroundColor = "#CCCCCC"; }
function MMI_Click()
{
	this.obj.eM.style.display = "block";
}

/* Object definition: */
function MapMenu(oMapObj)
{
	this.oMO = oMapObj;
	var el = this.node = document.createElement("DIV");
	el.obj = this;
	el.bPreserve = true;
	el.id = "MapMenuIcon";

	el.onmouseover = MMI_MouseOver;
	el.onmouseout = MMI_MouseOut;
	el.onclick = MMI_Click;

	var eM = this.eM = document.createElement("DIV");
	eM.bMenuDiv = true;
	eM.id = "MapMenuDiv";
	eM.onmouseout = MM_MouseOut;

	this.arrLayers = null;

	el.appendChild(eM);
}

MapMenu.prototype.prepareP = function (sText)
{
	var eP = document.createElement("P");
	eP.oMM = this;
	eP.sLayer = sText;

	eP.appendChild(document.createTextNode("Show " + sText + " Map"));

	eP.onmouseover = MMP_MouseOver;
	eP.onmouseout = MMP_MouseOut;
	eP.onclick = MMP_Click;

	return eP;
}

MapMenu.prototype.update = function(arr)
{
	var bReset = true;
	var a, i, n;

	a = this.arrLayers;

	if (a)
	{
		n = a.length;

		if (n == arr.length)
			for (i=0; bReset && i<n; i++)
				bReset = (a[i] == arr[i]);
	}

	if (bReset)
	{
		RemoveChildren(this.eM);
		var eDefault;
		for (i=0; i<arr.length; i++)
			this.eM.appendChild(this.prepareP(arr[i]));

		this.setSel(this.eM.firstChild);
		this.arrLayers = arr;
	}

	this.showIcon(top.bLoggedIn);
}

MapMenu.prototype.showIcon = 
function(bShow)
{
	this.node.style.display = (this.arrLayers && this.arrLayers.length>1 && bShow)?"block":"none";
}

MapMenu.prototype.showMenu = 
function(bShow)
{
	this.eM.style.display = (this.arrLayers && this.arrLayers.length>1 && bShow)?"block":"none";
}

MapMenu.prototype.setSel = function (e)
{
	if (this.eCurSel)
		this.eCurSel.className = "";

	if (e)
		e.className = "sel";

	this.eCurSel = e;
}
