/// <reference path="../../node_modules/@types/jquery/index.d.ts"/>
var RegionFilter;
(function (RegionFilter_1) {
    var RegionFilter = (function () {
        function RegionFilter($selector, config) {
            this.selected = {};
            this.strings = ['район', 'района', 'районов'];
            this.typeName = 'районы';
            this.$selector = $selector;
            var strings = $selector.data('strings');
            var typeName = $selector.data('type-name');
            if (strings !== undefined) {
                this.strings = eval(strings);
            }
            if (typeName !== undefined) {
                this.typeName = typeName;
            }
            this.config = config;
            this.$selector.click(this.onSelectorClick.bind(this));
            this.loadSelected();
        }
        RegionFilter.prototype.onSelectorClick = function (event) {
            event.preventDefault();
            var context = this;
            var itemsContainer = this.$selector.parent().find('.' + this.config.itemsContainerClass);
            if (itemsContainer.length) {
                itemsContainer.fadeOut(200, function () {
                    $(this).remove();
                });
            }
            else {
                this.$selector.parent().append(this.createContainer());
            }
        };
        RegionFilter.prototype.createContainer = function () {
            var itemsContainer = $("<div class=\"" + this.config.itemsContainerClass + "\"></div>");
            var context = this;
            var field = $(this.$selector.data('field'));
            field.find('option').each(function () {
                var id = $(this).attr('value');
                itemsContainer.append(
                // create container
                $("<div class=\"" + context.config.itemContainerClass + "\"></div>")
                    .append($("<input type=\"checkbox\" class=\"" + context.config.itemCheckboxClass + "\" id=\"rfchb" + id + "\" value=\"" + id + "\">")
                    .attr('checked', $(this).prop('selected'))
                    .on('change', function () {
                    field.find("option[value=" + id + "]").prop('selected', $(this).prop('checked'));
                    context.setCount(field.find('option:selected').length);
                }))
                    .append($("<label class=\"" + context.config.itemLabelClass + "\" for=\"rfchb" + id + "\">" + $(this).html() + "</label>")));
            });
            return itemsContainer;
        };
        RegionFilter.prototype.loadSelected = function () {
        };
        RegionFilter.declOfNum = function (number, titles) {
            var cases = [2, 0, 1, 1, 1, 2];
            return titles[(number % 100 > 4 && number % 100 < 20) ? 2 : cases[(number % 10 < 5) ? number % 10 : 5]];
        };
        RegionFilter.prototype.checkChecked = function (id) {
            return this.selected.hasOwnProperty(id);
        };
        RegionFilter.prototype.setCount = function (count) {
            if (count === void 0) { count = 0; }
            this.$selector.html('Все ' + this.typeName);
            if (count > 0) {
                this.$selector.html(count + ' ' + RegionFilter.declOfNum(count, this.strings));
            }
        };
        return RegionFilter;
    }());
    RegionFilter_1.RegionFilter = RegionFilter;
})(RegionFilter || (RegionFilter = {}));
