// timeout before dropdown actually shows
var dropdownShowTimeout = null;
// timeout before dropdown closes on its own
var activeDropdownTimeout = null;
// timeout before dropdown submenu closes on its own
var submenuTimeout=null;
// the current active dropdown id
var currentActiveDropdown = "";
// the current active submenu group 
var currentActiveGroup="";
// currently highlighted dropdown title
var activeTop = "";
// current group id (dropdown name)
var currentGroupId = "";
// current group top title id
var curGroupTopId = "";

/** Sets visible the submenu group for the dropdown menu item, if any
	used by main php program
	@param group submenu group element id
*/
function setVisibleGroup(group) {
   getElementSafely(group).css.visibility="visible";
   currentActiveGroup=group;  
}

/** sets the element visible
	used in showDropdown() function to display current dropdown
	@param elId the element id attribute
*/
function setVisible(elId) {
  getElementSafely(elId).css.visibility="visible";
}

/** Hide the element (make it invisible)
	used in clearSubmenu() to hide current submenu group
	clearDropdown() and clearOtherDropdown() to hide current active dropdown
	@param elId element id attribute
*/
function doHide(elId) {
  getElementSafely(elId).css.visibility="hidden";
}

/** Highlights the current dropdown menu/submenu item
	used in the main php program
	@param tdId current menu item table cell id
	@param linkId current menu item link element id
*/
function doHiLite(tdId, linkId) {
	// set background color
  getElementSafely(tdId).css.backgroundColor="#00cc00";
	// set text color
  getElementSafely(linkId).css.color="#000000";
}

/** Un-highlights the current dropdown menu/submenu item
	used in the main php program
	@param tdId current menu item table cell id
	@param linkId current menu item link element id
*/
function undoHiLite(tdId,linkId) {
	// set background color
  getElementSafely(tdId).css.backgroundColor="#6633FF";
	// set text color
  getElementSafely(linkId).css.color="#FFFFFF";
}

/** Sets a timeout to clear the dropdown.
	Used in main program for dropdowns, submenu groups and top titles
	@param timeoutLength the number representing length of a timeout (1000 being one sec)
*/
 
function setDropdownTimeout(timeoutLength) {	
	activeDropdownTimeout = setTimeout("clearDropdown()", timeoutLength);
}

/** Sets the timeout before dropdown is actually shown
	to prevent from dropdowns showing if the user only goes briefly
	over the top menu items
	used in main php program on top items mouseover
	@param groupId the id attribute of the current dropdown
	@param groupTopId the id attribute of the current top item
	@param timeoutLength length of the timeout 
*/
function setDropdownShowTimeout(groupId, groupTopId, timeoutLength) {
	if(currentActiveDropdown!=groupId) {
		curGroupTopId=groupTopId;
		currentGroupId=groupId;
		dropdownShowTimeout = setTimeout("showDropdown()", timeoutLength);
	}
}

/** Sets the timeout before the submenu is cleared
	@param timeoutLength length of timeout
*/
function setSubmenuTimeout(timeoutLength) {
	submenuTimeout = setTimeout("clearSubmenu()", timeoutLength);
}

function clearSubmenu() {
	if(currentActiveGroup!="") {
		doHide(currentActiveGroup);
    currentActiveGroup="";
  }
}

function clearSubmenuTimeout() {
  if (submenuTimeout) {
		clearTimeout(submenuTimeout);
	}
	submenuTimeout = null;
}

/** Clears the dropdown once the dropdown timeout expires */
function clearDropdown() {
	if(currentActiveDropdown!="") {
		doHide(currentActiveDropdown);
		currentActiveDropdown="";
  }
  clearSubmenu();
	if(activeTop!="") {
		changeColor(activeTop, false);
	}
}

/** Show the dropdown once show dropdown timeout expires */
function showDropdown() {
	clearOtherDropdown(currentGroupId);
	changeColor(curGroupTopId, true);
	activeTop=curGroupTopId;	
	currentActiveDropdown=currentGroupId;
	setVisible(currentActiveDropdown);
}

function clearOtherDropdown(groupId) {
	if(currentActiveDropdown!="" && currentActiveDropdown!=groupId) {
		doHide(currentActiveDropdown);
		clearSubmenu();
		if(activeTop!="")
			changeColor(activeTop, false);
	}
}

function clearDropdownTimeout() {
	if (activeDropdownTimeout) {
		clearTimeout(activeDropdownTimeout);
	}
	activeDropdownTimeout = null;
}

function clearDropdownShowTimeout() {
	if (dropdownShowTimeout) {
		clearTimeout(dropdownShowTimeout);
	}
	dropdownShowTimeout = null;
}

/** Changes text color of the element, used for the top dropdown titles
	@param linkId id attribute of the top title link
	@param active true if change to active color false if change to inactive color
*/
function changeColor(linkId, active) {
	if(curTopMenuInactiveColor!=null && curTopMenuActiveColor!=null) {
		if(active) 
			getElementSafely(linkId).css.color=curTopMenuActiveColor;
		else
			getElementSafely(linkId).css.color=curTopMenuInactiveColor;
	}
}