// ************************************************************************ 
// Menu
// a class for popup menus
// ************************************************************************

function Menu(nMenuWidth, nItemPadding){
	this.constructor.nObjectCount++;

	// ************************************************************************ 
	// PRIVATE VARIABLES AND FUNCTIONS 
	// ONLY PRIVELEGED METHODS MAY VIEW/EDIT/INVOKE 
	// ************************************************************************
	var	aMenuItems = new Array();
	var nItemHeight = 21;
	var nTotalHeight = 0;
	
	function createMenu(){
		var oDiv = document.createElement('div');
		var oShim = document.createElement("iframe");
		
		nTotalHeight = 0;
		
		oDiv.setAttribute('id','MenuPopup');
		oDiv.className = 'popupMenuContainer';
		document.getElementsByTagName("body")[0].appendChild(oDiv);
		
		for(var i = 0;i < aMenuItems.length;i++){
			oDiv = document.createElement('div');
			if(nItemPadding > 0){
				oDiv.style.padding = nItemPadding.toString() + 'px';
			}
			if(aMenuItems[i][2] == 1){
				oDiv.className = 'content popupMenuItem';
				oDiv.innerHTML = aMenuItems[i][0];
				//oDiv.onmouseover = function(){this.className='subHeaderSolid popupMenuItem'};
				//oDiv.onmouseout = function(){this.className='content popupMenuItem'};
				oDiv.onmouseup = new Function(aMenuItems[i][1] + ';closeMenu();');
				if(aMenuItems[i][3]){
					//indent item
					oDiv.style.marginLeft = '14px';
				}
				nTotalHeight += nItemHeight + (nItemPadding * 2);
			}else if(aMenuItems[i][2] == 2){
				oDiv.className = 'content popupMenuItem';
				oDiv.innerHTML = aMenuItems[i][0];
				oDiv.style.cursor = 'default';
				nTotalHeight += nItemHeight + (nItemPadding * 2);
			}else{
				oDiv.className = 'dividerLight';
				oDiv.style.margin = '4px';
				oDiv.style.cursor = 'default';
				nTotalHeight += 9; //1 for the div height, 4 * 2 (margin heights);
			}
			document.getElementById('MenuPopup').appendChild(oDiv);
		}
		
		if(nItemPadding == 0){
			nTotalHeight += 3; //add some extra bottom padding to the box 
		}else{
			nTotalHeight += 3;
		}
						
		//create an iframe background
		oShim.src="javascript:''";
		oShim.style.position = 'absolute';
		oShim.id="MenuPopupShim";
		oShim.frameBorder = 0;
		oShim.scrolling = "no";
		oShim.style.display="block";
		document.getElementsByTagName("body")[0].appendChild(oShim);
	}

	// ************************************************************************ 
	// PRIVILEGED METHODS 
	// MAY BE INVOKED PUBLICLY AND MAY ACCESS PRIVATE ITEMS 
	// ************************************************************************ 
	this.addItem = function(sText, sFunction, bIndent){
		var nIdx = aMenuItems.length;
		aMenuItems[nIdx] = new Array();
		aMenuItems[nIdx][0] = sText;
		aMenuItems[nIdx][1] = sFunction;
		aMenuItems[nIdx][2] = 1;//(1 = item, 2 = header, 3 = divider)
		if(typeof bIndent == "undefined"){
			aMenuItems[nIdx][3] = false;//indent item
		}else{
			aMenuItems[nIdx][3] = bIndent;//indent item
		}
	}
	
	this.addHeader = function(sText){
		var nIdx = aMenuItems.length;
		aMenuItems[nIdx] = new Array();
		aMenuItems[nIdx][0] = sText;
		aMenuItems[nIdx][1] = '';
		aMenuItems[nIdx][2] = 2;//(1 = item, 2 = header, 3 = divider)
		aMenuItems[nIdx][3] = false;//indent item
	}
	
	this.addDivider = function(){
		var nIdx = aMenuItems.length;
		aMenuItems[nIdx] = new Array();
		aMenuItems[nIdx][0] = '';
		aMenuItems[nIdx][1] = '';
		aMenuItems[nIdx][2] = 3;//(1 = item, 2 = header, 3 = divider)
		aMenuItems[nIdx][3] = false;//indent item
	}

	this.openMenu = function(oClickEvent){
		var nRight = document.body.clientWidth - oClickEvent.clientX;
		var nBottom = document.body.clientHeight - oClickEvent.clientY;
		var nClickOffset = -2;
		var oContentDiv = '';
		var oShim = '';
		
		document.body.onmouseup = new Function("if(document.getElementById('MenuPopup'))closeMenu()");
		
		createMenu();
		
		oContentDiv = document.getElementById('MenuPopup');
		oContentDiv.style.display = "block";
		oContentDiv.style.width = nMenuWidth;
		//oContentDiv.style.height = nTotalHeight;

		oShim = document.getElementById('MenuPopupShim');
		
		if (nRight < oContentDiv.offsetWidth){
			oContentDiv.style.left = document.body.scrollLeft + oClickEvent.clientX - oContentDiv.offsetWidth + nClickOffset;
		}else{
			oContentDiv.style.left = document.body.scrollLeft + oClickEvent.clientX + nClickOffset;
		}
		
		if (nBottom < oContentDiv.offsetHeight){
			oContentDiv.style.top = document.body.scrollTop + oClickEvent.clientY - oContentDiv.offsetHeight + nClickOffset;
		}else{
			oContentDiv.style.top = document.body.scrollTop + oClickEvent.clientY + nClickOffset;
		}
		
		// move box to the left if out of screen bounds on right
		if((document.body.clientWidth - oContentDiv.style.width) <  oContentDiv.style.left.substring(0, oContentDiv.style.left.length - 2)){
			oContentDiv.style.left =  (oContentDiv.style.left.substring(0, oContentDiv.style.left.length - 2) - oContentDiv.style.width);
		}

		// move box to the top if out of screen bounds on bottom
		if((document.body.clientHeight - oContentDiv.style.height) <  oContentDiv.style.top.substring(0, oContentDiv.style.top.length - 2)){
			oContentDiv.style.top =  (oContentDiv.style.top.substring(0, oContentDiv.style.top.length - 2) - oContentDiv.style.height);
		}
		
		oShim.style.width = nMenuWidth;
		oShim.style.top = oContentDiv.style.top;
		oShim.style.left = oContentDiv.style.left;
		oShim.style.height = oContentDiv.offsetHeight;
		oShim.style.zIndex = oContentDiv.zIndex;
	}
}

// ************************************************************************ 
// PUBLIC METHODS -- ANYONE MAY READ/WRITE 
// ************************************************************************ 
nMenuClick = 0;

function closeMenu(){
	document.getElementsByTagName("body")[0].removeChild(document.getElementById('MenuPopup'));
	document.getElementsByTagName("body")[0].removeChild(document.getElementById('MenuPopupShim'));
	document.body.onmouseup = function(){};
	nMenuClick = 0;
}
	
// ************************************************************************ 
// STATIC PROPERTIES -- ANYONE MAY READ/WRITE 
// ************************************************************************ 
Menu.nObjectCount = 0;
