var POPUPMENU_DEFAULTTIMEOUT = 500;

function JMB_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=JMB_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function JMB_swapImage() { //v3.0
  var i,j=0,x,a=JMB_swapImage.arguments; document.JMB_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=JMB_findObj(a[i]))!=null){document.JMB_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function JMB_swapImgRestore() { //v3.0
  var i,x,a=document.JMB_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function JMB_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.JMB_p) d.JMB_p=new Array();
   var i,j=d.JMB_p.length,a=JMB_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.JMB_p[j]=new Image; d.JMB_p[j++].src=a[i];}}
}

function visiblePopupMenu(id) {
  if (document && document.getElementById) {
    popupMenu = document.getElementById(id);
    if (popupMenu) {
      return popupMenu.style.display != "none";
    }
  }
}

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();