(function($){
    /**
     * Plugin jQuery zarządzający listami wartości określających dolny i górny zakres
     *
     * @author Marek Kobyliński <marek.kobylinski@ffcreation.com>
     */
    $.fn.dependentSelect = function(dependent)
    {
        /**
         * Obiekt główny
         */
        var _me = $(this);

        /**
         * Pole zależne
         */
        var _dependent = dependent;

        /**
         * Kierunek
         */
        var _direction = true;

        /**
         * Zdarzenie zmiany wartości listy
         */
        _me.change(function(){
            var _val = _dependent.val();
            _dependent.find(':gt(0)').remove();
            if(_me.val() == ""){
                _dependent.append(_me.find(':gt(0)').clone());
                _dependent.children().each(function(){
                    if(_val == $(this).attr('value')){
                        $(this).attr('selected', 'selected');
                    }
                })
                return;
            }
            var _element;
            _me.find(':gt(0)').each(function(){
                if(_direction
                    ? (parseInt($(this).attr('value')) >= parseInt(_me.val()))
                    : (parseInt($(this).attr('value')) <= parseInt(_me.val()))
                ){
                    _element = $(this).clone();
                    if(_val == $(this).attr('value')){
                        _element.attr('selected', 'selected');
                    }
                    _dependent.append(_element);
                }
            })
        });

        /**
         * Publiczny interfejs obiektu
         */
        var _this = {
            reverseDirection:function(){
                _direction = false;
                return _this;
            }
        }
        return _this;
    }
})(jQuery);