/*!
 * AW RANGE SLIDER
 *
 *
 * @author  Roman Janko <janko@andweb.cz>
 * @version  1.0.0
 *
 */

 /*
    AW RANGE SLIDER - wrapper over Range Slider http://ionden.com/a/plugins/ion.rangeSlider/index.html
 */

;(function() {
    'use strict';

    function awRangeSlider(element, options) {
        this.self  = element;
        this.$self  = $(element);
        this.settings   = $.extend(true, {}, $.fn.awRangeSlider.defaults, options);
        
        this.instance = null;
        this.ourFormControls = {
            priceFrom: null,
            priceTo: null,
            select: null
        };
    }


    awRangeSlider.prototype = {
        
        /* PRIVATE METHODS */
        _create: function() { 
            
            var oldOnFinish = this.settings.onFinish;
            var that = this;

            this.settings.onFinish = function(data) {
                that.instance.trigger("finish");
                oldOnFinish.apply(this, [data]);
            };

            this.instance = this.$self.ionRangeSlider(this.settings);
            if (this.settings.sliderType == 'price') {
                this._bindOnChangePrice();
            } else {
                this._bindOnChangeDefault();
            }
        },

        _bindOnChangePrice: function() {
            var that = this;

            this.ourFormControls.priceFrom = this.$self.next(); 
            this.ourFormControls.priceTo = this.$self.next().next(); 

            this.instance.on("finish", function (e) {
                var $inp = $(this);

                var from = $inp.data("from"); 
                var to = $inp.data("to"); 

                that.ourFormControls.priceFrom.val(from).trigger('change');
                that.ourFormControls.priceTo.val(to).trigger('change');

            });
        },

        _bindOnChangeDefault: function() {
            var that = this;

            var select = this.ourFormControls.select = this.$self.next(); 
            var options = select.find('option');
            
            this._resetOptions(options);
            //this._setSelectedAllOptions(options);

            this.instance.on("finish", function (e) {
                var $inp = $(this);
                var dataValues = $inp.data('values').split(',');
                var from = $inp.data("from"); 
                var to = $inp.data("to"); 
                var values = $inp.prop("value").split(';'); // human readable values = text values
                var range = that._createRange(from, to);

                $inp.data('range', range);
                                
                that._resetOptions(options);

                // loop over selected items only when if we do not choose everything, it does not make a sense
                if (dataValues.length != range.length) {
                    range.forEach(function(i) {
                        that._setSelectedOption(options.eq(i));
                    });
                }
    
            }); 
        },

        _createRange: function(start, end) {
            var range = [];
            for (var i = start; i <= end; i++) {
                range.push(i);
            }
            return range;
        },

        _resetOptions: function(options) {
            options.prop('selected', false);
        },

        _setSelectedOption: function(option) {
            option.prop('selected', true);
        },

        _setSelectedAllOptions: function(options) {
            options.prop('selected', true);
        },

        /* PUBLIC METHODS */
        updateMinMax: function(min, max) {
            var update = {
                min: min,
                //from: min,
                max: max,
                //to: max,
            };
            
            if(this.$self.data('min') == this.$self.data('from') || update.min >= this.$self.data('from')) {
                update.from = update.min;
            }

            if(this.$self.data('max') == this.$self.data('to') || update.max <= this.$self.data('to')) {
                update.to = update.max;
            }

            this.$self.data('min', update.min);
            this.$self.data('max', update.max);

            var inst = this.$self.data("ionRangeSlider");
            
            inst.update(update);
        }
    };


    (function ($) {
        $.fn.awRangeSlider = function(options) {
            var
            args   = Array.prototype.slice.call(arguments, 1),
            method = awRangeSlider.prototype[options];

            
            return this.each(function() {
                var self     = $(this);
                var instance = self.data('awRangeSlider');

                if (!instance) {
                    instance = new awRangeSlider(this, options);
                    instance._create();
                    self.data('awRangeSlider', instance);
                }

                if (method && options.substring(0, 1) == '_') 
                    $.error('Cannot use private method "' + options + '" outside of object');
                
                if (method) {
                    method.apply(instance, args);
                } else if (options && typeof options !== 'object') {
                    $.error('Method "' + options + '" not found.');
                }
            });
        };

        $.fn.awRangeSlider.defaults = {
            sliderType: 'price' // price || default
        };
    })(jQuery);
})();