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

var carrousel = {

  nbSlide: 0,
  nbCurrent: 1,
  elem: null,
  elemCurrent : null,
  timer: null,
  timeInterval: 5000,

  init : function(elem)
  {
    this.elem = jQuery(elem);
    this.nbSlide = this.elem.find('.slide').length;

    this.elem.append('<div class="navigation"></div>');
    for(var i=1;i<=this.nbSlide;i++)
    {
      this.elem.find(".navigation").append('<span class="' + i + '"></span>');
    }

    this.elem.find('.navigation span').click(function(){ carrousel.gotoSlide(jQuery(this).attr("class")); });
    
    this.elem.find('.title').css({'opacity':'0.7'});


    this.elem.find('.slide').hide();
    this.elemCurrent = this.elem.find('.slide:first');
    this.elemCurrent.show();
    this.elem.find('.navigation span:first').addClass('active');

    carrousel.play();

    this.elem.bind('mouseenter', function(){
      carrousel.stop();
    }).bind('mouseleave', function(){
      carrousel.play();
    });
  },
  
  gotoSlide : function(num)
  {
    if (this.nbCurrent == num)
    {
      return false;
    }
    
    var titleHeight = this.elemCurrent.find('.title').height();
    var sens = 1;
    if (num < this.nbCurrent)
    {
      sens = -1;
    }
    var cssDeb = {'left':sens*this.elemCurrent.find('.visu').width()};
    var cssFin = {'left':-sens*this.elemCurrent.find('.visu').width()};
    

    this.elemCurrent.find('.title').animate({'bottom':-titleHeight}, 500);
    this.elem.find('#slide' + num + ' .title').css({'bottom':-titleHeight}).animate({'bottom':0}, 500);
    this.elemCurrent.find('.visu').fadeOut();
    this.elem.find('#slide' + num).show();
    this.elem.find('#slide' + num + ' .visu').hide().fadeIn();

    this.elem.find('.navigation span').removeClass('active');
    this.elem.find('.navigation span:eq(' + (num-1) + ')').addClass('active');
    
    this.elemCurrent = this.elem.find('#slide' + num);
    this.nbCurrent = num;
  },

  next : function()
  {
    var num = this.nbCurrent + 1;
    if (num > this.nbSlide)
    {
      num = 0;
    }
    this.gotoSlide(num);
  },
  
  previous : function()
  {
    var num = this.nbCurrent - 1;
    if (num < 1)
    {
      num = this.nbSlide;
    }
    this.gotoSlide(num);
  },

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

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