/* insert drop down menus into dom */
function initMenu(ie) {
   loadedPage = document.URL;

   /* add drop down for 'produkte' */
   if (loadedPage.search(/produkte/) != -1) {
      lastPathDelimiter = loadedPage.lastIndexOf('\\');
      if (lastPathDelimiter == -1) lastPathDelimiter = loadedPage.lastIndexOf('/');
      loadedPage = (loadedPage.slice(lastPathDelimiter + 1, loadedPage.length));

      var menubar_2 = document.getElementById('menubar_2');
      var givenUl = menubar_2.getElementsByTagName('ul').item(0)

      var givenLi = givenUl.getElementsByTagName('li').item(0);
      for ( ; givenLi != null; givenLi = givenLi.nextSibling) {

         /* fix ie bugs */
         if (ie) ieFix(givenLi);

         /* get drop down content */
         id = givenLi.getAttribute('id');
         data = menu[id];

         if (data.length != 0) {

            /* create submenu */
            ul = document.createElement('ul');
            addClass(ul, 'depth_2');
            first = true;

            /* create elements in submenu */
            for (var i=0; i<data.length; i++) {

               /* create list items, anchors, set attributes, etc */
               li = document.createElement('li');
               addClass(li, 'depth_2');

               a = document.createElement('a');
               addClass(a, 'depth_2');
               var href = document.createAttribute('href');
               href.nodeValue = rootDir + 'produkte/' + id + '/' + data[i].href;
               a.setAttributeNode(href);

               textNode = document.createTextNode(data[i].text);

               /* set class=loaded for the currently loaded page */
               if (loadedPage == data[i].href) {
                  addClass(li, 'loaded');
                  addClass(a, 'loaded');
               }
               /* set class=first for each first element in list */
               if (first) {
                  addClass(li, 'first');
                  addClass(a, 'first');
                  first = false;
               }

               /* put things together */
               a.appendChild(textNode);
               li.appendChild(a);
               ul.appendChild(li);
            }
            givenLi.appendChild(ul);
         }
      }
   }
}

function menuItem(text, href) {
   this.text = text;
   this.href = href;
}

/* add a string to the class of given element */
function addClass(element, value) {
   if (element.className)
      oldValue = element.className;
   else
      oldValue = element.getAttribute('class');
   if (!oldValue) oldValue = '';
   if (oldValue.search(value) == -1) {
      var newValue = oldValue + ' ' + value;
      var attributeNode = document.createAttribute('class');
      attributeNode.nodeValue = newValue;
      element.setAttributeNode(attributeNode);
   }
}

var menu = new Array();
   menu['burger'] = new Array(
         new menuItem('JaSoJa', 'jasoja.html'),
         new menuItem('Lupi', 'lupi.html'),
         new menuItem('Topi', 'topi.html'),
         new menuItem('Ideal', 'ideal.html'),
         new menuItem('Tante Emmer', 'tante_emmer.html'),
         new menuItem('Mama Loup', 'mama_loup.html'),
         new menuItem('Roter Baron', 'roter_baron.html')
   );
   menu['pasta'] = new Array(
         new menuItem('Spaghetti', 'spaghetti.html'),
         new menuItem('Conchiglie', 'conchiglie.html'),
         new menuItem('Chifferi', 'chifferi.html')
   );
   menu['saucen'] = new Array(
         new menuItem('TomVera', 'tomvera.html'),
         new menuItem('TomSoja', 'tomsoja.html'),
         new menuItem('TomOliva', 'tomoliva.html')
   );
   menu['aufstriche'] = new Array(
         new menuItem('Pflaumenmus', 'pflaumenmus.html'),
         new menuItem('Sauerkirsche', 'sauerkirsche.html'),
         new menuItem('Aprikose Kajszi / Aprikose Rozsa', 'aprikose_kajszi_aprikose_rozsa.html'),
         new menuItem('Sanddorn & Apfel', 'sanddorn_apfel.html'),
         new menuItem('Zucchini & Apfel', 'zucchini_apfel.html'),
         new menuItem('Kürbis & Sanddorn', 'kuerbis_sanddorn.html'),
         new menuItem('Grüne Erbse', 'gruene_erbse.html'),
         new menuItem('Mais', 'mais.html')
   );
   menu['dips'] = new Array(
         new menuItem('Tomaten-Ketchup', 'tomaten-ketchup.html'),
         new menuItem('Paprikapüree', 'paprikapueree.html')
   );
   menu['oele'] = new Array(
   );
   menu['drinks'] = new Array(
         new menuItem('Aprikose / Birne', 'aprikose_birne.html'),
         new menuItem('Brombeere / Pfirsich', 'brombeere_pfirsich.html'),
         new menuItem('Sanddorn Pur', 'sanddorn_pur.html'),
         new menuItem('Sanddorn & Apfel / Sanddorn & Topinambur', 'sanddorn_apfel_sanddorn_topinambur.html'),
         new menuItem('Kürbis & Sanddorn / Rote Bete & Sanddorn', 'kuerbis_sanddorn_rote_bete_sanddorn.html'),
         new menuItem('Möhre & Sanddorn / Tomate', 'moehre_sanddorn_tomate.html'),
         new menuItem('Rote Bete / Rote Bete & Apfel', 'rote_bete_rote_bete_apfel.html'),
         new menuItem('Möhre & Weizengras / Möhre & Zucchini & Ingwer', 'moehre_weizengras_moehre_zucchini_ingwer.html')
   );


/* fix ie bugs */
function ieFix(element) {
   /* some values don't work via css file */
   element.style.zIndex = '99';
   element.style.position = 'relative';
   /* workaround for missing hover */
   element.onmouseover = function(event) {
      this.className += " iehover";
   }
   element.onmouseout = function(event) {
      this.className = this.className.replace(" iehover", "");
   }
}
