var __actionHolders = new Object();
var __ActionMenuEl;
var __ActionMenuElDoc;
var __ActionMenuCurrent;
// rename to aspform per .NET 2.0 change
var __ActionMenuInput;

jQuery(document).ready(function(){
	var container = window;
	var doc = container.document;
	if ($j("#ActionMenu").length == 0)
	{
		$j(doc.body).append('<iframe id="ActionMenu" class="ActionMenu" src="/ActionMenu.html" frameborder="no" scrolling="no"></iframe>');
	}
	__ActionMenuEl = $j("iframe#ActionMenu")[0];
	__ActionMenuElDoc = container.frames["ActionMenu"];         //--- for IE
	if (__ActionMenuElDoc == null)								//--- for FF
	{
		__ActionMenuElDoc = __ActionMenuEl.contentDocument.defaultView;
	}
	__ActionMenuCurrent = null;
	__ActionMenuInput = parent.document.forms[0]["__PopupMenuInput"];
});

function DoAction( commandName, id, who )
{
	var parent = $j(who).parent()[0];
	while ( parent != null && __actionHolders[parent.id] == null )
	{
		parent = $j(parent).parent()[0];
	}
	var actions;
	if ( parent == null )
	{
		actions = __actionHolders["document"]
	}
	else
	{
		actions = __actionHolders[parent.id];
	}
	commandName = commandName.toLowerCase();
	if (actions[commandName] == null)
	{
		alert( commandName + " is not registered." );
		return;
	}
	if (commandName == "delete")
	{
		if(confirm("Are you sure you want to " + commandName + "?") )
		{
			actions[ commandName ]( id, commandName, who );
		}
	}
	else
	{
		actions[ commandName ]( id, commandName, who );
	}
}

function RegisterAction( commandName, method, parentId )
{
	var holder = (parentId == null || parentId == "") ? "document" : parentId;
	var actions = __actionHolders[holder];
	if ( actions == null )
	{
		actions = new Object();
	}
	actions[ commandName.toLowerCase() ] = method;
	__actionHolders[holder] = actions;
}

function ActionMenuOpen(srcElement)
{
	var el, oPos, oMenuItem, i, nodes;
	var oMenu, oXmlMenu, oRoot, oMenuEl;
	oMenu = new Array();
	el = srcElement;
	if (el.getElementsByTagName("xml").length > 0)
	{
		nodes = el.getElementsByTagName("xml")[0].XMLDocument.childNodes[0].childNodes;
	}
	else
	{
		nodes = el.getElementsByTagName("item");
	}
	for(i = 0; i < nodes.length; i++)
	{
		oMenuEl = nodes[i];
		oMenuItem = {label: oMenuEl.getAttribute("text"), command: oMenuEl.getAttribute("command")};
		if (oMenuEl.getAttribute("confirm"))
		{
			oMenuItem.confirm = true;
		}
		else
		{
			oMenuItem.confirm = false;
		}
		if (oMenuEl.getAttribute("input"))
		{
			oMenuItem.input = oMenuEl.getAttribute("input");
		}
		oMenu.push( oMenuItem );
	}
	var offset = {};
	$j(el).offset({}, offset);
	offset.top += $j(el).height();
	$j(__ActionMenuEl).css(offset);
	__ActionMenuCurrent = el;
	var isMouseOver;
	if (window.event != null)
	{
		isMouseOver = (window.event.type =="mouseover");
	} 
	else 
	{
		isMouseOver = false;
	}
	if (__ActionMenuEl.style.visibility == "visible" && el == __ActionMenuCurrent && !isMouseOver)
	{
		ActionMenuClose();
	}
	else
	{
		__ActionMenuElDoc.Context = this;
		__ActionMenuElDoc.onMenu = function(width, height)
		{
			this.Context.ActionMenuShow(width, height);
		}
		__ActionMenuElDoc.onSelect = function(oMenu)
		{
			this.Context.ActionMenuSelect(oMenu);
		}
		__ActionMenuElDoc.BuildMenu( oMenu );
	}
}

function ActionMenuSelect(oMenu)
{
	var sCommand = oMenu.command;
	var oInput = $j("div.Action")[0];
	var el = $j(__ActionMenuCurrent);
	var itemId = ( el.attr("itemid") ) ? el.attr("itemid") : "";
	var usePostBack = ( el.attr("usepostback") == "False" ) ? false : true;
	if ( !usePostBack )
	{
		DoAction(sCommand, itemId, el[0]);
	}
	else
	{
		if (oMenu.confirm)
		{
			if (! confirm("Are you sure you want to " + oMenu.label + "?") )
			{
				this.ActionMenuClose();
				return;
			}
		}
		if (oMenu.input)
		{
			var sInput;
			sInput = window.prompt(oMenu.input, "");
			if (sInput.length > 0 && sInput != null && sInput != "null" && sInput != " ")
			{
				__ActionMenuInput.value = sInput;
			}
			else
			{
				this.ActionMenuClose();
				return;
			}
		}
		oInput.value = sCommand;
		oInput.onchange();
	}
	this.ActionMenuClose();
}

function ActionMenuShow(iWidth, iHeight)
{
    var menu = $j(__ActionMenuEl);
    menu.width(iWidth)
        .height(iHeight)
        .css("visibility",  "visible")
        .css("left",        menu.offset().left + $j(__ActionMenuCurrent).width() - iWidth);
}

function ActionMenuClose()
{
	$j(__ActionMenuEl)
		.width(0)
		.height(0)
		.css("visibility", "hidden");
	__ActionMenuCurrent = null;
}

document.onmouseup = ActionMenuClose;
