/* 
	we only want one drop down open at a time- openedMenu contains the id of 
	the currently opened menu which we use to close it if the user opens another 
*/
var openedMenu = "";
var justOpened = "";

// opens and closes options and action bar drop down menus
function toggleDDMenu(ddMenuID) {
	var ddMenu = document.getElementById(ddMenuID);
		
	if (ddMenu.style.display == "" || ddMenu.style.display == "none") {
		// open drop down menu
		ddMenu.style.display = "block";
		
		// check to see if another action bar menu is open
		// if so, close it
		if(openedMenu != "" && openedMenu != ddMenuID) {
			var closeThisMenu = document.getElementById(openedMenu);
			closeThisMenu.style.display = "none";
			toggleIFrameMask(openedMenu, false);

		}

		// set openMenu equal to ddMenuID
		openedMenu = ddMenuID;

		//if search menu is opened set focus on search input field
		if(ddMenuID == "ddMenu-search-actionbar") {
			var searchField = document.getElementById("q01");
			searchField.focus();
		}

		// add handler to body tag so that clicking outside the menus clears them
		if (!document.body.getAttribute("onClick")) {
			document.body.setAttribute("onClick","clearDDMenu()");
		}

		// prevent the click that opens the menu from bubbling up
		// to the body and closing it right away...
		justOpened = true;
		
		// toggle the Iframe mask element to keep select boxes behind the dropdown menu in IE6 - davidg
		toggleIFrameMask(ddMenuID, true);
		
	} else {
		// close drop down menu
		ddMenu.style.display = "none";
		toggleIFrameMask(ddMenuID, false);
	}
}

function clearDDMenu() {
	if (openedMenu && !justOpened) {
		document.getElementById(openedMenu).style.display = "none";
		toggleIFrameMask(openedMenu, false);
	}
	justOpened = false;
}

function isMenuEmpty()
{
	/**
	* If the actions menu has no buttons, we need to hide it
	* So, check to see if the ul element containing the list has any child li nodes
	* If it has no li elements as children, hide the div containing the action drop down
	*/
	var emptyList = true;
	// actionDD is the id of the drop down- it is defined in elements/lw-ddMenu-actions.jsp
	
	for(var n = 0; n < actionDD.childNodes.length; n++)
	{
		if(actionDD.childNodes[n].nodeName == "LI" || actionDD.childNodes[n].nodeName == "li" ) {
			// if an li element is found, the actions menu is not empty
			emptyList = false;
			break;	// no need to continue checking
		}
	}
		
	if(emptyList == true)
	{
		// actionDiv is the id of the actions header- it is defined in elements/lw-ddMenu-actions.jsp
		actionDiv.style.display = "none";	
	}	
}	

function toggleIFrameMask(ID,show) {
  if (navigator.appName == 'Microsoft Internet Explorer') {

      var theboxhidername = "boxHide"+ID;
    // ID = the id of the dropdown element DIV (or UL)
    // show = boolean value to show or hide the dropdown
    //get the id of the element we want to turn on or off
	var subnavmenu = document.getElementById(ID);	//alert("on: "+ID+" "+show+ " "+ line);
	//if the menu is showing, create the box hider
    if (!show) {
    	if (document.getElementById(theboxhidername) ){
          var removeiFrame = document.getElementById(theboxhidername)
		  removeiFrame.style.display = "none";
		}
    }else{
        if(document.getElementById(theboxhidername)) {
		 var boxHideIframe = document.getElementById(theboxhidername);
		  boxHideIframe.style.display = "block";  
        }else{ //create one
		 var boxHideIframe = document.createElement("iframe");
		 boxHideIframe.setAttribute("id",theboxhidername);
		 boxHideIframe.setAttribute("src","about:blank");
		 boxHideIframe.setAttribute("scrolling","no");
		 boxHideIframe.setAttribute("frameBorder","0");
		 boxHideIframe.style.display = "block";
		 boxHideIframe.style.position = "absolute";
		 // append the iframe to the dropdown element's parent because the subnav has relative positioning
		 navmommy = subnavmenu.parentNode;
		 navmommy.appendChild(boxHideIframe);
		 boxHideIframe.style.top = subnavmenu.offsetTop;
		 boxHideIframe.style.left = subnavmenu.offsetLeft;
		 boxHideIframe.style.width = subnavmenu.offsetWidth;
		 boxHideIframe.style.height = subnavmenu.offsetHeight;
		 subnavmenu.style.zIndex = 100;
         boxHideIframe.style.zIndex = subnavmenu.style.zIndex -1;
     
       }
    }
  }
}
