$(document).ready(function(){
  $("ul.menu > li")
    .children("a:not(:has(span))").each(function(i,v){
      $(v).prepend("<span class='ombra'>"+$(v).text()+"</span>");
    }).end()
    .children("ul").each(function(i, v){
    $(v.parentNode).addClass("freccia")
    .mouseover( function(){ var po=$(this).offset(); po.top+=35;
      $(v).show(300).data("chiudi", false).css({top: po.top+"px", left: po.left+"px", bottom: "auto", right: "auto", zIndex: "2010"})
      .mouseover(function(){ $(v).data("chiudi", false); });
    })
    .add(v).mouseout( function(){ $(v).css({zIndex: "2000"}).data("chiudi", true); setTimeout(function(){ if ($(v).data("chiudi")) { $(v).hide(100); } }, 750); });
  });
  $(".tooltip").each(function(){
    $(this).prev()
    .bind("mouseenter mousemove", function(e){ $(this).next().show()
      .css({top: (($(window).height()+$(window).scrollTop()-e.pageY-$(this).next().height()<10)?e.pageY-5-$(this).next().height():e.pageY+5)+"px",
            left: (($(window).width()+$(window).scrollLeft()-e.pageX-$(this).next().width()<10)?e.pageX-5-$(this).next().width():e.pageX+5)+"px",
            bottom: "auto", right: "auto"});
    })
    .mouseout( function(e){ $(this).next().hide(); });
    /* if ($(this).prev().get(0).tagName=="IMG") {
      $("<img src='/qt/grafica/icon_lente.png' alt='' width='20' height='21' border='0' style='position: absolute; z-index: 10;'>").appendTo("body").offset($(this).prev().offset());
    } */
  });
  $(".over").hoverClass("evidenzia").addClass("cursore");
  $("#ajax").ajaxSend(function(evt, request, settings){ $(this).removeClass().addClass("notice").html("Inizio processo").css({top: $(window).scrollTop()+2+"px"}).show(); })
            .ajaxError(function(evt, request, settings){ $(this).removeClass().addClass("error").html("Errore alla pagina:<br>"+request.responseText+""); })
            .ajaxSuccess(function(evt, request, settings){ $(this).removeClass().addClass("success").html("Fine processo...").delay(500).fadeOut(500); })
            .click(function(){ $(this).hide(500);} );
  $("a.esterno").attr("target","_blank");
  $("#indirizzomail").html(" | <a href=''>e-mail</a>").find("a").click(function(){window.location="mai"+"lto:redazione"+"@"+"questotrentino.it";return false;});
  // $("img:hidden").each(function(i,x){ var img = new Image(); $(img).attr("src", $(x).attr("src")); });
});

function mostratooltip(i, t){
  $(""+i+" .tooltip").each(function(){
    $(this).prev().unbind("mouseover").unbind("mousemove").unbind("mouseout");
  }).addClass("note").removeClass("tooltip").removeClass("w200").show();
  $("span", t.parentNode).toggle();
}
function ripristinatooltip(i, t){
  $(""+i+" .note").each(function(){
    $(this).prev()
    .mouseover( function(e){ $(this).next().show().css({top: (e.pageY+20)+"px", left: (e.pageX+20)+"px"}); })
    .mousemove( function(e){ $(this).next().css({top: (e.pageY+20)+"px", left: (e.pageX+20)+"px"}); })
    .mouseout( function(e){ $(this).next().hide(); });
  }).addClass("tooltip").addClass("w200").removeClass("note").hide();
  $("span", t.parentNode).toggle();
}
function vota(t, m){
  var n=$(".scelta:checked", t.form).length;
  if (n==0) {alert2("<div><strong>Errore!</strong></div> <div>Seleziona una scelta!</div>")}
  else if (n>m) {alert2("<div><strong>Errore!</strong></div> <div>Troppe scelte: puoi selezionare <b>"+m+"</b> scelte!</div>")}
  else { t.disabled=true; $.post("vota.asp", {scid: $(".scelta", t.form).serialize() }, function(r){ return; }, 'script'); }
}

function alert2(t, f1, f2){
  if ($("#popup").length==0) { $("<div id='popupsfondo'></div><div id='popup'></div>").appendTo("body").hide(); } else { $("#popup, #popupsfondo").hide(); }
  if (typeof f1 != "function") { f1=function(){ $("#popup, #popupsfondo").hide() } }
  $("#popupsfondo").css({top: "0px", left: "0px", height: $(document).height()+"px", width: $(document).width()+"px", opacity: 0.4}).show();
  if (typeof f2 != "function") {
    $("#popup").css({top: ($(window).height()/2-100+$(window).scrollTop())+"px", left: ($(window).width()/2-200+$(window).scrollLeft())+"px"})
      .html("<div class='contenuto'>"+t+"</div><div class='bottoni'><input type=button name='ok' id='ok' value='Ok'></div>").show();
    $("#ok, #popupsfondo").click(f1);
  } else {
    $("#popup").css({top: ($(window).height()/2-100+$(window).scrollTop())+"px", left: ($(window).width()/2-200+$(window).scrollLeft())+"px"})
      .html("<div class='contenuto'>"+t+"</div><div class='bottoni'><input type=button name='ok' id='ok' value='Si'> <input type=button name='okno' id='okno' value='No'></div>").show();
    $("#ok").click(f1);
    $("#okno, #popupsfondo").click(f2);
  }
}
