var objDiv;//oggetto div clickato
var req;//rif. oggetto asincrono
var urlHttpReq;
var req2;

//si attiva al click del link del menù
function getNavigator(myDiv, folder, url, urlRem) {

  

  //viene impostato solo la prima volta, poi mi arriva sempre vuoto
  if(urlRem!="") urlHttpReq = urlRem;

  //link clickato in bold
  if(objDiv!=null) {
    //imposto lo stile normale x il link clickato in precedenza..
    objDiv.getElementsByTagName("a")[0].style.fontWeight = "normal";
    //imposto lo stile x il link attivo...
    myDiv.getElementsByTagName("a")[0].style.fontWeight = "bold";
  }

  //resetto il menù..
  // se non ho clickato su una voce della root di menù, elimino tutti i figli dei fratelli del
  //div clickato. sostanzialmente resetto il menù
  if(myDiv.id!="divMenu"){
   var myParent = myDiv.parentNode;
   for(var i=0;i<myParent.childNodes.length;i++){
    var brosDiv = myParent.childNodes[i];
    for(var x=brosDiv.childNodes.length-1;x>-1;x--)
      if(brosDiv.childNodes[x].tagName=="DIV") brosDiv.removeChild(brosDiv.childNodes[x]);
   }
  }

      
  //mi serve più tardi x aprire sempre con ajax il linx
  objDiv=myDiv;//riferimento div clickato
  folder = escape(folder);

  //creo l'oggetto gestione asincrona x mozilla e ie
  if (window.XMLHttpRequest) req = new XMLHttpRequest();
  else if (window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");
  
  //gestione AJAX, menù
  req.open("GET",urlHttpReq+"?folder="+folder, true);
  req.onreadystatechange = creaSubMenu;
  req.send(null);
  
  //imposto il linx, senza reset del menù
  getLinx(url);
    
 //gestione statistiche google. script inclusione in index.jsp
 if(url!="") urchinTracker(url);
 
}

//richiamata al click dei linx esclusi quelli del menù di sx. toglie il bold dal link clickato del menù
function resetBold(){
 if(objDiv.getElementsByTagName("a")[0].style.fontWeight=="bold") 
    objDiv.getElementsByTagName("a")[0].style.fontWeight="normal";
}

//prende il body del link e imposta il contenuto del div
function getLinx(url2Open){


  // svuoto sempre il div con il bread
 if(document.getElementById("breadC")!=null)document.getElementById("breadC").innerHTML="";

 //gestione AJAX dei linx
 if(url2Open!=""){
   
   if(url2Open.indexOf("homePage.html")>-1) document.getElementById("tdNews").style.display="block";
   else document.getElementById("tdNews").style.display="none";
   
   //creo l'oggetto gestione asincrona x mozilla e ie
   if (window.XMLHttpRequest) req2 = new XMLHttpRequest();
   else if (window.ActiveXObject) req2 = new ActiveXObject("Microsoft.XMLHTTP");

   req2.open("GET",url2Open, true);
   req2.onreadystatechange = processLinx;
   req2.send(null);
 }
 
}

//hyper linx visualizzato nel div
function processLinx(){

 //finito..
 if (req2.readyState == 4) {
    //risultato ok
    if (req2.status == 200) 
        //imposto il contenuto del div
        if(document.getElementById("ifMain")!=null) {

          document.getElementById("ifMain").innerHTML=req2.responseText;
          //in un div nascosto della notizia c'è il bread crumbs... lo imposto
          if(document.getElementById("breadN")!=null && document.getElementById("breadC")!=null){
              document.getElementById("breadC").style.display="block";
              document.getElementById("breadC").innerHTML = document.getElementById("breadN").innerHTML;
             }
        }
    else alert("impossibile ottenere un risultato");
 }

 

}

//gestione del risultato...
function creaSubMenu() {
    //finito..
    if (req.readyState == 4) {
        //risultato ok
        if (req.status == 200) parseResult();        
        else alert("impossibile ottenere un risultato");
    }
}

//processo il risultato
function parseResult() {

 var myDocument;//doc xml contenente il risultato della rappresentazione xml delle voci
 //della cartella clickata
 if (document.implementation.createDocument){ 
   // Mozilla, create a new DOMParser 
   var parser = new DOMParser(); 
   myDocument = parser.parseFromString(req.responseText, "text/xml"); 
 } else if (window.ActiveXObject){ 
   // Internet Explorer, create a new XML document using ActiveX 
   // and use loadXML as a DOM parser. 
   myDocument = new ActiveXObject("Microsoft.XMLDOM") 
   myDocument.async="false"; 
   myDocument.loadXML(req.responseText);   
 }
 //root del doc
 var menu= myDocument.getElementsByTagName("menu")[0];
 var treeDiv = ""; 
 //prendo gli attributi e creo l'html da appendere al div sul quale ho clickato
 for(var i=0;i<menu.childNodes.length;i++) {
   
   var level = menu.childNodes[i].getAttribute("navLevel");
   var url = menu.childNodes[i].getAttribute("url");
   var descr = menu.childNodes[i].getAttribute("descr");
   var rsc = menu.childNodes[i].getAttribute("rsc");
   var img = menu.childNodes[i].getAttribute("img");

   //imposto l'immagine..
   if(img=="yes") img="<img src='./immagini/arrow_menud.gif' border='0'>&nbsp;"; 
     
   treeDiv = treeDiv + "<div class='liv"+level+"' style='padding:"+level+"'>";
   treeDiv = treeDiv + img +
    "<a class='linkNav' href='#' style='font-weight:normal;color:black' onclick=\"getNavigator(this.parentNode,'" + rsc 
    + "','"+url+"','')\">"+ descr+"</a></div>";
        
  }//fine ciclo
 
  //appendo l'html
  objDiv.innerHTML = objDiv.innerHTML + treeDiv;

}
//switch x la mappa del sito
function switchStyle(objA){
 if(objA.parentNode.className=="mappaS")objA.parentNode.className="mappaSOver";
 else objA.parentNode.className="mappaS";
}
