jQuery(function($) {
  $('.mini-thumbnail a').tooltip({
    delay: 0,
    showURL: false,
    fade: 250,
    track: true,    
    top : -200,
    bodyHandler: function() {
      return $("<img/>").attr("src", this.href);
    }
  });
  $(".mini-thumbnail a").click(function(){
    return false;
  });

  // Onglet images
  $('ul.carousel-nav').galleria({
    insert: '#carousel',
    clickNext: false,
    history: false,
    onImage: function(image,caption,thumb) { // let's add some image effects for demonstration purposes
      // fade in the image & caption
      if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
        image.css('display','none').fadeIn(1000);
      }
      caption.css('display','none');

      // Grab the image's dimensions
      var imgH = image.attr('height');
      var imgW = image.attr('width');
      
      // Find which dimension is scaled the most
      var scaleH = 280 / imgH;
      var scaleW = 420 / imgW;
      // Scale the image
      if (scaleH < scaleW) {
        image.attr('height','280');
        image.attr('width', Math.round(imgW * scaleH));
        // alert(Math.round(imgW * scaleH));
      } else {
        image.attr('width','420');
        image.attr('height', Math.round(imgH * scaleW));
      }
      
      // fetch the thumbnail container
      var _li = thumb.parents('li');
      // fade out inactive thumbnail
      _li.siblings().children('img.selected').fadeTo(500,1);
      // fade in active thumbnail
      thumb.fadeTo('fast',1).addClass('selected');
      image.attr('title','');
      var _a = thumb.parents('a');
      // Construction de l'url complète de l'image
      src = "http://" + location.hostname + "/" + image.attr('src').substring(image.attr('src').indexOf("#") + 1);
      // Insertion du lien dans la blog this de l'image
      $("#blog-this-image").val("<a href=\"" + location.href + "\"><img src=\"" + src + "\" alt=\"\" /></a>");
      // Insertion dans la carousel du lien + image
      $("#carousel .galleria_wrapper").html("<a id=\"fancy-image\" href=\"" + image.attr('src').substring(image.attr('src').indexOf("#") + 1) + "\"></a>");
      
      
      // Initialisation de la lightbox
      $("#fancy-image").append(image);
      $("a#fancy-image").fancybox({'hideOnContentClick': true});
      // Image x sur y
      $("#image-current").html($(".carousel-nav img").index(thumb) + 1);
    },
    onThumb : function(thumb) { // thumbnail effects goes here
      // fetch the thumbnail container
      var _li = thumb.parents('li');
      // if thumbnail is active, fade all the way.
      var _fadeTo = _li.is('.active') ? '1' : '1';
      // fade in the thumbnail when finnished loading
      thumb.css({
        display:'none',
        opacity:_fadeTo
      }).fadeIn(1500);
      // hover effects
      thumb.hover(
        function() { thumb.fadeTo('fast', 1); },
        function() { _li.not('.active').children('img').fadeTo('fast',1); } // don't fade out if the parent is active
      )
    }
  });
  // Onglet images miniatures à droite
  // Précédentes
  $("#images .scroll-prev").click(function(){
    $(".carousel-nav").scrollTo({top:'-=200px'}, 500);
    return false;
  });
  // Suivantes
  $("#images .scroll-next").click(function(){
    $(".carousel-nav").scrollTo({top:'+=200px'}, 500);
    return false;
  });
  // Commentaire, widget note
  $("#stars-wrapper").stars({
      inputType: "select"
  });

  // Onglet vidéos
  $("ul.video-nav a").click(function(){
    var li = $(this).parents('li');
    var player = li.children("textarea").text();
    li.siblings().removeClass("active");
    li.addClass("active");
    $("#blog-this-video").val(player);
    $("#video-current").html($(".video-nav li").index(li) + 1);
    $("#video").html(player);
    return false;
  });
  // Lien vidéo précédente
  $("#videos a.prev-video").click(function(){
    var li_active = $(".video-nav li.active");
    li_active = li_active.prev();
    if ($(".video-nav li").index(li_active) < 0) {
      return false;
    } else {
      var li = $(".video-nav li");
      li.removeClass("active");
      li_active.addClass("active");
      var player = li_active.children("textarea").text();
      $("#blog-this-video").val(player);
      $("#video-current").html($(".video-nav li").index(li_active) + 1);
      $("#video").html(player);
      $(".video-nav").scrollTo(li_active, 500);
      return false;
    }
  });
  // Lien vidéo suivante
  $("#videos a.next-video").click(function(){
    var li_active = $(".video-nav li.active");
    li_active = li_active.next();
    if ($(".video-nav li").index(li_active) == $(".video-nav li").index("li:last")) {
      return false;
    } else {
      var li = $(".video-nav li");
      li.removeClass("active");
      li_active.addClass("active");
      var player = li_active.children("textarea").text();
      $("#blog-this-video").val(player);
      $("#video-current").html($(".video-nav li").index(li_active) + 1);
      $(".video-nav").scrollTo(li_active, 500);
      $("#video").html(player);
      return false;
    }
  });

  // Onglet images miniatures à droite
  // Précédentes
  $("#videos .scroll-prev").click(function(){
    $(".video-nav").scrollTo({top:'-=200px'}, 500);
    return false;
  });
  // Suivantes
  $("#videos .scroll-next").click(function(){
    $(".video-nav").scrollTo({top:'+=200px'}, 500);
    return false;
  });

  // Au chargement de la page on active la première vidéo
  $(document).ready(function(){
    var li_active = $(".video-nav li:first");
    li_active.addClass("active");
    var player = li_active.children("textarea").text();
    $("#blog-this-video").val(player);
    $("#video-current").html($(".video-nav li").index(li_active) + 1);
    $("#video").html(player);
    return false;
  });
});
