var POPUPMENU_DEFAULTTIMEOUT = 500;

function visiblePopupMenu(id) {
  if (document && document.getElementById) {
    popupMenu = document.getElementById(id);
    if (popupMenu) {
		var dps = popupMenu.style.display;
		if(dps == "block") {
			bVisible = true;
		} else {
			bVisible = false;
		}
	  return bVisible;
    }
  }
}

function showPopupMenu(id) {
  if (document && document.getElementById) {
    popupMenu = document.getElementById(id);
    if (popupMenu) {
      popupMenu.style.display = "block";
      popupMenuPoppedUp[id] = true;
    }
  }
}

function hidePopupMenu(id) {
  if (document && document.getElementById) {
    popupMenu = document.getElementById(id);
    if (popupMenu) {
      popupMenu.style.display = "none";
      popupMenuTimeouts[id] = null;
      popupMenuPoppedUp[id] = false;
    }
  }
}

function fadePopupMenu(id, timeout) {
  if (timeout == null)
    timeout = POPUPMENU_DEFAULTTIMEOUT;

  oldClockId = popupMenuTimeouts[id];
  if (oldClockId != null)
    clearTimeout(oldClockId);
  clockId = setTimeout("hidePopupMenu('" + id + "')", timeout);
  popupMenuTimeouts[id] = clockId;
}

function popupPopupMenu(id) {
  // Mark this popup menu as popped up
  popupMenuPoppedUp[id] = true;

  // remove old timeouts for this menu
  clockId = popupMenuTimeouts[id];
  if (clockId != null) {
    clearTimeout(clockId);
    popupMenuTimeouts[id] = null;
  }

  // immediately hide any other menus that have timeouts set (and clear their timeouts)
  for (key in popupMenuTimeouts) {
    if (key != id) {
      clockId = popupMenuTimeouts[key];
      clearTimeout(clockId);
      popupMenuTimeouts[id] = null;
      popupMenuPoppedUp[id] = false;
      hidePopupMenu(key);
    }
  }

  // hide any menus that had been popped up, but not faded yet
  for (key in popupMenuPoppedUp) {
    if ((key != id) && (popupMenuPoppedUp[key] == true)) {
      hidePopupMenu(key);
      popupMenuPoppedUp[key] = false;
    }
  }

  if (! visiblePopupMenu(id)) {
    showPopupMenu(id);
  }
}

// Object used as associative array. 
// See this page why you should not use an Array as associative array:
// http://www.andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful
var popupMenuTimeouts = new Object();
var popupMenuPoppedUp = new Object();
