var isDOM = (document.getElementById ? true : false); 
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);

// --------------------
function getRef(id)
{
 if (isDOM) return document.getElementById(id);
 if (isIE4) return document.all[id];
 if (isNS4) return document.layers[id];
}

// --------------------
function getSty(id)
{
 return (isNS4 ? getRef(id) : getRef(id).style);
} 

// *** MOUSEOVER/OUT CONTROL FUNCTIONS ***

// Hide timeout.
var popTimer = 0;
// Array showing highlighted menu items.
var litNow = new Array();

// --------------------
function popOver(menuNum, itemNum)
{
 clearTimeout(popTimer);

 // Hide all other menus & dim old highlighted items, still showing this menu.
 hideAllBut(menuNum);

 // Get tree of parent menu items and light them up - global variable for hideAllBut!
 litNow = getTree(menuNum, itemNum);
 changeCol(litNow, true);

 // Get target menu to show - if it's nonzero, position & show it.
 targetNum = menu[menuNum][itemNum].target;
 if (targetNum > 0)
 {
  // Get current menu position.
  thisX = parseInt(menu[menuNum][0].ref.left) + parseInt(menu[menuNum][itemNum].ref.left);
  thisY = parseInt(menu[menuNum][0].ref.top) + parseInt(menu[menuNum][itemNum].ref.top);

  // Add those to the target's offset to set the target's position, show it.
  with (menu[targetNum][0].ref)
  {
   left = parseInt(thisX + menu[targetNum][0].x);
   top = parseInt(thisY + menu[targetNum][0].y);
   visibility = 'visible';
  }
 }
}

// --------------------
function popOut(menuNum, itemNum)
{
 // If it's a root menu item that doesn't trigger a popout, hide now, else set timeout
 // to hide menu in 1/2 sec, *UNLESS* another mouseover clears the timeout!
 if ((menuNum == 0) && !menu[menuNum][itemNum].target)
  hideAllBut(0)
 else
  popTimer = setTimeout('hideAllBut(0)', 500);
}

// --------------------
function getTree(menuNum, itemNum)
{
 // Array index is the menu number. The contents are null (if that menu is not a parent)
 // or the item number in that menu that is an ancestor (to light it up).
 itemArray = new Array(menu.length);

 while(1)
 {
  itemArray[menuNum] = itemNum;
  // If we've reached the top of the hierarchy, return.
  if (menuNum == 0) return itemArray;
  itemNum = menu[menuNum][0].parentItem;
  menuNum = menu[menuNum][0].parentMenu;
 }
}

// --------------------
// Pass an array and a boolean to specify colour change, true = over colour.
function changeCol(changeArray, isOver)
{
 // Cycle through array searching for items to change.
 for (menuCount = 0; menuCount < changeArray.length; menuCount++)
 {
  // If item number is present, change its colour.
  if (changeArray[menuCount])
  {
   newCol = isOver ? menu[menuCount][0].overCol : menu[menuCount][0].backCol;

   // Change the colours of the div/layer background.
   with (menu[menuCount][changeArray[menuCount]].ref)
   {
    if (isNS4) bgColor = newCol;
    else backgroundColor = newCol;
   }
  }
 }
}

// --------------------
function hideAllBut(menuNum)
{
 // Get array of parent menus (item numbers irrelevant, just pass '1').
 var keepMenus = getTree(menuNum, 1);

 // ...and work through it, hiding menus that are not its ancestors/itself.
 for (count = 0; count < menu.length; count++)
  if (!keepMenus[count])
   menu[count][0].ref.visibility = 'hidden';

 // Dim all the items in litNow array.
 changeCol(litNow, false);
}

// *** MENU CONSTRUCTION FUNCTIONS ***

