﻿
/**
*
*  Simple Context Menu
*  http://www.webtoolkit.info/
*
**/
var SimpleContextMenu =
{
	// private attributes
	_menus : new Array,
	_attachedElement : null,
	_menuElement : null,
	_preventDefault : true,
	_preventForms : true,
	_followMousePos : true,
	_ignoreNextHide : true,

	// public method. Sets up whole context menu stuff..
	setup : function (conf)
	{
		if ( document.all && document.getElementById && !window.opera )
			SimpleContextMenu.IE = true;

		if ( !document.all && document.getElementById && !window.opera )
			SimpleContextMenu.FF = true;

		if ( document.all && document.getElementById && window.opera )
			SimpleContextMenu.OP = true;

		if ( SimpleContextMenu.IE || SimpleContextMenu.FF )
		{
			document.oncontextmenu = SimpleContextMenu._show;
			document.onclick = SimpleContextMenu._hide;

			if (conf && typeof(conf.preventDefault) != "undefined")
				SimpleContextMenu._preventDefault = conf.preventDefault;

			if (conf && typeof(conf.preventForms) != "undefined")
				SimpleContextMenu._preventForms = conf.preventForms;

			if (conf && typeof(conf.followMousePos) != "undefined")
				SimpleContextMenu._followMousePos = conf.followMousePos;
		}
	},

	// public method. Attaches context menus to specific class names
	attach : function (classNames, menuId)
	{
		if (typeof(classNames) == "string")
			SimpleContextMenu._menus[classNames] = menuId;

		if (typeof(classNames) == "object")
		{
			for (x = 0; x < classNames.length; x++)
				SimpleContextMenu._menus[classNames[x]] = menuId;
		}
	},

	// private method. Get which context menu to show
	_getMenuElementId : function (e)
	{
		if (SimpleContextMenu.IE)
			SimpleContextMenu._attachedElement = event.srcElement;
		else SimpleContextMenu._attachedElement = e.target;

		while(SimpleContextMenu._attachedElement != null)
		{
			var className = SimpleContextMenu._attachedElement.className;

			if (typeof(className) != "undefined")
			{
				className = className.replace(/^\s+/g, "").replace(/\s+$/g, "");
				var classArray = className.split(/[ ]+/g);

				for (i = 0; i < classArray.length; i++)
				{
					if (SimpleContextMenu._menus[classArray[i]])
						return SimpleContextMenu._menus[classArray[i]];
				}
			}

			if (SimpleContextMenu.IE)
				SimpleContextMenu._attachedElement = SimpleContextMenu._attachedElement.parentElement;
			else SimpleContextMenu._attachedElement = SimpleContextMenu._attachedElement.parentNode;
		}

		return null;
	},

	// private method. Shows context menu
	_getReturnValue : function (e)
	{
		var returnValue = true;
		var evt = SimpleContextMenu.IE ? window.event : e;

		if (evt.button != 1)
		{
			if (evt.target)
				var el = evt.target;
			else if (evt.srcElement)
				var el = evt.srcElement;

			var tname = el.tagName.toLowerCase();

			if ((tname == "input" || tname == "textarea"))
			{
				if (!SimpleContextMenu._preventForms)
					returnValue = true;
				else returnValue = false;
			}
			else
			{
				if (!SimpleContextMenu._preventDefault)
					returnValue = true;
				else returnValue = false;
			}
		}

		return returnValue;
	},

	// private method. Shows context menu by providing an element to show directly
	_showElement : function (element, mousePos, scrollPos)
	{
		if (!element)
			return true;

		SimpleContextMenu._menuElement = element;

		if (typeof onPreMenuAppear == 'function')
			onPreMenuAppear(SimpleContextMenu._attachedElement);

		if (SimpleContextMenu._followMousePos)
		{
			var rtlOffset = SimpleContextMenu.IE ? 0 : -20;
			rtlOffset += 130;
			
			SimpleContextMenu._menuElement.style.left = mousePos.x + scrollPos.x - rtlOffset + 'px';
			SimpleContextMenu._menuElement.style.top = mousePos.y + scrollPos.y + 'px';
		}
		SimpleContextMenu._menuElement.style.display = 'block';
		SimpleContextMenu._ignoreNextHide = true;
		return false;
	},
	
	// private method. Shows context menu
	_show : function (e)
	{
		SimpleContextMenu._hide();
		var menuElement = document.getElementById(SimpleContextMenu._getMenuElementId(e));
		var m = SimpleContextMenu._getMousePosition(e);
		var s = SimpleContextMenu._getScrollPosition(e);
			
		if (SimpleContextMenu._showElement(menuElement,m,s))
			return SimpleContextMenu._getReturnValue(e);
		else return false;
	},

	// private method. Hides context menu
	_hide : function ()
	{
		if (SimpleContextMenu._ignoreNextHide)
		{
			SimpleContextMenu._ignoreNextHide = false;
			return;
		}	
		if (SimpleContextMenu._menuElement)
			SimpleContextMenu._menuElement.style.display = 'none';
	},

	// private method. Returns mouse position
	_getMousePosition : function (e)
	{
		e = e ? e : window.event;
		var position =
		{
			'x' : e.clientX,
			'y' : e.clientY
		}
		return position;
	},

	// private method. Get document scroll position
	_getScrollPosition : function ()
	{
		var x = 0;
		var y = 0;

		if( typeof( window.pageYOffset ) == 'number' )
		{
			x = window.pageXOffset;
			y = window.pageYOffset;
		}
		else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
		{
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		}
		else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
		{
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		}

		var position =
		{
			'x' : x,
			'y' : y
		}

		return position;
	}
}


