﻿	var popUp, mObj = null;
	var mColor = "#eeeeee", mStatus = "&nbsp;", sgnSysMenu = false, sgnCN = false;

	function evtPopMenu(iFrame, iMenucontent, iEvtclick) {
		if (iFrame.event) {
			var mnuHeight	= (sgnCN ? 21 : 20);
			var mnuWidth	= 6;
			var iWidth	= 0;
			var iHeight	= 0;
			var iMaxlen	= 0;
			var iObj		= iFrame.event.srcElement;
			var ix		= (!sgnSysMenu ? (iFrame.event.offsetX + 2) : 0);
			var iy		= (!sgnSysMenu ? (iFrame.event.offsetY + 2) : iObj.offsetHeight);
			var iData	= 0;

			hidePopup();
			popUp = window.createPopup();
			var oPopBody = popUp.document.body;
			oPopBody.innerHTML = iMenucontent;

			// Assign events to each of the children in the pop-up window, then show the custom elements in the pop-up window
			for(j = 0; j < oPopBody.all.tags("td").length; j++) {
				// Attach events to the children of the menu
				var std = oPopBody.all.tags("td")[j];
				var iLen = 0;
				if (std.id) {
					iLen = evtGetLength(std.innerText);
					iMaxlen = (iLen > iMaxlen ? iLen : iMaxlen);
					std.onmouseover = evtMouseOver;
					std.onmouseout = evtMouseOut;
					std.onclick = iEvtclick;
				} else {
					iData += 14;
				}
			}

			iWidth = iMaxlen * mnuWidth;
			iWidth = (iWidth > 100 ? iWidth : 100) + 20 + 20;
			iHeight = oPopBody.all.tags("td").length * mnuHeight - iData + 4;

			// Define the popUp's style
			oPopBody.style.borderLeft = "2 outset #ffffff";
			oPopBody.style.borderTop = "2 outset #ffffff";
			oPopBody.style.borderRight = "2 ridge #808080";
			oPopBody.style.borderBottom = "2 ridge #808080";
			oPopBody.style.position = "absolute";
			oPopBody.style.backgroundColor = mColor;

			// Call the hidePop function upon onmouseleave firing
			oPopBody.onmouseleave = hidePopup;

			oPopBody.onselectstart = evtSelectStart;
			oPopBody.oncontextmenu = evtSelectStart;

			// Show the popUp using the show method
			popUp.show(ix , iy, iWidth, iHeight, iObj);
		}
	}

	function hidePopup(){
		if(popUp)
			if(popUp.isOpen)
				popUp.hide();
		if (mObj && sgnSysMenu) {
			evtMnuMouseOut(mObj);
			mObj = null;
		}
	}

	function evtMouseOver() {
		// Cancel the event so that it does not bubble up to its parent
		popUp.document.parentWindow.event.cancelBubble = true;

		// Retrieve the source element from the pop-up window which fired the event
		var sObj = popUp.document.parentWindow.event.srcElement;
		if (sObj.id == "") sObj = sObj.parentElement;

		// Define the current element's style
		sObj.style.color = "#ffffff"; 
		sObj.style.backgroundColor = "#000066";

		update_status(sObj.innerHTML);
	}

	function evtMouseOut() {
		// Cancel the event so that it does not bubble up to its parent
		popUp.document.parentWindow.event.cancelBubble = true;

		// Retrieve the source element from the pop-up window which fired the event
		var sObj = popUp.document.parentWindow.event.srcElement;
		if (sObj.id == "") sObj = sObj.parentElement;

		// Define the current element's style
		sObj.style.color = "#000000"; 
		sObj.style.backgroundColor = mColor;

		update_status(mStatus);
	}

	function evtSelectStart() {
		return false;
	}

	function evtGetLength(iStr) {
		var iLen = 0;
		for (var i=0; i<iStr.length; i++) {
			iLen += (iStr.charCodeAt(i) < 0 || iStr.charCodeAt(i) > 126 ? 2 : 1);
		}
		return iLen;
	}

	function update_status(sMsg) {
		try {
			mBottom.stStatus.innerHTML = sMsg;
		} catch (e) {
			// do nothing
		}
	}

	function evtTrim(iStr) {
		while (iStr.length > 0) {
			if (iStr.substr(0, 1) != " " && iStr.substr(iStr.length-1, 1) != " ") return iStr;
			if (iStr.substr(0, 1) == " ") iStr = iStr.substr(1, iStr.length - 1);
			if (iStr.substr(iStr.length - 1, 1) == " ") iStr = iStr.substr(0, iStr.length-1);
		}
	}
