//=================================================
//==            MenuGenerator v.1.0              ==
//==  designed and developed by Stefano Roncari  ==
//==               www.syntax.it                 ==
//=================================================

ns4 = (document.layers) ? true:false
ie4 = (navigator.appVersion.indexOf("MSIE 4") != -1) ? true:false
ie = (document.getElementById && navigator.appName.indexOf("Netscape") == -1) ? true:false
op5 = (navigator.userAgent.indexOf('Opera') != -1) ? true:false
ns6 = (document.getElementById && navigator.appName.indexOf("Netscape") != -1) ? true:false

function centerDivs()
{
   for (t=0; t<theDivs.length; t++)
   {
      if (ns4){
         //alert('Ns4!');
         container = document.layers.divASContainer;
         eval(theDivs[t][0] + "= document.layers." +theDivs[t][1]);
         centerPage = window.innerWidth/2 - 8;
      }
      else if (op5){
         //alert('Op5!');
         eval(theDivs[t][0] + "= document.all." +theDivs[t][1] + ".style");
         container = document.all.divASContainer.style;
         centerPage = (window.innerWidth/2 -1);
      }
      else if (ie4){
         //alert('IE 5+!');
         eval(theDivs[t][0] + "= document.all." +theDivs[t][1] + ".style");
         container = document.all.divASContainer.style;
         centerPage = document.body.clientWidth/2;  // normalmente si usa document.body.scrollWidth, ma dà problemi con IE 4.01
      }
      else if (ie || ns6) {
         //alert('IE 5+ o ns6+!');
         eval(theDivs[t][0] + "= document.getElementById('" +theDivs[t][1] + "').style");
         container = document.getElementById('divASContainer').style;
         centerPage = document.getElementById('sizer').offsetWidth / 2;
      }

      // Qui sotto: ho messo "&& !ns6" perché mozilla e ns6+ mantengono il sito centrato anche se la finestra è piccola, facendo slittare i <div> sulla sinistra del browser
      if (centerPage<=389 && !ns6){
         //alert('minore!');
         eval(theDivs[t][0] +".left = '423px'");
         container.left = "430px";
      }
      else
      { 
         eval(theDivs[t][0] +".left =("+ centerPage +" + 34) + 'px'");
         container.left = centerPage +41+ "px";
      }
   }
}

function writeMenu()
{
   // Facoltativo: carica in memoria l'immagine "list_on", per il rollover sui menuChildren
   list=new Image(); list.src="images/menu_list_on.gif";

   // Scrive i vari DIV uno alla volta, generando i Parents (voci principali di menu) per ognuno di essi
   for (i=0; i<theDivs.length; i++)
   {
      document.write(''
      +'<div id="' +theDivs[i][1]+ '" style="position:absolute; z-index:100; left:423px; top:12px; visibility:hidden; width:155px;">'
      +'<table cellpadding="0" cellspacing="0" border="0" width="155">');

      generateParents(i); // qui viene chiamata la funzione che genera i Parents

      document.write(''
      +'</table>'
      +'</div>');
   }
}

function generateParents(divNum)
{
// Si gira all'interno dell'Array theParents, perché su ogni Div bisogna scrivere tutti i Parents.
// In alcuni Div, in più, ci saranno anche dei menuChildren
   for (p=0; p<theParents.length; p++)
   {
      // Se la chiamata di generateParents() è stata fatta sul Div1, allora bisognerà scrivere i menuChildren del menu1, ecc.
      // Qui sotto: perché "divNum -1"?  Perché sul Div0 non devo visualizzare nessun menuChildren; sul Div1 devo visualizzare i menuChildren del menu 0, ecc.
      // Quindi, nel caso del Div0 (chiamato "mainMenu"), la condizione è sempre falsa, e quindi non vengono creati i menuChildren.
      if (p == (divNum -1))
      {
         document.write(''
         +   '<tr>'
         +      '<td width="26" bgcolor="#999999"><img src="' +theParents[p][2]+ '" width="26" height="14" border="0" alt=""></td>'
         +      '<td width="130" bgcolor="#999999" class="subMenu"><a href="javascript:showMenu(' +(p+1)+ ')" onfocus="this.blur();" class="subMenu">' +theParents[p][0]+ '</a></td>'
         +   '</tr>'
         +   '<tr>'
         +      '<td class="subMenu2"><img src="images/menu_sub_top1.gif" width="26" height="5" border="0" alt=""></td>'
         +      '<td class="subMenu2"><img src="images/ghost.gif" width="129" height="5" border="0" alt=""></td>'
         +   '</tr>');

         generateChildren(p); // viene chiama la funzione che genera i menu figli.
      }
      else // ...altrimenti, non si scrivono i menuChildren
      {
         document.write(''
         +   '<tr>'
         +      '<td width="26"><img src="' +theParents[p][1]+ '" width="26" height="14" border="0" alt=""></td>'
         +      '<td width="130" class="menu"><a href="javascript:showMenu(' +(p+1)+ ')" onfocus="this.blur();" class="menu">' +theParents[p][0]+ '</a></td>'
         +   '</tr>'
         +   '<tr>'
         +     '<td colspan="2"><img src="images/ghost.gif" width="1" height="5" border="0" alt=""></td>'
         +   '</tr>');
      }
   }
}


