// requires element_positioning.js


/***************************************************
By Bevan Wishart 2007

	DropDown Menu System.
	
	Use: 
	put a hidden div in a table cell.
	the hidden div will be the drop down menu
	make the mouseover on the table cell to displayRightMenuObj(this)
	
	
	
***************************************************/




//============================================================
//	Display Sub Menus                  
//============================================================	
function displayRightMenuObj (leftanchorObj) {

	//Before we open a new supmenu, ensure the last one is closed
	closeSubMenus();

	//find the first DIV with leftanchorObj - this SHOULD be the dropdownMenu.
	for (i=0; i < leftanchorObj.childNodes.length; i++){
		if (leftanchorObj.childNodes[i].nodeName == 'DIV') submenu = leftanchorObj.childNodes[i];
	}

	//set Global var activeSub so we can hide it (access from closeSubMenus()).
	activeSub = submenu;


	//Create a menucover and a hidedropdown.
	if (document.getElementById('MnuCover') == null) {
		creatediv('MnuCover', '<img id="MnuCoverImg" src="images/transparent.gif">', '0', '0', '0', '0', '2'); //Programmatically create the MnuCover Div
		mnuCover = document.getElementById('MnuCover');	
		mnuCoverImg = document.getElementById('MnuCoverImg');
	}

	if (document.getElementById('HideDropDown') == null) {
		creatediv('HideDropDown', '<img id="HideDropDownImg" src="images/transparent.gif" onMouseOver="closeSubMenus()">', '0', '0', '0', '0', '1'); //Programmatically create the MnuCover Div
		hideDropDown = document.getElementById('HideDropDown');
		hideDropDownImg = document.getElementById('HideDropDownImg');
	}

	//if the drop down menu is not already display, go through the process of display it
	if (submenu.style.visibility != 'visible') {

		anchorRightObj(leftanchorObj, submenu); //Anchor submenu to the item specified by leftanchor, usually a table cell
		submenu.style.visibility='visible'; // show the submenu
		
		anchorLeftObj(leftanchorObj, mnuCover);  //cover the original link with a transparent gif avoiding triggering hidedropdowns mouseover event and closing the menu
		mnuCover.style.visibility='visible';  //show the menucover
		mnuCoverImg.style.width = leftanchorObj.offsetWidth + 'px'; //make the menucover cover the whole element
		mnuCoverImg.style.height = leftanchorObj.offsetHeight + 'px';
	
		hideDropDown.style.visibility='visible'; //Display the hidedropdown div (covers the whole screen bar the submenu (by z-index)) and can trigger a mouseover (simulating a mouseout event)
		hideDropDown.style.top =  '0px'; //position in the top left corner
		hideDropDown.style.left =  '0px';
		hideDropDownImg.style.width = findPageDimensions('width') +'px'; //cover the whole viewport.
		hideDropDownImg.style.height = findPageDimensions('height') +'px';
		
	}
}

//================================================================
// Position menu to top right of element
//================================================================
function anchorRightObj(sourceObj, targetObj) {

	if (document.getElementById('HideDropDown') == null) {
		//creatediv('HideDropDown', 'sourceObj: ', '0', '0', 50, 40, 1);
	}

		//targetObj = document.getElementById(target);
		//sourceObj = document.getElementById(source);

        // Get the Left Pixel Space
    	leftpos = findPos(sourceObj, 'left');
		//set leftpos to the position 
		leftpos = parseInt(leftpos) + sourceObj.offsetWidth - 5;
		toppos = findPos(sourceObj, 'top');
	
	
	
		// Apply Left Pixel Space
		targetObj.style.left = leftpos + "px";
		//targetObj.style.minWidth = sourceObj.offsetWidth - 2 + 'px';
		if (targetObj.id != 'MnuCover') {
			targetObj.style.top = parseInt(toppos) + 'px';
		} else {
			targetObj.style.top = parseInt(toppos) + 'px';
		}
		targetObj.style.minHeight = '20px';


};


//================================================================
// Position menu to top left of element
//================================================================
function anchorLeftObj(sourceObj, targetObj) {

		//targetObj = document.getElementById(target);

        // Get the Left Pixel Space
    	leftpos = findPos(sourceObj, 'left');
		//set leftpos to the position 
		leftpos = parseInt(leftpos) + 2;
		toppos = findPos(sourceObj, 'top');
			
		// Apply Left Pixel Space
		targetObj.style.left = leftpos + "px";
		targetObj.style.minWidth = sourceObj.offsetWidth - 2 + 'px';
		if (targetObj.id != 'MnuCover') {
			targetObj.style.top = parseInt(toppos) + 'px';
		} else {
			targetObj.style.top = parseInt(toppos) + 'px';
		}
		
};

//============================================================
//Close all the sub menus
//============================================================
function closeSubMenus () {

		//Hide the submenu
		if (typeof(activeSub) != 'undefined') activeSub.style.visibility = 'hidden';
		if (typeof(mnuCover) != 'undefined') mnuCover.style.visibility = 'hidden';
		if (typeof(hideDropDown) != 'undefined') hideDropDown.style.visibility = 'hidden';

}
