$(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); });
  });
  $(".over").hoverClass("evidenzia").addClass("cursore");
  $(".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(); });
  });
  $("#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");
  if ($("#indicejs").length) {
    $("h2,h3,h4,h5,h6", $("#articolotesto").get(0)).each(function(i){
      $("#indicejs").append("<div><a href='#int"+i+"'>"+$(this).text()+"</a></div>");
      $(this).prepend("<a href='#indicejs' name='int"+i+"' id='int"+i+"' style='float:right; font-size: 80%;'>indice</a>");
      if (i==0) { $("#indicejs").show(); }
    }); 
  }
  $("a[href^=#][href!=#]").live("click",function(e){
    $("html").animate({"scrollTop": $($(this).attr("href")).offset().top+"px"}, 1500); 
    e.preventDefault();  
  });
  if ($("#articolotesto p:first").length) {
    $("#articolotesto p:first").html($("#articolotesto p:first").html().replace(/^((<[^>]*>)*)([^\w\s]*[\w\s][^\w\s]?)(.*)/,"$1<span class=\"capolettera\">$3</span>$4"));
  }
  $("#indirizzomail").html(" | <a href=''>e-mail</a>").find("a").click(function(){window.location="mai"+"lto:redazione"+"@"+"questotrentino.it";return false;});
  $(window).load(function(){
    $("#articolotesto img").each(function(i,x){
      $("<img src='"+$(x).attr("src")+"' style='position: absolute; z-index: 100; border: 2px solid #900; cursor: pointer' id='img"+i+"'>").css("opacity",0.5)
      .appendTo("body").data("id", x).data("w", $("#img"+i).width()).data("h", $("#img"+i).height()).hide()
      .bind("click", function(){
        var id=$(this).data("id"); var o=$(id).offset();
        $(this).animate({top: o.top+"px", left: o.left+"px", width: $(id).width()+"px", height: $(id).height()+"px", borderWidth: "2px", opacity: 0.5}, 500, function(){ $(this).hide(); });
      });
      if ( $(x).width() < $("#img"+i).data("w") ){ var o=$(x).offset(), h=$("#img"+i).data("h"), w=$("#img"+i).data("w");
        $("<img src='grafica/lente.gif' style='position: absolute; z-index: 10; top: "+o.top+"px; left: "+o.left+"px'>").appendTo("body");
        $(x).css("cursor","pointer").bind("click", function(){ var o=$(this).offset();
          $("#img"+i).css({ top: o.top+"px", left: o.left+"px", width: $(this).width()+"px", height: $(this).height()+"px" })
          .animate({top: ($(window).scrollTop()+($(window).height()-h)/2), left: ($(window).scrollLeft()+($(window).width()-w-12)/2), 
          width: w+"px", height: h+"px", borderWidth: "6px", opacity: 1}, 500);
        });
      } else { $("#img"+i).remove(); }
    });
  });
  // $("img:hidden").each(function(i,x){ var img = new Image(); $(img).attr("src", $(x).attr("src")); });
});

function ingrande(){
}

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);
  }
}
