//==========================================//
//    SLIDE MENU v.3  [object-oriented]     //
//        Script by Stefano Roncari         //
//              www.syntax.it               //
//==========================================//

//*** Impostazioni personalizzabili ***//
fatherTop = 314; // collocazione del menu (in pixel) dall'alto della pagina
fatherLeft = 203; // collocazione del menu (in pixel) da sinistra
sonWidth = 105;
startX = 0; // posizione iniziale (in pixel) degli elementi del menu, rispetto al div "padre"
endX = 120; // posizione finale (in pixel) degli elementi del menu, rispetto al div "padre"
interval = 50; // distanza in millisecondi tra lo slide di un elemento e quello dell'elemento successivo
speed = 10; // velocità dello slide


// Crea gli item dei menu
function writeMenu()
{
   document.write('<style>'
   + '.menuPadre {position:absolute; width:240px; height:150px; z-index:400; top:' +fatherTop+ 'px; margin-left:' +fatherLeft+ 'px; clip:rect(0 240px 150px 120px); overflow:hidden;}'
   + '.menuFiglio {position:absolute; width:' +sonWidth+ 'px; height:15px; z-index:300; background:#CFD6D8; padding:0 5px 0 5px; border:1px solid #fff; filter:Alpha(Opacity=85); -moz-opacity:0.85;}'
   + '</style>');

   document.write("<div id='padre' class='menuPadre'>");
   for (k=0; k<theSons.length; k++)
   {
      theSons[k].aperto = false;
      theSons[k].inApertura = false;
      for (i=0; i<theSons[k].length; i++)
      {
         theSons[k][i] = new MenuItem(theSons[k][i][0],theSons[k][i][1],theSons[k][i][2],i);
         theSons[k][i].setX(startX);
      }
   }
   document.write("</div>");
}


// CLASSE MenuItem
function MenuItem(name, label, url, height)
{
   this.divName = name;
   this.setX = setXMethod;

   topPos = 19 * height;
   if (url!="") {document.write ("<div id='" +this.divName+ "' class='menuFiglio' style='top:" +topPos+ "px;'><a href='" +url+ "'>" +label+ "</a></div>");}
   else {document.write ("<div id='" +this.divName+ "' class='menuFiglio' style='top:" +topPos+ "px;'>" +label+ "</div>");}
}

function setXMethod(xPos)
{
   //alert (this.divName);
   document.getElementById(this.divName).style.left = xPos + "px";
   this.x = xPos;
}


function activateMenu(menuId)
{
   // Chiude gli eventuali menu aperti...
   disactivateOthersThan(menuId);

   // ...E poi chiama la funzione slideSon per ogni elemento del menu, ad un certo intervallo di tempo l'uno dall'altro, per creare un effetto "onda"
   if (theSons[menuId].aperto==false && theSons[menuId].inApertura==false && altroInApertura(menuId)==false)
   {
      for (p=0; p<theSons[menuId].length; p++)
      {
         myInterv = interval * p;
         theSons[menuId][p].myTimer1 = setTimeout("iterateSlideIn("+menuId+","+p+")",myInterv);
      }
      roll(menuId,"on"); // Serve solo nel caso specifico del sito Syntax.it, per caricare i rollover
   }
   else if (theSons[menuId].aperto == true)
   {
      for (p=0; p<theSons[menuId].length; p++)
      {
         myInterv = interval * p;
         //alert("muovo " +theSons[menuId][p].divName);
         theSons[menuId][p].myTimer1 = setTimeout("iterateSlideOut("+menuId+","+p+")",myInterv);
      }
      roll(menuId,"off"); // Serve solo nel caso specifico del sito Syntax.it, per caricare i rollover
   }
}


function disactivateOthersThan(menuId)
{
   //alert("disactivate!");
   for (a=0; a<theSons.length; a++)
   {
      if (a!=menuId && theSons[a].aperto==true) // Se c'è un menu aperto che NON è quello richiesto
      {
         for (q=0; q<theSons[a].length; q++)
         {
            if (theSons[a][q].x > startX)
            {
               myInterv = interval * q
               theSons[a][q].myTimer2 = setTimeout("iterateSlideOut("+a+","+q+")",myInterv);
            }
         }
         roll(a,"off"); // Serve solo nel caso specifico del sito Syntax.it, per caricare i rollover
      }
   }
}


function iterateSlideIn(menu,subMenu)
{
   myObj = theSons[menu][subMenu];
   if (myObj.x < endX)
   {
      theSons[menu].inApertura = true;
      myObj.setX(myObj.x +10);
      myObj.timerIn = setTimeout("iterateSlideIn("+menu+","+subMenu+")",speed);
   }
   else
   {
      // Se è l'ultima voce del menu...
      if (subMenu == theSons[menu].length-1) {
         theSons[menu].aperto=true;
         theSons[menu].inApertura = false;
      }
   }
}

function iterateSlideOut(menu,subMenu)
{
   myObj = theSons[menu][subMenu];
   if (myObj.x > startX)
   {
      myObj.setX(myObj.x -10);
      myObj.timerOut = setTimeout("iterateSlideOut("+menu+","+subMenu+")",speed);
   }
   // Se è l'ultima voce del menu...
   if (subMenu == theSons[menu].length-1) { theSons[menu].aperto=false; }
}

function altroInApertura(myMenuId)
{
   for (g=0; g<theSons.length; g++)
   {
      if (theSons[g] != myMenuId && theSons[g].inApertura==true) {return true;}
   }
   return false;
}

function roll(langId,mod)
{
   nome = (langId==0) ? "ita" : "eng";
   myImg = document.getElementById(nome);
   urlOff = "images/" +nome+ "_off.gif";
   urlOn = "images/" +nome+ "_on.gif";
   
   if (mod=="on")
   {
      myImg.src = urlOn;
   }
   else if (mod=="off")
   {
      myImg.src = urlOff;
   }
}

