(function($){
  $.fn.diaporama = function(options) {

    var defaults = {
      delay: 4,
      animationSpeed: "normal",
      controls:true
    };
        
    var options = $.extend(defaults, options);
    
    this.each(function() {    
      var obj = $(this);
      
      // Initialisation de la hauteur de la boite
      var height = 0;
      
      $(obj).find("li img").each(function() {
        if ($(this).attr('min_height') > height) {
          height = $(this).attr('min_height');
          $(obj).css({
            'min-height': height + 'px'
          });
        }
      });
      
      if($(obj).find("li").length > 1) {
        var inter = setInterval(function(){nextElt(options)}, (options.delay*1000));
        var sens = "right";
        var pause = false;
        
        $(obj).find("li").hide();
        $(obj).find("li:first-child").addClass("active").fadeIn(options.animationSpeed);
        
        // Controls
        
        if(options.controls)
        {
          $(obj).after("<div class='diaporama_controls'><div class='btns'><span class='prev'>Prec.</span> <span class='pause'>Pause</span> <span class='next'>Suiv.</span></div></div>");
          
          $(obj).siblings().find(".prev").click(function(){
            clearInterval(inter);
            prevElt(options);
            if(!pause)
              inter = setInterval(function(){prevElt(options)}, (options.delay*1000));
            sens = "left";
          });
          
          $(obj).siblings().find(".next").click(function(){
            clearInterval(inter);
            nextElt(options);
            if(!pause)
              inter = setInterval(function(){nextElt(options)}, (options.delay*1000));
            sens = "right";
          });
                          
          $(obj).siblings().find(".pause").toggle(
            function(){
              $(this).removeClass("pause").addClass("play");
              clearInterval(inter);
              pause = true;
            },
            function(){
              $(this).removeClass("play").addClass("pause");
              inter = setInterval(function(){ (sens == "right")?nextElt(options):prevElt(options)}, (options.delay*1000));
              pause = false;
            }
          );
        }
        
        // Affiche l'élément suivant
        
        function nextElt(options)
        {
          $(obj).find("li.active").fadeOut(options.animationSpeed);
          
          if(!$(obj).find("li.active").is(":last-child"))
          {
            $(obj).find("li.active").next().addClass("active").prev().removeClass("active");
            $(obj).find("li.active").fadeIn(options.animationSpeed);
            
          }
          else
          {
            $(obj).find("li:first-child").addClass("active").fadeIn(options.animationSpeed);
            $(obj).find("li:last-child").removeClass("active");
          }
        }
        
        // Affiche l'élément précédent
        
        function prevElt(options)
        {
          $(obj).find("li.active").fadeOut(options.animationSpeed);
          
          if(!$(obj).find("li.active").is(":first-child"))
          {
            $(obj).find("li.active").prev().addClass("active").next().removeClass("active");
            $(obj).find("li.active").fadeIn(options.animationSpeed);
            
          }
          else
          {
            $(obj).find("li:last-child").addClass("active").fadeIn(options.animationSpeed);
            $(obj).find("li:first-child").removeClass("active");
          }
        }
      }
    });
    
    return this;
  };
})(jQuery);
