
PopupMenu = function(menuItemId, popupMenuId)
{
	this.MenuItemId = menuItemId;
	this.PopupMenuId = popupMenuId;
	
	this.MenuItem = document.getElementById(this.MenuItemId);
	this.PopupMenu = document.getElementById(this.PopupMenuId);
	
	this.PopupMenu.style.display = "none";	
	this.Visible = false;	
	
	//MenuOnMouseOver
	var currentObject = this;
	currentObject.MenuOnMouseOver = function() {currentObject.Show();}
	currentObject.MenuOnMouseOut = function() { currentObject.Hide();}
	this.MenuItem.onmouseover = currentObject.MenuOnMouseOver;
	document.onmousemove = function(ev) { currentObject.MouseMove(ev);}
	document.onclick = currentObject.MenuOnMouseOut;
	
	this.IsRun = false;
}

PopupMenu.prototype.MouseMove = function(ev)
{
	if (!ev) ev = event;

	/*if (!this.IsRun)
	{
		this.IsRun = true;
		alert(ev.clientX);
	}*/
	
	//this.MenuItem.innerText = "Coords: (" + ev.clientX + ", " + ev.clientY + ")";	
	//window.status = "ZZZZZZZZZZZZ"; //event.screenY;
	
	if (ev.clientY < this.findPosY(this.PopupMenu) - 20 || ev.clientX < this.findPosX(this.PopupMenu) ||
		ev.clientY > this.findPosY(this.PopupMenu) + this.PopupMenu.offsetHeight ||
		ev.clientX > this.findPosX(this.PopupMenu) + this.PopupMenu.offsetWidth)
	{
		this.Hide();
	}
}

PopupMenu.prototype.Show = function()
{
	if (this.Visible) return true;
	
	//alert(this.findPosY(this.MenuItem));
	
	var posPopupTop = this.findPosY(this.MenuItem) + this.MenuItem.offsetHeight+2;
	var posPopupLeft = this.findPosX(this.MenuItem) - 2;
	this.PopupMenu.style.top = posPopupTop + "px";
	this.PopupMenu.style.left = posPopupLeft + "px";

	this.PopupMenu.style.display = "inline";	
	this.Visible = true;	
}

PopupMenu.prototype.Hide = function()
{
	//alert(this.Visible);
	if (!this.Visible) return true;	

	this.PopupMenu.style.display = "none";	
	this.Visible = false;	
}

PopupMenu.prototype.findPosX = function(htmlObject)
{
	var result = 0;
    
	if (htmlObject.offsetParent)
	{
		while (htmlObject.offsetParent)
		{
			result += htmlObject.offsetLeft
			htmlObject = htmlObject.offsetParent;
		}
	}
	else if (htmlObject.x)
	{
	    result += htmlObject.x;
	}
		
	return result;
}

PopupMenu.prototype.findPosY = function(htmlObject)
{
	var result = 0;
    
	if (htmlObject.offsetParent)
	{
		while (htmlObject.offsetParent)
		{
			result += htmlObject.offsetTop
			htmlObject = htmlObject.offsetParent;
		}
	}
	else if (htmlObject.y)
	{
	    result += htmlObject.y;
	}
		
	return result;
}
