/* 
	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";
		}

		// 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;
		
	} else {
		// close drop down menu
		ddMenu.style.display = "none";
	}
}

function clearDDMenu() {
	if (openedMenu && !justOpened) {
		document.getElementById(openedMenu).style.display = "none";
	}
	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";	
	}	
}	

