$(function() {
  /* Mise en route des onglets Description et Spécificités techniques */
  
  $("#description").siblings().hide();
  
  $("#center_column .corps .bloc .menu a").click(function() {
    var $elementDuMenu = $(this).parent();
    
    if (!$elementDuMenu.hasClass("active")) {
      $elementDuMenu.addClass("active").siblings().removeClass("active");
      
      $("#"+$(this).attr("className")).slideDown().siblings().slideUp();
    }
    
    return false;
  });
  
  
  
  /* Mise en route des formulaires d'achat */
  
  $("#lienAchatImmediat").click(function() {
    $("#form_achatImmediat").submit();
    
    return false;
  });
  
  $("#lienAjouterAuPanier").click(function() {
    $("#form_ajouterAuPanier").submit();
    
    return false;
  });
  
  
  
  /* Mise en route du vote */
  
  $("#liensLateraux .voter").hide();
  
  $("#liensLateraux .vote").mouseover(function() {
    $(this).css({ backgroundPosition: "bottom" });
    $(".voter", this).show();
    $(".note", this).hide();
  }).mouseout(function() {
    $(this).css({ backgroundPosition: "top" });
    $(".note", this).show();
    $(".voter", this).hide();
  });
  
  if (!$("#liensLateraux .voter").hasClass("dejaVote")) {
    $("#liensLateraux .voter .etoiles").mousemove(function(e) {
      var x = e.clientX - $(this).offset().left;
      var note = Math.ceil((x + 1) / 25);
      $("img", this).css({ top: -(note * 22)+"px" });
    }).mouseleave(function() {
      $("img", this).css({ top: "0" })
    }).click(function(e) {
      var x = e.clientX - $(this).offset().left;
      var note = Math.ceil((x + 1) / 25);
      
      $.get(
          base_dir_ssl+"themes/geeknstyle/aphp/noterUnProduit.php",
          { idProduit: id_product, idClient: id_customer, note: note },
          function(resultat) {
            if (resultat == "ok") {
              alert("Votre vote a bien été pris en compte.");
              window.location.reload();
            } else {
              alert(resultat);
            }
          },
          "text"
      );
    });
  } else {
    $("#liensLateraux .voter .etoiles img").css({ top: -(vote * 22)+"px" }).each(function() {
      $(this).parent().click(function() {
        alert("Vous avez déjà voté "+vote+"/5 pour ce produit.");
      });
    });
  }

  // Slider d'images

  var flecheHaut = $("#imageUp");
  var flecheBas = $("#imageDown");
  var position = 0;
  var NB_POSITIONS = $("#imageSlider li").size();
  
  var listeImages = $("#imageSlider ul")[0];
  var HAUTEUR_IMAGE = 100;
  var DUREE_DE_L_INTERPOLATION = 750; /* en ms */
  var TYPE_D_ACCELERATION = "easeInOutQuart";

  if ( NB_POSITIONS <= 2 ) {
      $('#imagePager').hide();
  }
  else {
      $(flecheHaut).click(function() {
          if (--position < 0) {
            position += NB_POSITIONS - 1; // On se place sur l'avant dernier element
          }

          $(listeImages).stop().animate(
              { top: (-position * HAUTEUR_IMAGE)+"px" },
              DUREE_DE_L_INTERPOLATION,
              TYPE_D_ACCELERATION
          );

        return false;
      });

      $(flecheBas).click(function() {
          position = (position + 1) % (NB_POSITIONS - 1); // On retourne au début après l'avant derniere image

          $(listeImages).stop().animate(
              { top: (-position * HAUTEUR_IMAGE)+"px" },
              DUREE_DE_L_INTERPOLATION,
              TYPE_D_ACCELERATION
          );

        return false;
      });

      $(window).load(function() {
          for (var i = 0; i < NB_POSITIONS - 1; ++i) {
              setTimeout("$(\"#imageDown\").click();", (i + .75) * DUREE_DE_L_INTERPOLATION * 1);
          }
      });
  }
  
});
