/* ------------------------------------------------------------------------
	Objet: Slider
	Auteur: Jonathan Boyer (http://www.grafikart.fr)
	Version: 1.0
------------------------------------------------------------------------- */

$(document).ready(function() {
    s = new slider("#SliderVideos", "li", 1);
});

/*
@elem : Div contenant le slider
@content : les élément dans le slider,    "a" par défaut
@ratio : De combien de fois on bouge
*/
var slider = function(elem, content, ratio) {
    var self = this;
    this.div = $(elem);
    this.slider = this.div.find(".slider");
    this.content = content == undefined ? this.slider.find("a") : this.slider.find(content);
    ratio = ratio == undefined ? 1 : ratio;
    this.suiv = this.div.find('.suiv');
    this.prec = this.div.find('.prec');
    this.largeurCache = this.div.width();
    this.largeur = 0;
    this.content.each(function() {
        self.largeur += $(this).width();
        self.largeur += parseInt($(this).css("margin-left"));
        self.largeur += parseInt($(this).css("margin-right"));
        self.largeur += parseInt($(this).css("padding-left"));
        self.largeur += parseInt($(this).css("padding-right"));
    });
    this.numCourant = 0;
    this.saut = this.largeurCache * ratio;
    this.etapes = Math.ceil((this.largeur / this.saut) - (this.largeurCache / this.saut));
    this.prec.hide();
    if (this.largeurCache > this.largeur) { this.suiv.hide(); }

    // Avancer d'un cran
    this.next = function() {
        if (self.numCourant >= self.etapes) { return false; }
        if (self.numCourant == 0) { self.prec.fadeIn(); }
        self.numCourant++;
        self.div.find(".slider").animate({
            "left": -self.saut * self.numCourant
        }, 1000);
        if (self.numCourant >= self.etapes) { self.suiv.fadeOut(); }
    }

    // Reculer d'un cran
    this.prev = function() {
        if (self.numCourant <= 0) { return false; }
        if (self.numCourant >= self.etapes) { self.suiv.fadeIn(); }
        self.numCourant--;
        self.div.find(".slider").animate({
            "left": -self.saut * self.numCourant
        }, 1000);
        if (self.numCourant == 0) { self.prec.fadeOut(); }
    }

    // Aller à un cran particulié
    this.goto = function(num) {
        if (num == 0) { self.prec.fadeOut(); }
        else { self.prec.fadeIn(); }
        if (num == self.etapes) { self.suiv.fadeOut(); }
        else { self.suiv.fadeIn(); }
        self.div.find(".slider").animate({
            "left": -self.saut * num
        }, 1000);
        self.numCourant = num;

    }

    this.prec.bind('click', this.prev);
    this.suiv.bind('click', this.next);

}
