var Slider = function (_container, _options) {

    var self = this;
    var index = 0;


    this.options = {
        labels : [],
        values : [],
        change : function (ui, userValue) {  },
        jquery : {      // Options de configuration jquery
            animate : false,    // Anime le déplacement du curseu
            min : 0,            // Valeur minimal du curseur
            max : 7,          // Valeur maximal du curseur
            range : false,      // Disposition de la barre de remplissage
            step : 1,           // Taille des intervalles
            value : 0,          // Valeur pour un curseur
            values : []         // Valeurs pour plusieurs curseurs
        }
    };

    this.slider = _container;
    this.sliderLabel = null;
    this.sliderBar = $(".slider-bar", this.slider);
    this.sliderBarBackground = $(".slider-bar-background", this.sliderBar);
    this.sliderBarHandle = $(".ui-slider-handle", this.sliderBar);
    this.sliderGraduationTop = $(".slider-graduation-top", this.slider);
    this.sliderGraduationMiddle = $(".slider-graduation-middle", this.sliderBar);
    this.sliderGraduationBottom = $(".slider-graduation-bottom", this.slider);

    this.onChange = function (event, ui, callback) {
        if (typeof(ui.values) == 'object') {
            var userValues = [];
            for(var i=0; i < ui.values.length; i++) {
                userValues[i] = this.getUserValue(ui.values[i]);
            }
        } else {
            var userValues = this.getUserValue(ui.value);
        }
        if (callback != null && typeof(callback) == "function") {
            callback(ui, userValues);
        }
    }

    this.getKey = function (needle, haystack) {
        var key = 0;
        for (key in haystack) {
            if (haystack[key] == needle) {
                return key;
            }
        }
        return null;
    }

    this.getJqueryValue = function (userValue) {
        var key = this.getKey(userValue, this.options.values)
        return key;
    }

    this.getUserValue = function (jqueryValue) {
        if (jqueryValue >= 0) return this.options.values[jqueryValue];
        else return null;
    }

    ///////////////////////////////////
    // Initialisation
    //////////////////////////////////
    this.options = $.extend(true, {}, this.options, _options);
    this.length = this.options.values.length - 1;
    this.options.jquery.min = 0;
    this.options.jquery.step = 1;
    this.options.jquery.max = this.length;


    if (typeof(this.options.jquery.values) == 'object' && this.options.jquery.values.length > 0) {
        // Controlle d'erreur
        for(index in this.options.jquery.values) {
            if (this.getKey(this.options.jquery.values[index], this.options.values) == null) {
                throw new Error("Les valeurs jquery ne correspondent pas aux valeurs utilisateur");
            }
        }

        this.options.jquery.value = this.options.jquery.values[0];
    } else if (typeof(this.options.jquery.value) == 'number') {
        // Controlle d'erreur
        if (this.getKey(this.options.jquery.value, this.options.values) == null) {
            throw new Error("La valeur jquery ne correspond pas a une des valeurs utilisateur");
        }
        this.options.jquery.value = this.getJqueryValue(this.options.jquery.value);
    }

    this.options.jquery.change = function (event, ui) {
        return self.onChange(event, ui, self.options.change);
    }

    // Converti les valeurs utilisateurs en valeur jquery
    var userValues = this.options.jquery.values;
    this.options.jquery.values = new Array();
    for (index in userValues) {
        this.options.jquery.values.push(this.getJqueryValue(userValues[index]));
    }


    var border = 2;
    if ($.browser.msie) border = 2;

    this.sliderLabel = $('<ul class="slider-label"></ul>').appendTo(this.slider);
    for(index = 0; index <= this.length; index++) {
        var li = $('<li>' + (this.options.labels[index] != null ? this.options.labels[index] : '') + '</li>').appendTo(this.sliderLabel);
        if (index == 0) li.addClass('first');
        else if (index == this.length) li.addClass('last');
    }


    this.sliderGraduationTop = $('<ul class="slider-graduation-top graduation"></ul>').appendTo(this.slider);
    for(index = 0; index < this.length; index++) {
        li = $('<li></li>').appendTo(this.sliderGraduationTop);
        if (index == 0) li.addClass('first');
        else if (index == this.length - 1) li.addClass('last');
    }


    this.sliderBar = $('<div class="slider-bar"></div>').appendTo(this.slider);

    this.sliderGraduationMiddle = $('<ul class="slider-graduation-middle graduation"></ul>').appendTo(this.sliderBar);
    for(index = 0; index < this.length; index++) {
        li = $('<li></li>').appendTo(this.sliderGraduationMiddle);
        if (index == 0) li.addClass('first');
        else if (index == this.length - 1) li.addClass('last');
    }



    this.sliderBar.slider(this.options.jquery);

    if(typeof(this.options.jquery.range) == "boolean" && this.options.jquery.range == true) {
        if (this.options.jquery.values.length > 1) {
            li = $('.ui-slider-handle', this.sliderBar);
            li.first().addClass('ui-slider-handle-first')
              .nextAll().addClass('ui-slider-handle-between');
            li.last().removeClass('ui-slider-handle-between').addClass('ui-slider-handle-last');
        }
    }


    this.sliderGraduationBottom = $('<ul class="slider-graduation-bottom graduation"></ul>').appendTo(this.slider);
    for(index = 0; index < this.length - 1; index++) {
        li = $('<li></li>').appendTo(this.sliderGraduationBottom);
        if (index == 0) li.addClass('first');
        else if (index == this.length - 2) li.addClass('last');
    }


    var liSliderLabel = $('li', this.sliderLabel);
    liSliderLabel.width(Math.round((this.sliderBar.width() / (liSliderLabel.length - 1)) - border));

    var liSliderGraduationTop = $('li', this.sliderGraduationTop);
    liSliderGraduationTop.width((this.sliderBar.width() / liSliderGraduationTop.length) - border);

    var liSliderGraduationMiddle = $('li', this.sliderGraduationMiddle);
    liSliderGraduationMiddle.width((this.sliderBar.width() / liSliderGraduationMiddle.length) - border);

    var liSliderGraduationBottom = $('li', this.sliderGraduationBottom);
    liSliderGraduationBottom.width((this.sliderBar.width() / (liSliderGraduationBottom.length + 1)) - border);
    this.sliderGraduationBottom.css({
        'margin-left' : (liSliderGraduationBottom.width() + border) / 2
    });
};

