function menu_init() {
  var all_elements = document.getElementsByTagName('*');
  for (this_element in all_elements) {
    o = all_elements[this_element];
    if (o && o.id) {

      // init all tables in the "submenu_" class
      if ((o.tagName == 'TABLE') && (o.id.substring(0, 8)=='submenu_')) {
        o.onmouseover = function (){ stop_timer(); }
        o.onmouseout  = function (){ start_timer(); }
        table_rows = o.getElementsByTagName('TR');
        for (this_row in table_rows) {
          table_rows[this_row].onmouseover = function (){ this.className = 'over'; }
          table_rows[this_row].onmouseout  = function (){ this.className = ''; }
        }
      }


      if (o.id.substring(0, 14) == 'menu_rollover_') {
        for (j=0; j<3; j++) {
          window['preloaded_image_' + o.id.substring(14) + j] = new Image();
          window['preloaded_image_' + o.id.substring(14) + j].src = 'images/' + o.id.substring(14) + '_' + j + '.gif';
        }
        eval('o.onmouseover = function (){ change_image(\''+o.id.substring(14)+'\',1); show_submenu(\''+o.id.substring(14)+'\', 1, 0); }');
        eval('o.onmouseout  = function (){ change_image(\''+o.id.substring(14)+'\',0); start_timer(); }');
        eval('o.onmousedown = function (){ change_image(\''+o.id.substring(14)+'\',2); }');
        eval('o.onmouseup   = function (){ change_image(\''+o.id.substring(14)+'\',1); }');
      }
      else if (o.id.substring(0, 5) == 'menu_') {
        eval('o.onmouseover = function (){ show_submenu(\''+o.id.substring(5)+'\', 0); }');
        eval('o.onmouseout  = function (){ start_timer(); }');
      }
    }
  }
}

function change_image(menu_id, position_num) {
  document.getElementById('menu_rollover_' + menu_id).src = eval('preloaded_image_' + menu_id + position_num + '.src');
  return false;
}

function show_submenu(menu_id, is_rollover, is_vertical) {
  stop_timer();
  hide_submenu();
  submenu = document.getElementById('submenu_' + menu_id);
  if (!submenu) return;

  var launchpoint;
  if (is_rollover) launchpoint = document.getElementById('menu_rollover_' + menu_id);
  else launchpoint = document.getElementById('menu_' + menu_id);
  var pos = get_launchpoint_position(launchpoint);

  if (is_vertical) {
    submenu.style.left = pos[0] + launchpoint.offsetWidth + 'px';
    submenu.style.top  = pos[1] + 'px';
  }
  else {
    submenu.style.left = pos[0] + 'px';
    submenu.style.top  = pos[1] + launchpoint.offsetHeight + 'px';
    if (submenu.offsetWidth < launchpoint.offsetWidth) submenu.style.width = launchpoint.offsetWidth + 'px';
  }

  submenu.style.visibility = 'visible';
}

function hide_submenu() {
  if (submenu) {
    if (submenu_timer) clearTimeout(submenu_timer);
    submenu.style.visibility = 'hidden';
    submenu = null;
  }
}

function start_timer() {
  if (submenu) submenu_timer = setTimeout('hide_submenu()', 250);
}

function stop_timer() {
  if (submenu_timer) submenu_timer = clearTimeout(submenu_timer);
}

function get_launchpoint_position(launchpoint_id) {
  var l=0, t=0;
  while (launchpoint_id) {
    l += launchpoint_id.offsetLeft;
    t += launchpoint_id.offsetTop;
    launchpoint_id = launchpoint_id.offsetParent;
  }
  return [l, t];
}

var submenu = null;
var submenu_timer = null;
menu_init();
