// ************************************************************************ 
// 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','AppToolboxPopup');
		oDiv.className = 'popupMenuContainer';
		
		$('body').prepend(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('AppToolboxPopup').appendChild(oDiv);
		}
		
		if(nItemPadding == 0){
			nTotalHeight += 3; //add some extra bottom padding to the box 
		}else{
			nTotalHeight += 3;
		}
		
		$('AppToolboxPopup').css({
			position: 'absolute'
		});
		
		$('.popupMenuItem').hover(function(){
			$(this).find('*').andSelf().addClass('subHeaderSolid');			   
		}, function(){
			$(this).find('*').andSelf().removeClass('subHeaderSolid');	
		});
	}

	// ************************************************************************ 
	// 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 $shim;
		
		if($('#AppToolboxPopup').length == 0){
			document.body.onmouseup = new Function("if(document.getElementById('AppToolboxPopup'))closeMenu()");	
			createMenu();
		}

		$('#AppToolboxPopup')
		.css({
			left: $('#ApplicationToolbox').offset().left,
			top: $('#ApplicationToolbox').offset().top + $('#ApplicationToolbox').outerHeight() + 5
		})
		.width(nMenuWidth)
		.hide();
		
		$shim = $('<iframe id="AppToolBoxShim"></iframe>')
		$shim
		.width($('#AppToolboxPopup').outerWidth())
		.height($('#AppToolboxPopup').outerHeight())
		.attr('frameborder', '0')
		.css({
			position: 'absolute',
			left: $('#AppToolboxPopup').css('left'),
			top: $('#AppToolboxPopup').css('top'),
			opacity: 0
		});
		
		$('#AppToolboxPopup')
		.before($shim)
		.slideDown('fast', function(){if($.browser.msie){this.style.removeAttribute('filter')}});
	}
}

// ************************************************************************ 
// PUBLIC METHODS -- ANYONE MAY READ/WRITE 
// ************************************************************************ 
nMenuClick = 0;

function closeMenu(){
	$('#AppToolBoxShim').remove();
	document.getElementsByTagName("body")[0].removeChild(document.getElementById('AppToolboxPopup'));
	//document.getElementsByTagName("body")[0].removeChild(document.getElementById('AppToolboxPopupShim'));
	document.body.onmouseup = function(){};
	nMenuClick = 0;
}
	
// ************************************************************************ 
// STATIC PROPERTIES -- ANYONE MAY READ/WRITE 
// ************************************************************************ 
Menu.nObjectCount = 0;

