// <script>

var currently_open = '';

function startmenu()
{
  var i = 1;
  var menuId = "menu" + i;
  var menuElem = document.getElementById(menuId);
  while (menuElem)
  {
    menuElem.style.display = "none";
    i = i + 1;
    menuId = "menu" + i;
    menuElem = document.getElementById(menuId);    
  }
}

function menufunc(menuId)
{
  if(document.getElementById(menuId).style.display == "none")
  {
    startmenu();
    document.getElementById(menuId).style.display = "block";
  }
  else
  {
    startmenu();
  }
}



// ----- Popup Control ---------------------------------------------------------

function pos_x(obj)
{
  var x = 0;
  if (obj)
  {
    if (obj.offsetParent)
    {
      while (obj.offsetParent)
      {
        x += obj.offsetLeft;
        obj = obj.offsetParent;
      }
    }
    else if (obj.x)
    {
      x += obj.x;
    }
  }
  return x;
}

function pos_y(obj)
{
  var y = 0;
  if (obj)
  {
    if (obj.offsetParent)
    {
      while (obj.offsetParent)
      {
        y += obj.offsetTop;
        obj = obj.offsetParent;
      }
    }
    else if (obj.y)
    {
      y += obj.y;
    }
  }
  return y;
}

function at_display(x)
{
  win = window.open();
  for (var i in x) win.document.write(i+' = '+x[i]+'<br>');
}

// ----- Show Aux -----

function at_show_aux(parent, child, container)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child);
  var box = document.getElementById(container);
  
  var left = 0;
  var top = p.offsetHeight + p.offsetTop;
  if (box)
    left = 0 - pos_x(box) + pos_x(p);
  else
    left = pos_x(p);

  // prevent last menu from going out of bounds...
  if (box && (left + c.offsetWidth > box.offsetWidth))
    left = left - ((left + c.offsetWidth) - box.offsetWidth);

  c.style.position   = "absolute";
  c.style.top        = top + "px";
  c.style.left       = left + "px";
  c.style.visibility = "visible";
  currently_open = child;
}

// ----- Show -----

function at_show()
{
  if (currently_open != this["at_child"] && currently_open != '') {
    document.getElementById(currently_open).style.visibility = 'hidden';
  }

  p = document.getElementById(this["at_parent"]);
  c = document.getElementById(this["at_child" ]);
  container = this["at_container"];
  if (container)
    box = document.getElementById(container);
  else
    box = null;

  if (box) {
    c["at_show_timeout"] = setTimeout("at_show_aux('"+p.id+"', '"+c.id+"', '"+box.id+"');", c["at_delay"]);
    //at_show_aux(p.id, c.id, box.id);
  } else {
    c["at_show_timeout"] = setTimeout("at_show_aux('"+p.id+"', '"+c.id+"');", c["at_delay"]);
    //at_show_aux(p.id, c.id);
  }

  clearTimeout(c["at_timeout"]);
}

// ----- Hide -----

function at_hide()
{
  c = document.getElementById(this["at_child"]);

  c["at_timeout"] = setTimeout("document.getElementById('"+c.id+"').style.visibility = 'hidden'", 1);
  clearTimeout(c["at_show_timeout"]);
}

// ----- Click -----

function at_click()
{
  p = document.getElementById(this["at_parent"]);
  c = document.getElementById(this["at_child"]);
  if (this["at_container"])
    box = document.getElementById(this["at_container"]);

  clearTimeout(c["at_show_timeout"]);


  if (c.style.visibility != "visible") {
    if (box)
      at_show_aux(p.id, c.id, box.id);
    else
      at_show_aux(p.id, c.id);
  } else { 
    c["at_timeout"] = setTimeout('c.style.visibility = "hidden"', 5);
  }

  return false;
}

// ----- Attach -----

// PARAMETERS:
// parent   - id of visible html element
// child    - id of invisible html element that will be dropdowned
// showtype - "click" = you should click the parent to show/hide the child
//            "hover" = you should place the mouse over the parent to show
//                      the child
// position - "x" = the child is displayed to the right of the parent
//            "y" = the child is displayed below the parent
// cursor   - Omit to use default cursor or check any CSS manual for possible
//            values of this field

function at_attach(parent, child, showtype, position, cursor, container, delay)
{
  p = document.getElementById(parent);
  c = document.getElementById(child);
  if (container)
    box = document.getElementById(container);
  else
    box = null;

  p["at_parent"]     = p.id;
  c["at_parent"]     = p.id;
  p["at_child"]      = c.id;
  c["at_child"]      = c.id;
  p["at_position"]   = position;
  c["at_position"]   = position;
  c["at_delay"]      = delay;
  if (box)
  {
    p["at_container"]  = box.id;
    c["at_container"]  = box.id;
  }

  c.style.position   = "absolute";
  c.style.visibility = "hidden";

  if (cursor != undefined) p.style.cursor = cursor;

  switch (showtype)
  {
    case "click":
      p.onclick     = at_click;
      p.onmouseout  = at_hide;
      c.onmouseover = at_show;
      c.onmouseout  = at_hide;
      break;
    case "hover":
      p.onmouseover = at_show;
      c.onmouseover = at_show;
      c.onmouseout  = at_hide;
//      p.onmouseout  = at_click;
      p.onmouseout  = at_hide;
      break;
  }
}


