/** Spouští proces vykreslení menu. */
function startup() {
  renderMenu();
  positionMenu();
}

/* Vykreslí položky všech menu. */
function renderMenu() {
  var html = "";
  for (i = 0; i < menu.length; i++) {
    html = "<table class='menu' id='menu" + i + "' border='1'>";
    for (j = 0; j < menu[i].length; j++) {
      html += "<tr><td class='menuitem' id='menu" + i + "_" + j + "' style=\"background-color: " + menuBckgDim + "\" width='" + eval("menu" + i + "Width") + "' onmouseover=\"highlightItem(this); setOn(this);\" onmouseout=\"dimItem(this); setOff(this); hideMenu(" + i + ");\" onclick=\"location.href='" + menu[i][j][2] + "'\">" + menu[i][j][1] + "</td></tr>";
    }
    html += "</table>"; 
    document.write(html);
  }
}

/* Rozmístí příslušně položky menu. */
function positionMenu() {
  var left = 0;
  var top = 0;
  for (i = 0; i < menu.length; i++) {
    left = 0;
    top = 0;
    document.getElementById("menu" + i).style.left = eval("menu" + i + "Left");
    document.getElementById("menu" + i).style.top = eval("menu" + i + "Top");
    for (j = 0; j < menu[i].length; j++) {
      document.getElementById("menu" + i + "_" + j).style.left = left;
      document.getElementById("menu" + i + "_" + j).style.top = top;
      top += eval("menu" + i + "Height");
    }  
  }
}

/* Nastaví položky menu s indexem elm na off, tj. připraví je ke skrytí. */
function setOff(elm) {
  for (i = 0; i < menu.length; i++) {
    for (j = 0; j < menu[i].length; j++) {
      if (elm.id == "menu" + i + "_" + j) menu[i][j][0] = "off";
    }
  }
}

/* Nastaví položky menu s indexem elm na on, tj. zakáže jejich skrytí. */
function setOn(elm){ 
  for (i = 0; i < menu.length; i++) {
    for (j = 0; j < menu[i].length; j++) {
      if (elm.id == "menu" + i + "_" + j) menu[i][j][0] = "on";
    }
  }
}

/* Nastaví časovač, který opakovaně zkouší skrýt menu. Skryje pouze to menu, které 
   má všechny položky nastaveny na off. */
function hideMenu(elmIndex) {
  setTimeout("hide(" + elmIndex + ")", 00);
}

/* Skryje menus daným indexem, a to jen pokud jsou všechny jeho položky nastaveny na off. */
function hide(elmIndex) {
  var hideMenu = true;
  for (j = 0; j < menu[elmIndex].length; j++) {
    if (menu[elmIndex][j][0] == "on") hideMenu = false; 
  }
  if (hideMenu) document.getElementById("menu" + elmIndex).style.display = 'none';
}

/* Podsvítí položku menu. */
function highlightItem(elm) {
  elm.style.backgroundColor = menuBckgHighlight;
}

/* Odpodsvítí položku menu. */
function dimItem(elm) {
  elm.style.backgroundColor = menuBckgDim;
}

startup(); 