// --------------------
function Menu(isVert, popInd, x, y, width, overCol, backCol, borderClass, textClass)
{
 // True or false - a vertical menu?
 this.isVert = isVert;
 // The popout indicator used (if any) for this menu.
 this.popInd = popInd
 // Position and size settings.
 this.x = x;
 this.y = y;
 this.width = width;
 // Colours of menu and items.
 this.overCol = overCol;
 this.backCol = backCol;
 // The stylesheet class used for item borders and the text within items.
 this.borderClass = borderClass;
 this.textClass = textClass;
 // Parent menu and item numbers, indexed later.
 this.parentMenu = null;
 this.parentItem = null;
 // Reference to the object's style properties (set later).
 this.ref = null;
}

// --------------------
function Item(text, href, frame, length, spacing, target)
{
 this.text = text;
 this.href = href;
 this.frame = frame;
 this.length = length;
 this.spacing = spacing;
 this.target = target;
 // Reference to the object's style properties (set later).
 this.ref = null;
}

// --------------------
function writeMenus()
{
 if (!isDOM && !isIE4 && !isNS4) return;

 // Loop through menus, using properties of menu description object, i.e. x, y, width etc...
 for (currMenu = 0; currMenu < menu.length; currMenu++) with (menu[currMenu][0])
 {
  // Variable for holding HTML for items and positions of next item.
  var str = '', itemX = 0, itemY = 0;

  for (currItem = 1; currItem < menu[currMenu].length; currItem++) with (menu[currMenu][currItem])
  {
   var itemID = 'menu' + currMenu + 'item' + currItem;

   // The width and height of the menu item - dependent on orientation!
   var w = (isVert ? width : length);
   var h = (isVert ? length : width);

   // Create a div or layer text string with appropriate styles/properties.
   if (isDOM || isIE4)
   {
    str += '<div id="' + itemID + '" style="position: absolute; left: ' + itemX +
     '; top: ' + itemY + '; width: ' + w + '; height: ' + h + '; visibility: inherit; ';
    if (backCol) str += 'background: ' + backCol + '; ';
    str += '" ';
   }
   if (isNS4)
   {
    str += '<layer id="' + itemID + '" left="' + itemX + '" top="' + itemY + '" width="' + 
     w + '" height="' + h + '" visibility="inherit" ';
    if (backCol) str += 'bgcolor="' + backCol + '" ';
   }
   if (borderClass) str += 'class="' + borderClass + '" ';
   
   // Add mouseover handlers and finish div/layer.
   str += 'onMouseOver="popOver(' + currMenu + ',' + currItem + ')" onMouseOut="popOut(' +
     currMenu + ',' + currItem + ')">';

   // Add contents of item (default: table with link inside).
   // In IE/NS6+, add padding if there's a border to emulate NS4's layer padding.
   str += '<table width="' + (w - 8) + '" border="0" cellspacing="0" cellpadding="' +
    (!isNS4 && borderClass ? 3 : 0) + '"><tr><td align="left" height="' + (h - 7) + '">' +
    '<a class="' + textClass + '" href="' + href + '"' +
    (frame ? ' target="' + frame + '">' : '>') + text + '</a></td>';

   if (target > 0)
   {
    // Set target's parents to this menu item.
    menu[target][0].parentMenu = currMenu;
    menu[target][0].parentItem = currItem;

    // Add a popout indicator.
    if (popInd) str += '<td class="' + textClass + '" align="right">' + popInd + '</td>';
   }

   // Finish off table and item.
   str += '</tr></table>' + (isNS4 ? '</layer>' : '</div>');

   // Move next item position down or across by this item's length and additional spacing.
   if (isVert) itemY += length + spacing;
   else itemX += length + spacing;

  // End loop through items and with([menu[currMenu][currItem]).
  }


  // Now, write the menu to the document depending on browser capabilities.
  // N.B: Still using properties of menu[currMenu][0] like 'ref' etc...

  // In IE5+ or NS6+, create a new DIV node and add text to it...
  if (isDOM)
  {
   var newDiv = document.createElement('div');
   document.getElementsByTagName('body').item(0).appendChild(newDiv);
   newDiv.innerHTML = str;
   ref = newDiv.style;
   ref.left = 1;
   ref.top = 1;
   ref.position = 'absolute';
   ref.visibility = 'hidden';
  }
   
  // Insert a div tag to the end of the BODY with menu HTML in place for IE4.
  if (isIE4)
  {
   document.body.insertAdjacentHTML('beforeEnd', '<div id="menu' + currMenu + 'div" ' +
    'style="position: absolute; visibility: hidden">' + str + '</div>');
   ref = getSty('menu' + currMenu + 'div');
  }
   
  // In NS4, create a reference to a new layer and write the items to it.
  if (isNS4)
  {
   ref = new Layer(0);
   ref.document.write(str);
   ref.document.close();
  }

  // Now items have been written, loop through them again to set up references.
  for (currItem = 1; currItem < menu[currMenu].length; currItem++)
  {
   itemName = 'menu' + currMenu + 'item' + currItem;
   if (isDOM || isIE4) menu[currMenu][currItem].ref = getSty(itemName);
   if (isNS4) menu[currMenu][currItem].ref = ref.document[itemName];
  }

 // End loop through menus and with (menu[currMenu][0]).
 }

 // Now they've all been written, position & show the root menu (others positioned later).
 with(menu[0][0])
 {
  ref.left = x;
  ref.top = y;
  ref.visibility = 'visible';
 }
}

