window.onAjaxLoad = function(){};
(function($){
  $(function(){
    //link esterni
    $("a.spip_out").attr("target","_blank");
  
    //menu fixed per IE
    if($.browser.msie) {
      var cont = $("#container").css({position:"relative"});
      var menu = cont.find(">div:first");
      menu.css({position:"absolute",left:"0"});
      var w = $(window);
      var updateMenu = function() {
        if(menu.is(":animated"))
          menu.stop();
        menu.animate({top:document.documentElement.scrollTop+"px"},'fast');
      }
      w.scroll(updateMenu);
    }
    
    //calcola larghezza barra
    var dummy = $("<div><br /><br /><br /></div>").css({overflow:"auto"});
    $("body").append(dummy);
    var w1 = dummy[0].clientWidth;
    dummy.css({overflow:"scroll"});
    var w2 = dummy[0].clientWidth;
    dummy.remove();
    var scrollbarWidth = w1-w2;
    
  
    $.easing.def = "jswing";
    var cont = $("div.column.second");
    var items = $("div.item",cont);
    var players = items.find("a.player");
    var profile = $("body").is(".profile");
    var home = $("body").is(".home");
    var mtop = profile?110:73;
    var lastScrollEl = items.eq(0);
    var itemh;
    
    var setupImages = function() {
      if(!cont.length)
       return;
      var top, index = items.index(lastScrollEl);
      if(index == 0) {
        top = 0;
      } else {
        if(!profile)
          lastScrollEl.prevAll().height("auto");
        top = lastScrollEl.offset().top+cont[0].scrollTop-mtop;
      }
      cont[0].scrollTop = top;
    }
    
    var fixMenuPosition = function() {
      if(!$.browser.msie && $(document).height()<=$(window).height()) {
        //c'è la barra - sposto a sx dei pixel della barra precedentemente calcolati
        var cl = $("#container").css("margin","0 auto").offset().left;
        $("#container").css({marginLeft:Math.ceil(cl-scrollbarWidth/2)+"px"});
      }    
    };
    
    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.logo").css({"marginBottom":0});
        }
        var h = $(this).height();
        max = Math.max(max,h);
      });
      itemh = profile?Math.max(wh-mtop,max):Math.max(wh-mtop-73,max);
      items.height(itemh);
      cont.height(itemh+mtop+(profile?0:73));
      setupImages();
    }
    
    if(!profile) {
      $(window).resize(resize);
      resize();
      items.slice(1).find("div.logo_bn").show();
    }
    
    if(home) {
      var cdata = document.cookie.split(/;/);
      cdata = $.map(cdata,function(n,i){
        if(n.search(/splash_zip=second/)==-1) return null;
        return n;
      })
      if(!cdata.length) {
        $("div.column:not(.first)").hide().filter(":last").css("display","inline");
        $("#first_menu li a").removeClass("on");
        $("#container").css("overflow","");
      }
    }
    
    //jump per barra
    $(window).resize(fixMenuPosition);
    fixMenuPosition();
    
    //nascondo arrows per dettaglio progetti
    if(!profile) {
      $("div.arrow_up,div.arrow_down:gt(0),div.arrow_down:last").css("visibility","hidden");
    }
    
    var hidePlayers = function() {
      players.find("object").remove();
      players.each(function(){
        $(this).next("div.player_image").appendTo(this).show();
      });
      
    }
    
    var speed = 500;
    
    var arrow_up_handler = function(){
      hidePlayers();
      var current_item = $(this).closest("div.item");
      var item = current_item.prev();
      if(!profile) {
        $("div.arrow_up,div.arrow_down").css("visibility","hidden");
        current_item.find("div.details").hide();
        current_item.find("div.logo_bn").animate({opacity:"show"},speed,"easeInQuad");
      }
      lastScrollEl = item;
      var top;
      if(items.index(item)==0)
        top = 0;
      else
        top = item.offset().top+cont[0].scrollTop-mtop;
      cont.scrollTo(top,speed*2,{
        easing: "easeOutExpo",
        onAfter:function(){
          if(profile) return;
          //item.find("div.arrow_up,div.arrow_down").css("visibility","visible");
          //item.find("div.details").slideDown(speed);
          //item.animate({height:itemh+"px"},speed);
        }
      });
      if(!profile) {
        item.find("div.arrow_up,div.arrow_down").css("visibility","visible");
        item.find("div.details").slideDown(speed);
        item.animate({height:itemh+"px"},speed);
        item.find("div.logo_bn").animate({opacity:"hide"},speed,"easeInQuad");
      }
      return false;
    };
    if(!profile)
      $("div.arrow_up").click(arrow_up_handler);
    
    var arrow_down_handler = function(){
      hidePlayers();
      var current_item = $(this).closest("div.item");
      //verifico fine loop, se si devo creare nuovo set di immagini
      if(items.index(current_item) == items.length-2) {
        //elimino il paddingBottom dall'ultimo elemento prima di clonare
        items.eq(items.length-1).css({paddingBottom:"0"});
        //clono con eventi
        var new_items = items.clone(true).appendTo(current_item.parent());
        //reimposto al primo elemento clonato il marginTop e la freccia verso l'alto
        var first_el = new_items.eq(0);
        first_el.css({marginTop:"22px"});
        $("<div class=\"arrow_up\"><a href=\"#\" title=\"\"><span>up</span></a></div>").
        prependTo(first_el.find("div.logo")).click(arrow_up_handler);
        //aggiungo i nuovi elementi
        items = items.add(new_items);
      }
      
      var item = current_item.next();
      var offset = item.offset(),dh = 0;
      if(!profile) {
        $("div.arrow_up,div.arrow_down").css("visibility","hidden");
        //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("div.details").hide();
        var h = current_item.height();
        current_item.css({height:"auto"});
        var hauto = current_item.height();
        dh = h - hauto;
        current_item.find("div.details").slideUp(speed);
        current_item.height(itemh).animate({height:hauto+"px"},speed);
        current_item.find("div.logo_bn").animate({opacity:"show"},speed,"easeInQuad");
      }
      lastScrollEl = item;        
      //current_item.queue(function() {
        var top = offset.top-dh+cont[0].scrollTop-mtop;
        cont.scrollTo(top,speed*2,{
          easing: "easeOutExpo",
          onAfter:function(){
            if(profile) return;
            //item.find("div.arrow_up,div.arrow_down").css("visibility","visible");
            //item.find("div.details").slideDown(speed);
            //item.animate({height:itemh+"px"});
          }
        });
        if(!profile) {
          item.find("div.arrow_up,div.arrow_down").css("visibility","visible");
          item.find("div.details").slideDown(speed);
          item.animate({height:itemh+"px"},speed);
          item.find("div.logo_bn").animate({opacity:"hide"},speed,"easeInQuad");
        }
      //  $(this).dequeue();
      //});
      return false;
    };
    
    if(!profile)
      $("div.arrow_down").click(arrow_down_handler);
  });
})(jQuery);
