/*
 *  Cobalt jQuery plugin for allowing HTML Select elements to function with ui.sliders plugin.
 *
 * Usage:
 *
 * This plugin accepts a number of agruments for configuring.
 *
 * sliderDiv - This is the ID of the empty div that should be the container for the slider when it is added to the DOM
 * sliderClass - Takes a string and adds this string to the class of the slider.
 * handleClass - Takes a string and adds this string to the class of the slider handles.
 *
 * noMax - A boolean value to set if the max slider has an empty option for "No Max". This adjusts the one to one ratio
 *      between the min and max selects indexes and adds 1 to the length. This makes the slider ticks work with "No Min"
 *      on the first position and only on the min select, and the last postion to be "No Max" on the max select.
 *
 * sliderOptions - Javascript object that accepts any of the parameters that jQuery UI sliders accepts. This will
 *      override the default slider values that are set in this plugin. See jQuery UI Slider documentation for further
 *      information. 
 *
 * Author: Benjamin Bostow
 * Version: 1.0
 * Requires: jQuery 1.3.2, jQuery UI 1.7.2 sliders.
 */
(function(jQuery){
    jQuery.fn.cbltslider = function(settings) {
        var current = this;
        var selects = jQuery(this);
        var optionLen = selects.eq(0).find('option').length;
        var minValue = selects.eq(0).attr('selectedIndex');
        var maxValue = selects.eq(1).attr('selectedIndex');

        var normalizeValues = function(val, defaultMax) {
            val = Number(val);
            defaultMax = Number(defaultMax);
            return val < 0 ? 0 : val > defaultMax ? defaultMax : val;
        };

        selects.attr("slider", settings.sliderDiv);

        var sliderMax = optionLen - 1;

        if (settings.noMax) {
            jQuery(settings.sliderDiv).attr("noMax", settings.noMax);
            sliderMax = optionLen;
            maxValue++;
        }

        var slider_config = jQuery.extend({
            animate: true,
            range: true,
            min: 0,
            max: sliderMax,
            step: 1,
            slide: function() {
                var values = jQuery(this).slider('option', 'values');
                var optionLen = selects.eq(0).find('option').length;
                var optLenIndex = Number(optionLen) - 1;
                var minValue = values[0];
                var maxValue = values[1];

                minValue = normalizeValues(minValue, optLenIndex);
                selects.eq(0).attr('selectedIndex', minValue);
                if (settings.noMax) {
                    maxValue--;
                }
                maxValue = normalizeValues(maxValue, optLenIndex);
                selects.eq(1).attr('selectedIndex', maxValue)
            },
            values: [minValue,maxValue]
        }, settings.sliderOptions);

        if (!(minValue == 0 && maxValue == 0)) {
            jQuery(settings.sliderDiv).slider(slider_config);
        }
        jQuery(settings.sliderDiv).addClass(settings.spriteClass);
        jQuery(settings.sliderDiv).addClass(settings.sliderClass);
        jQuery(settings.sliderDiv).children("a.ui-slider-handle").addClass(settings.handleClass);

        selects.change(function() {
            current.cbltslider_updater();
        });

        return this;
    };

    /*
     * Method for updating the sliders when the select values change.
     */
    jQuery.fn.cbltslider_updater = function() {
        var selects = jQuery(this);
        var minIndex = selects.eq(0).attr('selectedIndex');
        var maxIndex = selects.eq(1).attr('selectedIndex');
        var sliderElement = selects.attr("slider");
        var sliderMax = selects.eq(0).find('option').length;
        var noMax = jQuery(sliderElement).attr("noMax");
        var offset = 0;

        if (noMax) {
            maxIndex--;
            offset = 1;
        }
        if (Number(sliderMax) != Number(jQuery(sliderElement).slider('option', 'max'))) {
            jQuery(sliderElement).slider('option', 'max', sliderMax)
        }
        jQuery(sliderElement).slider('option', 'values', [minIndex,maxIndex])
    };

})(Cobalt.Core.JQueryFactory.getLatest());