function generateChildren(mainVoiceNum)
{
   for (c=0; c<theChildren[mainVoiceNum].length; c++)
   {
      document.write(''
      +   '<tr>'
      +      '<td class="subMenu2"><img src="images/ghost.gif" width="1" height="1" border="0" alt=""></td>'
      +      '<td class="subMenu2"><img src="images/ghost.gif" width="1" height="1" border="0" alt=""></td>'
      +   '</tr>'
      +   '<tr>'
      +      '<td class="subMenu2" width="26" valign="top"><img name="list' +mainVoiceNum+ '_' +c+ '" src="images/menu_list.gif" width="26" height="13" border="0" alt=""></td>'
      +      '<td class="subMenu2"><a href="' +theChildren[mainVoiceNum][c][1]+ '" class="subMenu2" onmouseover="if(!ns4)document.images.list' +mainVoiceNum+ '_' +c+ '.src=\'images/menu_list_on.gif\'" onmouseout="if(!ns4)document.images.list' +mainVoiceNum+ '_' +c+ '.src=\'images/menu_list.gif\'">' +theChildren[mainVoiceNum][c][0]+ '</a></td>'
      +   '</tr>');
   }

      document.write(''
      +   '<tr>'
      +      '<td colspan="2"><img src="images/menu_sub_bottom.gif" width="155" height="12" border="0" alt=""></td>'
      +   '</tr>');
}


function initMenu()
{
   for (q=0; q<theDivs.length; q++)
   {
      if (ns4) { eval(theDivs[q][0] + "= document.layers." + theDivs[q][1]) }
      else if (op5) { eval(theDivs[q][0] + "= document.all." + theDivs[q][1] + ".style") }
      else if (ie) { eval(theDivs[q][0] + "= document.all." + theDivs[q][1] + ".style") }
      else if (ns6) { eval(theDivs[q][0] + "= document.getElementById('" + theDivs[q][1] + "').style") }
   }
   mainMenu.visibility = 'visible';
}


function showMenu(num)
{
   tempMenu = 'menu' + num;
   //alert(tempMenu);

   for (a=0; a<theDivs.length; a++)
   {
      if ((tempMenu == theDivs[a][0]) && (eval (theDivs[a][0] + '.clicked == true')))
      {
         // Se il menu clickato è già aperto (es. "menu1.clicked = true"), allora esso si chiude
         eval (theDivs[a][0] + '.visibility = "hidden"');
         eval (theDivs[a][0] + '.clicked = false');
         mainMenu.visibility = "visible";
      }
      else if (tempMenu == theDivs[a][0])
      {
         // Se il menu clickano NON era già aperto, si apre :-)
         eval (theDivs[a][0] + '.visibility = "visible"');
         eval (theDivs[a][0] + '.clicked = true');
         // Il "clicked" del layer corrente viene settato a true; bisognerà poi settarlo a false
         // quando si aprirà un altro menu (quando questo menu verrà chiuso)
      }
      else
      {
         // Gli altri menu (su cui non si è clickato) vengono chiusi, e il loro "clicked" è settato a false.
         eval (theDivs[a][0] + '.visibility = "hidden"');
         eval (theDivs[a][0] + '.clicked = false');
      }
   }
}