(function() {
    var logModule = (function() {
        function log() {
            console.log.apply(console, arguments);
        }

        return {
            log: log
        }
    })();

    var storageModule = (function(log) {
        var direction,
            value,
            elements,
            limits = {min: null, max: null},
            subscribedFunctions = [],
            exchanges,
            statistic,
            storage = {};

        function init() {
            if (typeof window.exchange !== 'array') {
                log.log('Exchanges storage is empdy or don\'t exists');
            }

            exchanges = window.exchange.exchanges;
            statistic = window.exchange.statistic;
        }

        function setDirection(newDirection) {
            direction = newDirection;

            return storage;
        }

        function setValue(newValue) {
            value = newValue;

            return storage;
        }

        function getElements() {
            return elements;
        }

        function getLimits() {
            return limits;
        }

        function reloadElements() {
            var minLimit, maxLimit;

            elements = [];

            for (var i in exchanges) {
                if (
                    (exchanges[i].direction == direction)
//                    && (storage[i].value >= limits.min)
//                    && (storage[i].value <= limits.max)
                ) {
//                    if (!minLimit || minLimit > exchanges[i].value) {
//                        minLimit = exchanges[i].value;
//                    }
//
//                    if (!maxLimit || maxLimit < exchanges[i].value) {
//                        maxLimit = exchanges[i].value;
//                    }

                    elements.push(exchanges[i]);
                }
            }

            limits.min = statistic[direction].min;
            limits.max = statistic[direction].max;
            limits.optimal = statistic[direction].optimal;

            return storage;
        }

        function subscribeOnReload(fn) {
            subscribedFunctions.push(fn);
        }

        function updateEvent() {
            log.log('updating positions...');
            reloadElements();

            for (var i in subscribedFunctions) {
                subscribedFunctions[i](storage);
            }
        }

        init();

        storage.setDirection = setDirection;
        storage.setValue = setValue;
        storage.getElements = getElements;
        storage.getLimits = getLimits;
        storage.onReload = subscribeOnReload;
        storage.update = updateEvent;

        return storage;
    })(logModule);

    var mapModule = (function(storage) {
        var myMap, cluster;

        function initMapOnReady() {
            myMap = createMap();

            cluster = new ymaps.Clusterer();
            myMap.geoObjects.add(cluster);

            storage.onReload(reloadCluster);

            reloadCluster(storage);
        }

        function createMap() {
            var map = new ymaps.Map('map', {
                center: [53.90, 27.53],
                zoom: 11
            });

            map.behaviors.enable('scrollZoom');// Включим масштабирование колесом мыши
            map.controls.add(
                new ymaps.control.ZoomControl()// Создание экземпляра элемента управления
            );

            return map;
        }

        function reloadCluster(storage)
        {
            var elements = storage.getElements(),
                objects = [];

            console.log('reload with', elements);
            cluster.removeAll();

            for (var i in elements) {
                var object = createObject(elements[i]);
                objects.push(object);
            }

            cluster.add(objects);
        }

        function createObject(element) {
            var object = new ymaps.GeoObject({
                geometry: {
                    type: "Point",// тип геометрии - точка
                    coordinates: [element.office.latitude, element.office.longitude] // координаты точки
                },
                properties: {
                    balloonContent: 'qwert yui',
                    balloonContentHeader: element.office.bank.title,
                    balloonContentBody: element.office.address,
                    balloonContentFooter: element.office.title,
                    iconContent: element.value,
                    hintContent: element.office.bank.title,
                    clusterCaption: element.office.bank.title
                }
            },{
                preset: "twirl#blueStretchyIcon"
            });

            return object;
        }

        ymaps.ready(initMapOnReady);
    })(storageModule);

    var filterModule = (function(storage) {
        var selector = {
                switch: '.js-filter-switch',
                radio: '.js-filter-radio',
                slider: '.js-filter-slider'
            };

        function changeDirection() {
            var switchVal = $(selector.switch).val(),
                radioVal = $(selector.radio + ' input:radio:checked').val(),
                direction = radioVal + '_' + switchVal;

            storage.setDirection(direction).update();

        }

        function updateLimits(storage) {
            $(selector.slider).attr('min', storage.getLimits().min);
            $(selector.slider).attr('max', storage.getLimits().max);
            $(selector.slider).slider('refresh');

            console.log('update limits', storage.getLimits());
        }

        function init() {
            $(selector.switch).on('change', changeDirection);

            $(selector.radio).on('change', changeDirection);

            $(selector.slider).on('slidestop', function() {
                console.log('slider', $(selector.slider).val());
            });

            storage.onReload(updateLimits);

            changeDirection();
        }

        $(document).on('pagecreate', '#page-map', init);
    })(storageModule);
})();
@@ -3597,18 +3597,8 @@
 ith'
-, elements
 );%0A 
@@ -5266,32 +5266,169 @@
 ();%0A%0A        %7D%0A%0A
+        function changeValue() %7B%0A            var value = $(selector.slider).val();%0A%0A            console.log('slider', value);%0A        %7D%0A%0A
         function
@@ -5625,69 +5625,197 @@
 er).
-slider('refresh');%0A%0A            console.log('update limits', 
+val(storage.getLimits().optimal);%0A            $(selector.slider).slider('refresh');%0A%0A            console.log('UPDATE limits: '+storage.getLimits().min+'-%3E'+storage.getLimits().optimal+'%3C-'+
 stor
@@ -5829,16 +5829,20 @@
 Limits()
+.max
 );%0A     
@@ -6049,99 +6049,19 @@
 p', 
-function() %7B%0A                console.log('slider', $(selector.slider).val());%0A            %7D
+changeValue
 );%0A%0A

set optimal slider value on update limits