/*
* Do not reuse this for the next project.
* Lack of object-oriented approach leads to many implementation limitations/shortcomings 
* - MP
*/
var defaultMenuWidth="150px" //set default menu width.
////No need to edit beyond here
var bIe5=document.all && !window.opera;
var ns6=document.getElementById;
function contextMenuPreHide(){}
if (bIe5||ns6)
	document.write('<div style="text-align:left;position:relative;z-index:4;zoom:1;"><div id="popitmenu" onMouseover="clearhidemenu();" onMouseout="dynamichide(event)"></div></div>')

function iecompattest(){
	return (document.compatMode && document.compatMode.indexOf("CSS")!=-1)? document.documentElement : document.body
}

if(typeof getWindowSize == 'undefined'){
	function getWindowSize() {
	  var myWidth = 0, myHeight = 0;
	  if(typeof( window.innerWidth ) == 'number' ) {
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	  }
	  return [myWidth, myHeight];
	}
}
function showmenu(e, cMenuID, optWidth, cClassName, fnPreDisplay){
	if (!document.all&&!document.getElementById)return
	clearhidemenu();
	
	menuobj=bIe5? document.all.popitmenu : document.getElementById("popitmenu")

	menuobj.innerHTML = '';
	//menuobj.innerHTML= bIe5 ? document.all[cMenuID].innerHTML : document.getElementById(cMenuID).innerHTML;
	//menuobj.appendChild(document.getElementById(cMenuID).cloneNode(true))
	if(typeof(cMenuID) == 'string'){
		menuobj.appendChild(document.getElementById(cMenuID).cloneNode(true))
	}else{
		menuobj.appendChild(cMenuID.cloneNode(true))
		cMenuID = cMenuID.id
	}	
	menuobj.firstChild.id = cMenuID + '_ContextMenu'
	menuobj.firstChild.style.display = '';
	menuobj.style.width=(typeof optWidth!="undefined")? optWidth : defaultMenuWidth
	menuobj.contentwidth=menuobj.offsetWidth
	menuobj.contentheight=menuobj.offsetHeight;
	if(typeof optWidth != "undefined"){
		menuobj.className=cClassName;
	}
	eventX=bIe5? event.clientX : e.clientX
	eventY=bIe5? event.clientY : e.clientY
	var arrWindowSize = getWindowSize();
	var nClientWidth = arrWindowSize[0];
	var nClientHeight = arrWindowSize[1];

	var rightedge=nClientWidth-eventX
	var bottomedge=nClientHeight-eventY
	//if the horizontal distance isn't enough to accomodate the width of the context menu.

	if (rightedge<menuobj.contentwidth)
		menuobj.style.left=bIe5? iecompattest().scrollLeft+eventX-menuobj.contentwidth+"px" : window.pageXOffset+eventX-menuobj.contentwidth+"px"
	else
		menuobj.style.left=bIe5? iecompattest().scrollLeft+(eventX+10)+"px" : window.pageXOffset+(eventX+10)+"px"
	
	if (bottomedge<menuobj.contentheight)
		menuobj.style.top=bIe5? iecompattest().scrollTop+eventY-menuobj.contentheight+"px" : window.pageYOffset+eventY-menuobj.contentheight+"px"
	else
		menuobj.style.top=bIe5? iecompattest().scrollTop+eventY+"px" : window.pageYOffset+eventY+"px"

	if(document.all && !window.opera && typeof document.body.style.maxHeight == "undefined"){
		appendIframeShim(menuobj)
	}
	if(typeof(fnPreDisplay) != 'undefined'){
		fnPreDisplay(menuobj,eventX,eventY);
		positionIframeShim(document.getElementById(menuobj.id + '_iframe'))
	}
	menuobj.style.visibility="visible"
	return false
}

function removeIframeShim(menuobj){
	if(document.getElementById(menuobj.id + '_iframe')){
		var oiFrame = document.getElementById(menuobj.id + '_iframe');
		var oParent = oiFrame.parentNode;
		oParent.removeChild(oiFrame);
	}
}
function positionIframeShim(oSpan){
	if(oSpan == null)
		return false;
	var oIframe = oSpan.firstChild;
	oIframe.style.width = menuobj.offsetWidth + 'px';
	oIframe.style.height =menuobj.offsetHeight + 'px';
	oIframe.style.margin ='0px';
	oIframe.style.position = 'absolute';
	oIframe.style.zIndex = '4';
	oIframe.style.top = menuobj.style.top;
	oIframe.style.left = menuobj.style.left;
	oIframe.src="javascript:false;"
	oIframe.frameBorder="0";
	oIframe.scrolling="no";	
}

function appendIframeShim(menuobj){
	removeIframeShim(menuobj)
	var oSpan = document.createElement('SPAN');
	var oIframe = document.createElement('IFRAME');
	oSpan.id = menuobj.id + '_iframe';
	oSpan.appendChild(oIframe);
	positionIframeShim(oSpan);		
	oSpan.style.position = 'relative';
	oSpan.style.zIndex = '3';	
	menuobj.parentNode.appendChild(oSpan);
}
function contains_ns6(a, b) {
while (b.parentNode)
	if ((b = b.parentNode) == a)
		return true;
return false;
}

function hidemenu(){
	if (window.menuobj){
		if(menuobj.style){
			contextMenuPreHide(menuobj);
			removeIframeShim(menuobj)		
			menuobj.style.visibility="hidden"
		}
	}
}

function dynamichide(e){
if (bIe5&&!menuobj.contains(e.toElement))
	hidemenu()
else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
	hidemenu()
}

function delayhidemenu(){
	delayhide=setTimeout("hidemenu()",500)
}

function clearhidemenu(){
	if (window.delayhide)
	clearTimeout(delayhide)
}

if (bIe5||ns6)
document.onclick=hidemenu
