(function($){
    $.widget('ui.zoara_rangeslider', {
                    _init: function(){
                                    var sliderDiv = $(this.element);
                                    var logarithmic = this.options.logarithmic;
                                    var callback = this.options.callback;
                                    var slide_callback = this.options.slide_callback;
                                    var custom_wrapper = this.options.custom_wrapper;
                                    var margin_left = this.options.margin_left;
                                    var prefix = this.options.prefix;
                                    var min = logarithmic ? 0 : this.options.min;
                                    var max = logarithmic ? 1 : this.options.max;
                                    var step = logarithmic ? 0.01 : this.options.step;

                                    var inverse = $.proxy(this, "inverse");

                                    sliderDiv.wrap('<div class="'+custom_wrapper+'"></div>');
                                    var textDiv = $('<div class="sliderResults" />').insertAfter(sliderDiv.parent());
                                    var minBox = $('<input type="text" class="minAmount" />').appendTo(textDiv)
                                                    .val(prefix+this.options.values[0].toFixed(this.options.precision).toString())
                                                    .attr('name', this.options.min_box_name)
                                                    .change(function(e){
                                                                    var numeric_value = this.value.replace(/[^0-9\.]/, '');
                                                                    sliderDiv.slider('values', 0, inverse(numeric_value));
                                                                    this.value = prefix+numeric_value.toString();
                                                    });
                                    var maxBox = $('<input type="text" class="maxAmount" />').appendTo(textDiv)
                                                    .val(prefix+this.options.values[1].toFixed(this.options.precision).toString())
                                                    .attr('name', this.options.max_box_name)
                                                    .change(function(e){
                                                                    var numeric_value = this.value.replace(/[^0-9\.]/, '');
                                                                    sliderDiv.slider('values', 1, inverse(numeric_value));
                                                                    this.value = prefix+numeric_value.toString();
                                                    });

                                    if(this.options.values[0] == 0 && this.options.values[1] == 0){
                                                    this.options.values[1] = 1;
                                                    this.options.max = 1;
                                    }
                                    minBox.val(prefix+number_format(this.options.values[0], this.options.precision, '.', ','));
                                    maxBox.val(prefix+number_format(this.options.values[1], this.options.precision, '.', ','));
                                    var change = $.proxy(function(e, ui){
                                                    ui.value = this.convert(ui.value);
                                                    if (callback != null)
                                                    {
                                                        callback.call(sliderDiv, e, ui);
                                                    }
                                                    
                                    }, this);
                                    var slide = $.proxy(function(e, ui){
                                                    var index = $(ui.handle).prevAll().length;
                                                    ui.value = this.convert(ui.value);
                                                    ui.value = number_format(ui.value, this.options.precision, '.', ',');
                                                    if(index == 1){//Min handle
                                                                    minBox.val(prefix+ui.value.toString());
                                                    } else {//Max handle
                                                                    maxBox.val(prefix+ui.value.toString());
                                                    }

                                                    if(slide_callback){
                                                                    slide_callback.call(sliderDiv, e, ui);
                                                    }
                                    }, this);

                                    var values;
                                    if(this.options.logarithmic){
                                                    values = [inverse(this.options.values[0]), inverse(this.options.values[1])];
                                    } else {
                                                    values =  this.options.values;
                                    }
                                    sliderDiv.slider({
                                                    values: values,
                                                    min: min,
                                                    max: max,
                                                    step: step,
                                                    range: true,
                                                    change: change,
                                                    slide: slide
                                    });

                                    var width = sliderDiv.outerWidth() || sliderDiv.css('width');
                                    if(width){
                                                    textDiv.css('width', width);
                                    }
                    },
                    convert: function(value){
                                    if(!this.options.logarithmic){
                                                    return value.toFixed(this.options.precision);
                                    } else {
                                                    var max = this.options.max;
                                                    var min = this.options.min;
                                                    var range = (max - min);
                                                    var result = min + ((1/(this.options.log_base - 1))*(Math.pow(this.options.log_base, value) - 1))*range;

                                                    return result.toFixed(this.options.precision);
                                    }
                    },
                    inverse: function(value){
                                    if(!this.options.logarithmic){
                                                    return value;
                                    } else {
                                                    var max = this.options.max;
                                                    var min = this.options.min;
                                                    var range = (max - min);

                                                    var x = (value-min)/range;

                                                    return Math.log((this.options.log_base - 1)*(x+(1/(this.options.log_base - 1))))/Math.log(this.options.log_base);
                                    }
                    }
    });
    $.extend($.ui.zoara_rangeslider, {
                    defaults: {
                                    callback: null,
                                    slide_callback: false,
                                    margin_left: 0,
                                    logarithmic: false,
                                    precision: 0,
                                    log_base: 5000,
                                    custom_wrapper: 'slider-wrapper',
                                    values: [0,100],
                                    min: 0,
                                    max: 100,
                                    step: 1,
                                    prefix: '',
                                    min_box_name: 'min_box',
                                    max_box_name: 'max_box'
                    }
    });
})(jQuery);