// --------------------
// --------------------
var menu = new Array();

// Default colours passed to most menu constructors (just passed to functions, not
// a global variable.
var defOver = '#CCCC99', defBack = '#CBDE50';

// Default 'length' of menu items - item height if menu is vertical, width if horizontal.
var defLength = 22;

// Menu 0 is the special, 'root' menu from which everything else arises.
menu[0] = new Array();

// *** MOVE ROOT MENU AROUND HERE ***  it's positioned at (469, 60) and is 17px high now.
menu[0][0] = new Menu(false, '', 275, 38, 9, '#6699CC', '#006699', 'mainitemBorder', 'mainitemText');

// Topnav menu.
menu[0][1] = new Item('&nbsp;Aktuelles', 'http://www.klezmer.de/Neu/neu.html', '', 60, -2, 1);
menu[0][2] = new Item('&nbsp;Deutschland', 'http://www.klezmer.de/D_Klezmer/d_klezmer.html', '', 75, -2, 2);
menu[0][3] = new Item('&nbsp;Besprechungen', 'http://www.klezmer.de/Platten/platten.html', '', 90, -2, 3);
menu[0][4] = new Item('&nbsp;Download', '#', '', 65, -2, 4);
menu[0][5] = new Item('&nbsp;Links', 'http://www.klezmer.de/Klez-Links/klez-links.html', '', 40, -2, 5);
menu[0][6] = new Item('&nbsp;Forum', 'http://www.klezmer.de/discus/index.html', '', 42	, -2, 9);

// Aktuelles menu.
menu[1] = new Array();
menu[1][0] = new Menu(true, '&gt;', 0, 21, 130, '#ffffff', '#FFFF33', 'itemBorder', 'itemText');
menu[1][1] = new Item('&nbsp;<b>Neuigkeiten</b>', 'http://www.klezmer.de/Neu/neu.html', '', defLength, -1, 0);
menu[1][2] = new Item('&nbsp;Veranstaltungshinweise', 'http://www.klezmer.de/Neu/Termine/termine.html', '', defLength, -1, 0);

