$(function(){
  $("ul.menu li")
  .prepend("&nbsp;&nbsp;")
  .hover(function(){$(this).toggleClass("attivo");})
  .children("a")
  .each(function(){
    $(this.parentNode).data("href", $(this).attr("href")).css("cursor", "pointer").bind("click", function(){ location.href=$(this).data("href"); });
    $(this).replaceWith("<span>"+$(this).text()+"</span>");
  }).end()
  .has("ul")
  .mouseenter(function(){ 
    var ul=$(this).children("ul").get(0); 
    var l=$(ul).data("livello"); 
    $("ul.menu ul:visible").not(ul).each(function(){ if ($(this).data("livello")==l) { $(this).hide(); } }); 
    $(ul).data("chiudi", false).show();
  })
  .mouseleave(function(){ 
    var ul=$(this).children("ul").data("chiudi", true).get(0); 
    setTimeout(function(){ if ($.data(ul, "chiudi")) $(ul).hide(); }, 1500);
  })
  .children("ul")
  .css("cursor", "default")
  .data("chiudi", true)
  .click(function(){return false;})
  .each(function(){
    $(this).data("livello", $(this).parents("li").length);
    if ($(this).parents("ul:first").hasClass("orizzontale")) 
         { $(this).before("<img src='/images/freccia.gif' alt='' border=0>"); }
    else { $(this).before("<img src='/images/frecciadx.gif' alt='' border=0>"); }
  });
  $("a.esterno").attr("target","_blank");
});
