(function($)
{
  
  var defaults = {
    max_img: 4,
    split_id: '_',
    id_index: 2
  }, windowLoaded = false;

  $(window).bind('load.jslideshow', function() { windowLoaded = true; });
  
  $.jslideshow = function(e, o) {
    this.options = $.extend({}, defaults, o || {});
    
    this.locked          = false;
    this.current_num     = 1;
    
    var self = this;
       
    if (!windowLoaded && $.browser.safari) {
      $(window).bind('load.jslideshow', function() { self.setup(); });
    } else {
      this.setup();
    }
  };
  
  var $jc = $.jslideshow;

  $jc.fn = $jc.prototype = {
      jslideshow: '0.0.1'
  };

  $jc.fn.extend = $jc.extend = $.extend;

  $jc.fn.extend({
    
    setup: function() {

      this.timer = null;

      if (this.locked) {
          return;
      }
      var self = this;
      $("#slideshow_nav li").bind('click', function() {
      	// On récupère le numéro du bouton cliqué
      	if ($.browser.msie && $.browser.version == '6.0') alert($(this).prevAll('li').length+1);
      	self.displaySlide($(this).prevAll('li').length+1);
    
      	return false;
      });
      
      $("#slideshow").bind('mouseenter', function() {
      	self.stopslide();
      });

      $("#slideshow").bind('mouseleave', function() {
      	self.startslide();
      });
      this.startslide();
      
    },
    
    displaySlide: function(next_num) {
      // On récupère le numéro de l'image déjà affichée, on la masque et on change l'image du bouton
    	if ($.browser.msie)
    	  $("#slideshow_img_" + this.current_num).hide();
    	else
    	  $("#slideshow_img_" + this.current_num).fadeOut(500);  
    	$("#slideshow_nav_" + this.current_num + " a").removeClass("selected");
    
    	// On change l'image du bouton
    	if ($.browser.msie)
    	  $("#slideshow_img_" + next_num).show();
    	else
    	  $("#slideshow_img_" + next_num).fadeIn(1000);
      $("#slideshow_nav_" + next_num + " a").addClass("selected");
      	
    
    	this.current_num = next_num;
    },
    
    displayNextSlide: function() {
    	// On récupère le numéro de la prochaine image en prenant le compte le cas
    	// où l'image sélectionnée est la dernière
    	var next_num = parseInt(this.current_num) + 1;
    	if (next_num == (this.options.max_img + 1)) {
    		next_num = 1;
    	}
    	this.displaySlide(next_num);
    	this.stopslide();
    	this.startslide();
    },
    
    stopslide: function() {
      if (this.timer === null) {
        return;
      }
      window.clearTimeout(this.timer);
      this.timer = null;
    },
    
    startslide: function() {
      if (this.timer !== null) {
        return;
      }
      var self = this;
      this.timer = window.setTimeout(function() { self.displayNextSlide(); }, 4000);
    }
    
  });
  
  $.fn.jslideshow = function(o) {
    if (typeof o == 'string') {
      var instance = $(this).data('jslideshow'), args = Array.prototype.slice.call(arguments, 1);
      return instance[o].apply(instance, args);
    } else {
      return this.each(function() {
        $(this).data('jslideshow', new $jc(this, o));
      });
    }
  };

}(jQuery)); 	

$(function() {
  $("#slideshow_nav_1 a").addClass("selected");
  $("#slideshow_img_1").show();
  $("#slideshow").jslideshow({});
});