// Deutschland menu.
menu[2] = new Array();
menu[2][0] = new Menu(true, '&gt;', 0, 21, 240, '#ffffff', '#99CC99', 'itemBorder', 'itemText');
menu[2][1] = new Item('&nbsp;Einleitender Aufsatz: Klezmer in Deutschland', 'http://www.klezmer.de/D_Klezmer/D_intro/d_intro.html', '', 22, -1, 0);
menu[2][2] = new Item('&nbsp;<b>Deutsche Gruppen</b>', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/d_gruppen.html', '', 22, -1, 0);
menu[2][3] = new Item('&nbsp;Gruppen stellen sich selbst vor', 'http://www.klezmer.de/klez-home/klez-home.html', '', 22, -1, 0);
menu[2][4] = new Item('&nbsp;Linksammlung: deutschen Gruppen', 'http://www.klezmer.de/D_Klezmer/D_Links/d_links.html', '', 22, -1, 0);

// Besprechungen menu
menu[3] = new Array();
menu[3][0] = new Menu(true, '&lt;', 0, 21, 190, '#ffffff', '#FFCC66', 'itemBorder', 'itemText');
menu[3][1] = new Item('&nbsp;Liste aller besprochenen Gruppen', 'http://www.klezmer.de/Platten/P_Platten-Ubersicht/p_platten-ubersicht.html', '', defLength, -1, 0);
menu[3][2] = new Item('&nbsp;Gruppen/Platten', 'http://www.klezmer.de/Platten/platten.html', '', defLength, -1, 8);
menu[3][3] = new Item('&nbsp;Konzerte', 'http://www.klezmer.de/Konzerte/konzerte.html', '', defLength, -1, 0);
menu[3][4] = new Item('&nbsp;Bücher', 'http://www.klezmer.de/Buecher/buecher.html', '', defLength, -1, 0);
menu[3][5] = new Item('&nbsp;Filme', 'http://www.klezmer.de/Film/film.html', '', defLength, -1, 0);

// Download menu
menu[4] = new Array();
menu[4][0] = new Menu(true, '&gt;', 0, 21, 80, '#ffffff', '#66FF66', 'itemBorder', 'itemText');
menu[4][1] = new Item('&nbsp;MP3', 'http://www.klezmer.de/Mp3/mp3.html', '', defLength, -1, 0);
menu[4][2] = new Item('&nbsp;Noten', 'http://www.klezmer.de/Noten/noten.html', '', defLength, -1, 0);
menu[4][3] = new Item('&nbsp;Liedertexte', 'http://www.klesmer-musik.de/', '', defLength, -1, 7);

// Links menu
menu[5] = new Array();
menu[5][0] = new Menu(true, '&gt;', 0, 21, 200, '#ffffff', '#CCCCCC', 'itemBorder', 'itemText');
menu[5][1] = new Item('&nbsp;Linksammlung: internationale Gruppen', 'http://www.klezmer.de/Klez-Links/klez-links.html', '', defLength, -1, 0);
menu[5][2] = new Item('&nbsp;Linksammlung: deutschen Gruppen', 'http://www.klezmer.de/D_Klezmer/D_Links/d_links.html', '', defLength, -1, 0);

// Deutscher Klezmer menu
menu[6] = new Array();
menu[6][0] = new Menu(true, '&gt;', 115, 21, 125, '#ffffff', '#99CCCC', 'itemBorder', 'itemText');
menu[6][1] = new Item('&nbsp;Aufwind', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Aufwind/d_aufwind.html', '', 20, -2, 0);
menu[6][2] = new Item('&nbsp;a Tickle in the heart', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Tickle/d_tickle.html', '', 20, -2, 0);
menu[6][3] = new Item('&nbsp;Chalil', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Chalil/d_chalil.html', '', 20, -2, 0);
menu[6][4] = new Item('&nbsp;Colalaila', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Colalaila/d_colalaila.html', '', 20, -2, 0);
menu[6][5] = new Item('&nbsp;Di Chuzpenics', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_DiChuzpenics_/d_dichuzpenics_.html', '', 20, -2, 0);
menu[6][6] = new Item('&nbsp;Di grine Kuzine ', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Kuzine/d_kuzine.html', '', 20, -2, 0);
menu[6][7] = new Item('&nbsp;Duo Avierto', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Avierto/d_avierto.html', '', 20, -2, 0);
menu[6][8] = new Item('&nbsp;Helmut Eisel', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Eisel/d_eisel.html', '', 20, -2, 0);
menu[6][9] = new Item('&nbsp;Gebrider Moischele ', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Moischele/d_moischele.html', '', 20, -2, 0);
menu[6][10] = new Item('&nbsp;Giora Feidman', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Feidman/d_feidman.html', '', 20, -2, 0);
menu[6][11] = new Item('&nbsp;Harrys Freilach', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Harrys/d_harrys.html', '', 20, -2, 0);
menu[6][12] = new Item('&nbsp;Huljet ', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Huljet/d_huljet.html', '', 20, -2, 0);
menu[6][13] = new Item('&nbsp;Jerewan ', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Jerewan/d_jerewan.html', '', 20, -2, 0);
menu[6][14] = new Item('&nbsp;Kasbek ', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Kasbek/d_kasbek.html', '', 20, -2, 0);
menu[6][15] = new Item('&nbsp;Khupe ', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Khupe/d_khupe.html', '', 20, -2, 0);
menu[6][16] = new Item('&nbsp;Klezgojim ', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Klezgoyim/d_klezgoyim.html', '', 20, -2, 0);
menu[6][17] = new Item('&nbsp;KlezmerOrchester ', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_KlezmerOrchester/d_klezmerorchester.html', '', 20, -2, 0);
menu[6][18] = new Item('&nbsp;Klezmers Techter', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Techter/d_techter.html', '', 20, -2, 0);
menu[6][19] = new Item('&nbsp;Klezmorim (Tübingen) ', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_KlezmorimTB/d_klezmorimtb.html', '', 20, -2, 0);
menu[6][20] = new Item('&nbsp;Kol Simcha ', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Kol-Simcha/d_kol-simcha.html', '', 20, -2, 0);
menu[6][21] = new Item('&nbsp;MasselTov ', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Masseltov/d_masseltov.html', '', 20, -2, 0);
menu[6][22] = new Item('&nbsp;Mesinke ', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Mesinke/d_mesinke.html', '', 20, -2, 0);
menu[6][23] = new Item('&nbsp;Ojojoj ', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Ojojoj/d_ojojoj.html', '', 20, -2, 0);
menu[6][24] = new Item('&nbsp;Tacheles Klezmer Company', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Tacheles/d_tacheles.html', '', 20, -2, 0);
menu[6][25] = new Item('&nbsp;Schnaftl Ufftschik', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Schnaftl/D_Schnaftl.html', '', 20, -2, 0);
menu[6][26] = new Item('&nbsp;Yankele Kapelle', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Yankele/d_yankele.html', '', 20, -2, 0);
menu[6][27] = new Item('&nbsp;Yiddish Blues', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_YiddishBlues/yiddishBlues.html', '', 20, -2, 0);
menu[6][28] = new Item('&nbsp;Zwetschgendatschi', 'http://www.klezmer.de/D_Klezmer/D_Gruppen/D_Zwetschgendatschi/d_zwetschgendatschi.html', '', 20, -2, 0);

// Liedertexte menu
menu[7] = new Array();
menu[7][0] = new Menu(true, '&gt;', 40, 21, 125, '#ffffff', '#66FF99', 'itemBorder', 'itemText');
menu[7][1] = new Item('&nbsp;Neue Titel', 'http://www.klesmer-musik.de/letzte_aenderungen.htm', '', 20, -2, 0);
menu[7][2] = new Item('&nbsp;Übersetzungshinweise', 'http://www.klesmer-musik.de/hinweis.htm', '', 20, -2, 0);
menu[7][3] = new Item('&nbsp;Titel A-E', 'http://www.klesmer-musik.de/lieder_a_f.htm', '', 20, -2, 0);
menu[7][4] = new Item('&nbsp;Titel F-L', 'http://www.klesmer-musik.de/lieder_f_l.htm', '', 20, -2, 0);
menu[7][5] = new Item('&nbsp;Titel M-S', 'http://www.klesmer-musik.de/lieder_m_s.htm', '', 20, -2, 0);
menu[7][6] = new Item('&nbsp;Titel T-Z', 'http://www.klesmer-musik.de/lieder_t_z.htm', '', 20, -2, 0);

// Plattenbesprechungen menu
menu[8] = new Array();
menu[8][0] = new Menu(true, '&gt;', 90, 21, 160, '#ffffff', '#FFCC99', 'itemBorder', 'itemText');
menu[8][1] = new Item('&nbsp;<B>Übersicht</b>', 'http://www.klezmer.de/Platten/platten.html', '', 20, -2, 0);
menu[8][2] = new Item('&nbsp;Historisch <B>(vor 1940)</b>', 'http://www.klezmer.de/Platten/P_Historisch/p_historisch.html', '', 20, -2, 0);
menu[8][3] = new Item('&nbsp;in der Versenkung <B>(1940-75)</b>', 'http://www.klezmer.de/Platten/P_Versenkung/p_versenkung.html', '', 20, -2, 0);
menu[8][4] = new Item('&nbsp;Revival <B>(1975-90)</b>', 'http://www.klezmer.de/Platten/P_Revival/p_revival.html', '', 20, -2, 0);
menu[8][5] = new Item('&nbsp;Traditionalisten <B>(ab 1990)</b>', 'http://www.klezmer.de/Platten/P_Tradition/p_tradition.html', '', 20, -2, 0);
menu[8][6] = new Item('&nbsp;Erneuerer <B>(ab 1990)</b>', 'http://www.klezmer.de/Platten/P_Neu/p_neu.html', '', 20, -2, 0);
menu[8][7] = new Item('&nbsp;jüdische Lieder', 'http://www.klezmer.de/Platten/P_Juedische_Lieder/p_juedische_lieder.html', '', 20, -2, 0);
menu[8][8] = new Item('&nbsp;Aufnahmen für Kinder', 'http://www.klezmer.de/Platten/P_Kinder-Platten/p_kinder-platten.html', '', 20, -2, 0);
menu[8][9] = new Item('&nbsp;Stile und Formen', 'http://www.klezmer.de/Platten/P_Stile/p_stile.html', '', 20, -2, 0);
menu[8][10] = new Item('&nbsp;persönliche Top 10', 'http://www.klezmer.de/Platten/P_Top10/p_top10.html', '', 20, -2, 0);

// Forum menu
menu[9] = new Array();
menu[9][0] = new Menu(true, '&gt;', 0, 21, 130, '#ffffff', '#FFCC99', 'itemBorder', 'itemText');
menu[9][1] = new Item('&nbsp;<B>Diskussions-Forum</B>', 'http://www.klezmer.de/KlezphpBB2/index.php', '', defLength, -2, 0);
menu[9][2] = new Item('&nbsp;altes Gästebuch', 'http://www.klezmer.de/gaestebuch/index.html', '', defLength, -2, 0);
menu[9][3] = new Item('&nbsp;altes Schwarzes Brett', 'http://www.klezmer.de/forum/index.html', '', defLength, -2, 0);
menu[9][4] = new Item('&nbsp;Impressum+Kontakt', 'http://www.klezmer.de/impressum.html', '', defLength, -2, 0);


// *** OPTIONAL CODE FROM HERE DOWN ***

var popOldWidth = window.innerWidth;
nsResizeHandler = new Function('if (popOldWidth != window.innerWidth) location.reload()');

if (isNS4) document.captureEvents(Event.CLICK);
document.onclick = clickHandle;

function clickHandle(evt)
{
 if (isNS4) document.routeEvent(evt);
 hideAllBut(0);
}

function moveRoot()
{
 with(menu[0][0].ref) left = ((parseInt(left) < 100) ? 100 : 5);
}
