var running    = false;
var timerID    = null;
var currentID  = null;
var previousID = null;
var delay      = 300;
var behaviors  = {};
function InitMenus(id) {
  // get all list elements
  $A($(id).childNodes).each(function(child) {
    if (child.nodeName == 'LI') {
      // is there a menu?
      if ($(child.id+'-menu')) {
        // set ids
        nav_id  = child.id;
        menu_id = child.id+'-menu';
        // build behaviors
        eval("behaviors['#"+nav_id+":mouseover']  = function(e) { ShowMenu('"+nav_id+"'); }");
        eval("behaviors['#"+nav_id+":mouseout']   = function(e) { StartTimer(\"HideMenu('"+nav_id+"')\"); }");
        eval("behaviors['#"+menu_id+":mouseover'] = function(e) { StopTimer(); }");
      }
    }
  });
  // add behaviors
  Event.addBehavior(behaviors);
}
function StartTimer(event) { running = true; timerID = setTimeout(event, delay); }
function StopTimer() { running = false; clearTimeout(timerID); }
function ShowMenu(id) {
  if (running){StopTimer();}
  currentID = id;
  if (currentID != previousID && previousID != null){ HideMenu(previousID); }
  $(id+'-menu').className = 'show';
	$A($(id).childNodes).each(function(child) { if (child.nodeName == 'A') { Element.addClassName(child, 'hover'); } });
  previousID = currentID;
}
function HideMenu(id, level) {
  $(id+'-menu').className = 'hide';
  $A($(id).childNodes).each(function(child) { if (child.nodeName == 'A') { Element.removeClassName(child, 'hover'); } });
}