//// Internals
function setCookie(NameOfCookie, value, expiredays) 
{
	var ExpireDate = new Date();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	document.cookie = NameOfCookie + "=" + (value) + 
	((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function panelGetContentRow(btnElement)
{
	// Parent table
	var tableOwner = btnElement.parentNode.parentNode;
	while (tableOwner.tagName.toLowerCase() != "table")
		tableOwner = tableOwner.parentNode;

	var trOwner = tableOwner.parentNode;
	while (trOwner.tagName.toLowerCase() != "tr")
		trOwner = trOwner.parentNode;
	trOwner = trOwner.parentNode;
	for (var i=trOwner.childNodes.length-1;i>=0;i--)
	{
		if (trOwner.childNodes[i].nodeType != 1) continue;
		if (trOwner.childNodes[i].tagName.toLowerCase() == "tr")
			return trOwner.childNodes[i];
	}
	return null;
}

//// Library
function panelCollapse(button)
{
	button.style.display="none";
	var tdOwner = button.parentNode;
	tdOwner.childNodes[1].style.display="";
	
	var trContent = panelGetContentRow(button);
	trContent.style.display="none";

	setCookie(tdOwner.attributes.getNamedItem("name").value,"collapse",365);
}

function panelRestore(button)
{
	button.style.display="none";
	var tdOwner = button.parentNode;
	tdOwner.childNodes[0].style.display="";

	var trContent = panelGetContentRow(button);
	trContent.style.display="";

	setCookie(tdOwner.attributes.getNamedItem("name").value,"restore",365);
}

function panelRestore(button)
{
	button.style.display="none";
	var tdOwner = button.parentNode;
	tdOwner.childNodes[0].style.display="";

	var trContent = panelGetContentRow(button);
	trContent.style.display="";

	setCookie(tdOwner.attributes.getNamedItem("name").value,"restore",365);
}

function showPageJump(evt,jumpID)
{
	SimpleContextMenu._hide();
	SimpleContextMenu.setup({'preventDefault':false, 'preventForms':false, 'followMousePos':false});
	SimpleContextMenu._showElement(document.getElementById(jumpID),0,0);
}

function hidePageJump()
{
	SimpleContextMenu._hide();
}

function cancelEvent(evt)
{
	if (evt.preventDefault)
		evt.preventDefault();
	else evt.returnValue = false;
}
