(function() {
  var newMethods = {
    centra: function() { 
      return this.each( function(){ 
        $(this).css({ top: parseInt($(window).scrollTop()+(($(window).height()-$(this).height())/2))+"px", left: parseInt(($(window).width()-$(this).width())/2)+"px"});
      });
    },
    tooltip: function(txt){
      if ($("#tooltip").length==0) {
        $("body").append("<div id='tooltip' style='position: absolute; background-color:#ffff99; display: none; z-index: 1000; color: #000; padding: 5px; '></div>");
      }
      return this.each( function(){ 
        $(this)
          .bind("mouseleave", function(e){$("#tooltip").html("").hide();})
          .bind("mousemove",  function(e){$("#tooltip").html(txt).show().offset({left: (e.pageX+10), top: (e.pageY+10)});});
      });
    }
  };
  jQuery.each(newMethods, function(i) { jQuery.fn[i] = this; });
})(); 

$(function(){
  $("body").append("<div id='popupsfondo' style='position: absolute; background-color:#570000; display: none; '></div> <img src='../grafica/attesa.gif' alt='' id='attesa' border='0'> ");
  $(window).scroll( function(){ $("#fotona, #naviga").stop().animate({top: ($(window).scrollTop()+(($(window).height()-$("#fotona").height())/2))+"px"}, 1000); } );
  $("#popupsfondo").tooltip(" cliccare qui per chiudere la fotografia ");
  $("a img").not(":last").each(function(i, img){
    $(img.parentNode).click(function(){
      $("#popupsfondo").css({ top: "0px", left: "0px", width: $(window).width()+"px", height: $(document).height()+"px", opacity: .8, zIndex: 100}).show()
      .bind("click", function(){ $("#popupsfondo").hide(); $("#fotona, #naviga").remove(); });
      mostrafoto(i);
      return false;
    });
  });
});      

function mostrafoto(i){
  $("#tooltip").html("").hide();
  $("#attesa").show().centra();
  if (i>=($("a img").length)-1) {i=0} else if (i<0) {i=($("a img").length)-2}
  var img = new Image(), x, y;
  $(img).bind("load",function () {
    $("#attesa").hide();
    if ($("#fotona").length==0) {
      $(this).attr("id", "fotona").css("display","none").appendTo("body").fadeIn().unbind("load").centra();
      x=$(this).width();
      y=$(this).height();
      $("<div id='naviga' style='width: "+x+"px; height: "+y+"px'><div id='sx'>&nbsp;</div><div id='dx'>&nbsp;</div></div>").appendTo("body").centra().children("div").css({opacity: 0});
    } else {
      $(this).attr("id", "fotonatmp").css("display","none").appendTo("body");
      x=$("#fotonatmp").width();
      y=$("#fotonatmp").height();
      var o=$("#fotona").offset(), x1=$("#fotona").width(), y1=$("#fotona").height();
      $("#fotona, #naviga").stop().animate({ width: x, height: y, top: (o.top+(y1-y)/2), left: (o.left+(x1-x)/2) }, 500).not("#naviga").attr("src", $("#fotonatmp").attr("src"));
      $("#fotonatmp").remove();
    }
    $("#sx").unbind("click").bind("click", function(){ mostrafoto( i-1 ); }).tooltip(" foto precedente ");
    $("#dx").unbind("click").bind("click", function(){ mostrafoto( i+1 ); }).tooltip(" foto successiva ");
  }).error(function () {
     $("#popupsfondo").hide(); $("#fotona, #naviga").remove(); 
  }).attr("src", $("a img:eq("+i+")").parent().attr("href"));
}
