window.onAjaxLoad = function(){};
(function($){
  $(function(){
    $.easing.def = "jswing";
    var cont = $("#dx");
    if($.browser.msie) {
      cont.css({position:"relative"});
    }
    var items = $("div.project_detail",cont);
    var players = items.find("a.player");
    var mtop = 129;
    var lastScrollEl = items.eq(0);
    var itemh;
    var bottomHintHeight = 20;
    
    var setupImages = function() {
      if(!cont.length)
       return;
      var top, index = items.index(lastScrollEl);
      if(index == 0) {
        top = 0;
      } else {
        lastScrollEl.prevAll().height("auto");
        top = lastScrollEl.offset().top+cont[0].scrollTop-mtop;
      }
      cont[0].scrollTop = top;
    }
    
    var resize = function() {
      var wh = $(window).height();
      var max = 0;
      items.css("height","auto").each(function(){
        if($.browser.msie) {
          $(this).css({zoom:1});//.find("div.img_item").css({zoom:1});
        }
        var h = $(this).height();
        max = Math.max(max,h);
      });
      itemh = Math.max(wh-2*mtop/*-bottomHintHeight*/,max);
      items.height(itemh);
      cont.height(itemh+2*mtop/*+bottomHintHeight*/);
      setupImages();
    }
    
    if($("#dx.slideshow").length) {
      $(window).resize(resize);
      resize();
    }
    //items.slice(1).find("div.logo_bn").show();
    
    //nascondo arrows per dettaglio progetti
    /*
    if($.browser.msie && $.browser.version==6) {
      $("div.up a img:eq(1),div.down a img:eq(1)").css("visibility","hidden");
      items.find("div.up,div.down").hover(function(){
        $("a img:eq(1)",this).width(14).css("visibility","visible");
      },
      function() {
        $("a img:eq(1)",this).css("visibility","hidden");
      });
      items.slice(1).find("div.img_item a img:eq(1)").css("visibility","hidden");
    } else {
      $("div.up a img,div.down a img").css({opacity:0});
      items.find("div.up,div.down").hover(function(){
        $("a img",this).fadeTo(200,1);
      },
      function() {
        $("a img",this).fadeTo(200,0);
      });
      items.slice(1).find("div.img_item a img").css({opacity:0});
    }
    */
    items.find("div.img_item").hover(function(){
      var w = $(this).find(">img,>div.jScrollPaneContainer").width();
      $(this).parent().find("span.desc").css("display","block").
      width(w);
    },function() {
      $(this).parent().find("span.desc").css("display","none");
    }).find(">img,>div.jScrollPaneContainer img").slice(1).css("opacity",0.2);
    
    var hidePlayers = function() {
      players.find("object").remove();
      players.each(function(){
        $(this).next("div.player_image").appendTo(this).show();
      });
      
    }
    
    
    var speed = 500;
    $("a.arrow_up").click(function(){
      $(this).closest("div.project_detail").find("div.up").click();
    });
    $("div.up").click(function(){
      hidePlayers();
      var current_item = $(this).closest("div.project_detail");
      var item = current_item.prev();
      $("div.up,div.down").hide();
      current_item.find("p").slideUp(speed);
      current_item.find("div.img_item>img,div.img_item>div.jScrollPaneContainer img").animate({opacity:0.2},speed)
      //current_item.find("div.logo_bn").animate({opacity:"show"},speed,"easeInQuad");
      lastScrollEl = item;
      var top;
      if(items.index(item)==0)
        top = 0;
      else
        top = parseInt(item.offset().top+cont[0].scrollTop-mtop);
      cont.scrollTo(top,speed*2,{
        easing: "easeOutExpo"
      });
      item.find("div.up,div.down").show();
      item.find("p").slideDown(speed);
      item.animate({height:itemh+"px"},speed);
      item.find("div.img_item>img,,div.img_item>div.jScrollPaneContainer img").animate({opacity:1},speed);
      //item.find("div.logo_bn").animate({opacity:"hide"},speed,"easeInQuad");
      return false;
    });
    $("a.arrow_down").click(function(){
      $(this).closest("div.project_detail").find("div.down").click();
    });
    $("div.down").click(function(){
      hidePlayers();
      var current_item = $(this).closest("div.project_detail");
      var item = current_item.next();
      var offset = item.offset(),dh = 0;
      $("div.up,div.down").hide();
      //compatto spazio tra l'immagine corrente e la sottostante
      //con un'animazione. Per conoscere l'altezza finale dell'animazione,
      //nascondo i dettagli, imposto l'altezza del contenitore ad auto
      //e ne leggo il valore reale in px.
      current_item.find("p").hide();
      var h = current_item.height();
      current_item.css({height:"auto"});
      var hauto = current_item.height();//+mtop-bottomHintHeight;
      dh = h - hauto;
      current_item.find("p").slideUp(speed);
      current_item.height(itemh).animate({height:hauto+"px"},speed);
      current_item.find("div.img_item>img,div.img_item>div.jScrollPaneContainer img").animate({opacity:0.2},speed);
      //current_item.find("div.logo_bn").animate({opacity:"show"},speed,"easeInQuad");
      lastScrollEl = item;        
      //current_item.queue(function() {
        var top = parseInt(offset.top-dh+cont[0].scrollTop-mtop);
        cont.scrollTo(top,speed*2,{
          easing: "easeOutExpo"
        });
        item.find("div.up,div.down").show();
        item.find("p").slideDown(speed);
        item.animate({height:itemh+"px"},speed);
        item.find("div.img_item>img,div.img_item>div.jScrollPaneContainer img").animate({opacity:1},speed)
        //item.find("div.logo_bn").animate({opacity:"hide"},speed,"easeInQuad");
      //  $(this).dequeue();
      //});
      return false;
    });
    
    //gestione immagini larghe
    var ww = $(window).width()-195-48-48;//195 è il menu sx, 48 il margine a sx, 48 il margine a dx
    var maxw = Math.max(803,ww);
    $("#dx, #dx div.project_detail").width(maxw);
    $("#container").width(maxw+195);
    items.find("img").each(function(){
      $this = $(this);
      var w = $this.width();
      if(w>maxw) {
        var cont = $this.wrap("<div style='width:"+maxw+"px;float:left;overflow:auto;line-height:0'></div>").parent();
        if($.browser.msie)
          cont.css({paddingBottom:"10px"});
        cont.jScrollHorizontalPane();
        cont.siblings("div.jScrollPaneTrack").width(maxw);
      }
    });
    
    //link esterni
    $("a.spip_out").attr("target","_blank");
    
    
  });
})(jQuery);

