// JavaScript Document

// promenne urcujici graficky styl menu
var timeOut = 500;	    // doba, po ktere se skryje vysunute menu
var activeMenu = null;  // vybrane menu

var to = 0;  // identifikator timeoutu

// vytvoreni noveho menu
function Menu(label, width, x, y) {
  // identifikace menu podle nazvu
  this.label = label;
  // prirazeni sirky a pozice menu
  this.width = width;
  this.x = x;
  this.y = y;
  
  // vytvorime pole pro menu, pokud je jeste nemame
  if (!window.menus) window.menus = new Array()
  // pridame menu do pole
  window.menus[label] = this;
  window.menus[window.menus.length] = this;
  
  // pole pro polozky menu a odkazy jednotlivych polozek
  this.items = new Array();
  this.locations = new Array();

  // nastaveni funkce pro pridani polozky menu
  this.addMenuItem = addMenuItem;
}

// pridani polozky do menu
function addMenuItem(label, location) {
	this.items[this.items.length] = label;
	this.locations[this.locations.length] = location;
}

// vypsani/vytvoreni menu
function writeMenus() {
  // konec, pokud neni zadne menu definovano	
  if (!window.menus) return;
  // jinak projdi vsechna menu a vytvor je
  for (i=0; i<window.menus.length; i++) {
    document.writeln("<div id=" + window.menus[i].label + " class='submenu' "
					 + "style='top: " + window.menus[i].y + "px; left: " + window.menus[i].x + "px; width: "
					 + window.menus[i].width + "px; visibility: hidden;' "
					 + "onMouseOver='menuOver(window.menus[" + i + "]);' onMouseOut='menuOut(window.menus[" + i + "]);'>");
    for (j=0; j<window.menus[i].items.length; j++)
      document.writeln("<a href='" + window.menus[i].locations[j] + "'>"
					   + window.menus[i].items[j] + "</a>"); 
    document.writeln("</div>");
  }
}

// nalezeni prvku stranky s danym ID
function findItem(itemID) {
  if (document.all) 
    return document.all[itemID];
  if (document.getElementById)
    return document.getElementById(itemID);
  return false;
}

function menuOver(menu) {
  if (findItem(menu.label).style.visibility == 'visible') {
    activeMenu = menu;
	clearTimeout(to);
  }
}

function menuOut(menu) {
  hideMenu(menu);
}

// zobrazeni menu
function showMenu(menuID) {
  if (activeMenu)
    hideMenuInterval(activeMenu.label);  	
  menuItem = findItem(menuID);
  if (menuItem) 
    menuItem.style.visibility = 'visible';
  activeMenu = window.menus[menuID];
}

// skryti menu
function hideMenu(menu) {
  clearTimeout(to);
  to = setTimeout("hideMenuInterval('" + menu.label +"')", timeOut); 
}

function hideMenuInterval(menuLabel) {
  menuItem = findItem(menuLabel);
  if (menuItem)
    menuItem.style.visibility = 'hidden';
  activeMenu = null;
  clearTimeout(to);
}