/*
****************************************
*
*  (c) YZALIS
*
****************************************
*/

var slider = {
  
  elem : null,
  slider : null,
  largeurCache : null,
  largeur : 0,
  jump : null,
  nbStep : 0,
  currentStep : 0,
  timer : null,
  timeInterval : 5000,
  timeAnimation : 500,

  init: function(elem)
  {
    this.elem = jQuery(elem);
    this.slider = this.elem.find('.slider');
    this.largeurCache = this.elem.width();
    this.elem.find('a').each(function(){
      slider.largeur += jQuery(this).width();
      slider.largeur += parseInt(jQuery(this).css('margin-right'));
      slider.largeur += parseInt(jQuery(this).css('margin-left'));
      slider.largeur += parseInt(jQuery(this).css('padding-right'));
      slider.largeur += parseInt(jQuery(this).css('padding-left'));
    });

    this.elem.find('.prev').click(function(){ slider.prev(); });
    this.elem.find('.next').click(function(){ slider.next(); });
    this.jump = this.largeurCache + 8;
    this.nbStep = Math.ceil(this.largeur/this.jump - this.largeurCache/this.jump);
    
    this.elem.find('.navigation').append('<div class="step"></div>');
    for(var i = 0; i <= this.nbStep; i++)
    {
      this.elem.find('.step').append('<span class="' + i + '"></span>');
    }
    this.elem.find('.step span').click(function(){ slider.gotoSlide(jQuery(this).attr("class")); });
    this.elem.find('.step span:first').addClass('active');


    this.elem.bind('mouseenter', function(){
      slider.stop();
    }).bind('mouseleave', function(){
      slider.play();
    });

    this.play();
  },

  play : function()
  {
    this.timer = window.setInterval('slider.next()', this.timeInterval);
  },

  stop : function()
  {
    window.clearInterval(this.timer);
  },

  gotoSlide : function(step)
  {
    var sens = - 1;
    if (step < this.currentStep)
    {
      sens = 1;
    }
    
    if (step > this.currentStep)
    {
      this.slider.animate({'left':sens*step*this.jump}, this.timeAnimation);
    }
    else
    {
      this.slider.animate({'left':-sens*step*this.jump}, this.timeAnimation);
    }

    this.elem.find('.step span').removeClass('active');
    this.elem.find('.step span:eq(' + step + ')').addClass('active');
    
    this.currentStep = parseInt(step);
  },

  next : function()
  {
    var num = this.currentStep + 1;
    if (num > this.nbStep)
    {
      num = 0;
    }
    this.gotoSlide(num);
  },

  prev : function()
  {
    var num = this.currentStep - 1;
    if (num < 0)
    {
      num = this.nbStep;
    }
    this.gotoSlide(num);
  }
}