jQuery(document).ready(function() {
  $('div.box_exclusivites .racc-pdts').attr("id", "carousel");
  var lesSlides = $('div.box_exclusivites .racc-pdts .mention_particuliere') ;
  for (var i = 0 ; i<lesSlides.length ; i++ ) {
    var item = lesSlides.eq(i);
    item.attr("id", "slide" + (i+1) );
  } 
  if ($("body").attr('class').indexOf('rubrique_sommaire') == -1) { } 
  else {
    var carousel = {
      nbSlide : 0,
      nbCurrent : 1,
      elmtCurrent : null,
      elmt : null,
      timer : null,
      
      init : function(elmt){
        this.nbSlide = elmt.find(".mention_particuliere").length;
        
        //creation de la pagination
        elmt.append('<div class="navigation"></div>');
        for (var j = 1 ; j<=this.nbSlide ; j++){
          elmt.find(".navigation").append("<span>"+ j +"</span>")
        }
        elmt.find('.navigation span').click(function () {carousel.gotoSlide($(this).text())});
        
        //initialisation du carousel
        this.elmt = elmt;
        elmt.find(".mention_particuliere").hide();
        elmt.find(".mention_particuliere:first").show();
        this.elmtCurrent = elmt.find(".mention_particuliere:first");
        this.elmt.find(".navigation span:first").addClass("active");
        
        elmt.find(".mention_particuliere:first").css("display", "block");
        elmt.find(".mention_particuliere:first").css("left", "75px");
        
        //lancement du carousel
        carousel.play();
        
        // stop hover
        elmt.mouseover(carousel.stop);
        elmt.mouseout(carousel.play);
        
      },
      
      gotoSlide : function (num) {
        if(num == this.nbCurrent) {return false;}
        this.elmtCurrent.css("z-index", "3");
        this.elmtCurrent.animate({left: "200px"}, 'normal');
        //this.elmtCurrent.hide();
        //this.elmtCurrent.css("z-index", "-1");
        this.elmtCurrent.animate({left: "-200px"}, 'normal');
        this.elmt.find("#slide"+num).show();
        this.elmt.find("#slide"+num).animate({left: "75px"}, 'normal');
        this.elmt.find("#slide"+num).css("z-index", "5");
        this.elmt.find(".navigation span").removeClass("active");
        this.elmt.find(".navigation span:eq("+(num-1)+")").addClass("active");
        this.nbCurrent = num;
        this.elmtCurrent = this.elmt.find("#slide"+num);
      },
      
      next : function() {
        var num = this.nbCurrent+ 1;
        if (num > this.nbSlide) {
          num = 1;
        }
        this.gotoSlide(num);
      },
      
      prev : function() {
        var num = this.nbCurrent- 1;
        if (num < 1 ) {
          numt = this.nbSlide;
        }
        this.gotoSlide(num);
      },
      
      stop : function() {
        window.clearInterval(carousel.timer);
      },
      
      play : function() {
        //window.clearInterval(carousel.timer);
        carousel.timer = window.setInterval(function() {carousel.next();},3000);
      }


      
    }
    
    carousel.init($('#carousel'));
  }

});
