(function($) {
  var newMethods = {
    centra: function() { 
      return this.each( function(){ 
        $(this).css({ top: parseInt($(window).scrollTop()+(($(window).height()-$(this).outerHeight())/2))+"px", left: parseInt(($(window).width()-$(this).outerWidth())/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; max-width: 400px;'></div>");
      }
      return this.each( function(){ 
        $(this)
          .bind({
            mouseleave: function(e){$("#tooltip").html("").hide();},
            mouseenter: function(e){$("#tooltip").html(txt).show().css({left: (e.pageX+10)+"px", top: (e.pageY+10)+"px"});},
            mousemove:  function(e){$("#tooltip").css({left: (e.pageX+10)+"px", top: (e.pageY+10)+"px"});}
          });
      });
    }
  };
  $.each(newMethods, function(i) { $.fn[i] = this; });
})(jQuery);

var diap, slideon=false, stimer, idiap;

$(function(){
  $("#gallerie .tooltip").parent().parent().css({cursor: "pointer"}).click(function(){location.assign($("a", this).attr("href"));});
  
  $("body").append("<div id='popupsfondo' style='position: absolute; background-color:#000000; display: none;'></div> \
    <img src='http://www.sportrentino.it/portale/novitast/grafica/attesa.gif' alt='' id='attesa' style='position: absolute; display: none; z-index: 200; ' border='0'> \
    <img src='http://www.sportrentino.it/portale/novitast/grafica/asinistra.gif' alt='' id='fotoprec'> <img src='http://www.sportrentino.it/portale/novitast/grafica/adestra.gif' alt='' id='fotosucc'> \
    <div id='fotodesc' style='display: none; z-index: 101; width: 500px; position: absolute; background-color: Black; color: White; padding: 5px 0; text-align: center; font-size: 9pt;'></div>");
  
  $("#popupsfondo").tooltip(" cliccare qui per chiudere la fotografia/video ");
  
  $("#fotoprec, #fotosucc").css({position: "absolute", backgroundColor: "#ffffff", display: "none", color: "Black", fontSize: "15px", padding: "5px", zIndex: "200", opacity: 0.8 });
  
  $("#diapositive .tooltip")
    .hide()
    .parent()
    .css({opacity: 0.5, cursor: "pointer"})
    .each(function(i, td){ 
      $(td)
      .tooltip($("div", td).html())
      .bind({
        mouseenter: function(){ $(this).fadeTo(200, 1).addClass("sfondo2"); },
        mouseleave: function(){ $(this).fadeTo(200, 0.5).removeClass("sfondo2"); },
        click: function(){
          $("#popupsfondo")
            .css({ top: "0px", left: "0px", width: $(window).width()+"px", height: $(document).height()+"px", opacity: 0.8, zIndex: 100})
            .show()
            .bind("click", chiudipop);
          mostrafoto(i);
        }
      });
    }); 
  diap=$("#diapositive img");
  
  $(window).load(function(){
    diap.filter(".youtube").each(function(i, img){
      var o=$(img).offset();
      $("body").append("<img src='http://www.sportrentino.it/portale/novitast/grafica/youtube.png' style='position: absolute; top: "+o.top+"px; left: "+o.left+"px; z-index: 20;'>");
    });
    if (fotorichiesta>0) { $("#fid"+fotorichiesta).parent().click(); }
  });
});      

function mostrafoto(i){
  clearTimeout(stimer);
  $("#tooltip, #fotodesc").html("").hide();
  $("#attesa").show().centra();
  if (diap.eq(i).length==0 || i<0) {i=0}
  idiap=diap.eq(i);
  if (idiap.is(".youtube")) {
    var yh=400, yw=600; slideon=false;
    $("#attesa").hide();
    $("#fotona").remove();
    var code=idiap.attr("alt").split("|");
    if ($("#youtube").length==0) {
      $("<div id='youtube' style='z-index: 101; width: "+yw+"px; height: "+yh+"px; position: absolute; background-color: Black; color: White; padding: 5px 0; text-align: center;'></div>")
      .appendTo("body").centra();
    }
    var html="";
    if (code.length==2) {html="<object width='480' height='385'>\
      <param name='movie' value='http://www.youtube.com/p/"+code[1]+"&hl=it_IT&fs=1'></param><param name='allowFullScreen' value='true'></param>\
      <param name='allowscriptaccess' value='always'></param><embed src='http://www.youtube.com/p/"+code[1]+"&hl=it_IT&fs=1' type='application/x-shockwave-flash' \
      width='480' height='385' allowscriptaccess='always' allowfullscreen='true'></embed></object>"; }
    else { html="<object width='480' height='385'><param name='movie' value='http://www.youtube.com/v/"+code[0]+"&rel=1'></param>\
      <param name='wmode' value=''></param><embed src='http://www.youtube.com/v/"+code[0]+"&rel=1' type='application/x-shockwave-flash' width='480' height='385'></embed></object>"; }
    var o=$("#youtube").html(html).offset();
    $("#youtube")
      .unbind("mousemove").unbind("mouseout").unbind("click")
      .bind({
        click: function(e){ 
          $("#youtube").unbind("click").unbind("mousemove"); 
          if (e.pageX>((yw/2)+o.left)) { mostrafoto( i+1 ) } else { mostrafoto( i-1 ) }
        },
        mousemove: function(e){
          if (e.pageX>((yw/2)+o.left)) { $("#fotosucc").show(); $("#fotoprec").hide(); } else { if (i>0) { $("#fotoprec").show(); } $("#fotosucc").hide(); }
        },
        mouseout: function(e){ $("#fotosucc, #fotoprec").hide() }
      })
      .find("embed")
      .unbind("click");
    $("#fotodesc").html($("#diapositive .tooltip:eq("+i+")").html()).css({top: (o.top+yh+10)+"px", left: o.left+"px", width: yw+"px" }).slideDown(300);
    $("#fotoprec").hide().animate({top: (o.top+10)+"px", left: o.left+"px"}, 500);
    $("#fotosucc").hide().animate({top: (o.top+10)+"px", left: (o.left+yw-$("#fotosucc").outerWidth())+"px"}, 500);
  } else {
    $("#youtube").remove();
    $("#fotona").fadeTo(100, 0.6);
    var img=new Image(), w, h, o, f;
    $(img).bind("load", function(){
      $("#attesa").hide();
      if ($("#fotona").length==0) {
        f=$(this).attr("id", "fotona").css({display: "none", position: "absolute", zIndex: 101}).appendTo("body").fadeIn(500, showdesc).unbind("load").centra();
        o=$(this).offset();
        w=$(this).width();
        h=$(this).height();
      } else {
        w=$(this).css("display","none").appendTo("body").width();
        h=$(this).height();
        f=$("#fotona");
        o=f.offset();
        o={top: parseInt(o.top+(f.height()-h)/2), left: parseInt(o.left+(f.width()-w)/2)};
        f.fadeTo(100, 0.1, function(){f.attr("src", $(img).attr("src"));})
          .delay(100)
          .animate({ width: w+"px", height: h+"px", top: o.top+"px", left: o.left+"px", opacity: 1 }, 500, showdesc)
          .unbind("click").unbind("mousemove").unbind("mouseout");
      }
      $("#fotoprec").hide().animate({top: (o.top+10)+"px", left: o.left+"px"}, 500).unbind("click").bind({
        mouseenter: function(){ $(this).show() },
        mouseleave: function(){ $(this).hide() },
        click: function(){ mostrafoto( i-1 ) }
      });
      $("#fotosucc").hide().animate({top: (o.top+10)+"px", left: (o.left+w-$("#fotosucc").outerWidth())+"px"}, 500).unbind("click").bind({
        mouseenter: function(){ $(this).show() },
        mouseleave: function(){ $(this).hide() },
        click: function(){ mostrafoto( i+1 ) }
      });
      $("#fotodesc").html($("#diapositive .tooltip:eq("+i+")").html()+(slideon==true?"<div onClick=\"fncstop("+i+");\">ferma slideshow</div>":"<div onClick=\"fncshow("+i+");\">avvia slideshow</div>"))
        .css({top: (o.top+h)+"px", left: o.left+"px", width: w+"px" });
      f.bind({
        click: function(e){ 
          f.unbind("mousemove");
          clearTimeout(stimer);
          slideon=false;
          if (e.pageX>(w/2+o.left)) { mostrafoto( i+1 ); } 
          else { mostrafoto( i-1 ); } 
        },
        mousemove: function(e){ 
          if (e.pageX>((w/2)+o.left)) { $("#fotosucc").show(); $("#fotoprec").hide(); } 
          else { if (i>0) { $("#fotoprec").show(); } $("#fotosucc").hide(); } 
        },
        mouseleave: function(e){ $("#fotosucc, #fotoprec").hide(); }
      });
    })
    .error(chiudipop)
    .attr("src", idiap.attr("src").replace(/\/anteprime/,""));
  }
}

function fncshow(i){ 
  clearTimeout(stimer); 
  slideon=true; 
  stimer=setTimeout("mostrafoto("+(i+1)+")", 3000);
  if (diap.eq(i+1).length!=0 && ! diap.eq(i+1).is(".youtube")){ var img=new Image(); $(img).attr("src", diap.eq(i+1).attr("src").replace(/\/anteprime/,"")); }
  $("#fotodesc div:last").remove();
  $("#fotodesc").append("<div onClick=\"fncstop("+i+");\">ferma slideshow</div>");
}

function fncstop(i){ clearTimeout(stimer); slideon=false; $("#fotodesc div:last").remove(); $("#fotodesc").append("<div onClick=\"fncshow("+i+");\">avvia slideshow</div>"); }

function showdesc(){ $("#fotodesc").slideDown(300, function(){ if (slideon==true) { fncshow(diap.index(idiap)); }}); }

function chiudipop(){ $("#popupsfondo, #fotosucc, #fotoprec, #fotodesc, #attesa").hide(); $("#fotona, #youtube").remove(); clearTimeout(stimer); slideon=false; }

