/**
 * Чекбокс "с погрузкой". Появляется при выборе не менее 3-х контейнеров снега.
 *
 * @property jQuery node
 *           нода чекбокса
 * @property bool enabled
 *           активен ли чекбокс
 * @property bool checked
 *           выбран ли чекбокс
 * @property bool isSnow
 *           в качестве мусора выбран снег
 * @property int count
 *           количество контейнеров
 * @property function[] listeners
 *           слушатели переключения чекбокса
 */
CalcClass.WithLoading = go.Class({

    /**
     * Конструктор
     *
     * @param jQuery node
     *        нода чекбокса
     */
    '__construct': (function(node) {
        this.node      = $(node);
        this.enabled   = false;
        this.checked   = false;
        this.isSnow    = false;
        this.count     = 0;
        this.listeners = [];
        this.node.attr("checked", false);
        this.node.click(this.onClick);
    }),
    
    /**
     * Активировать чекбокс
     */
    'enable': (function() {
        if (!this.enabled) {
            this.enabled = true;
            this.node.parent().show();
        }
    }),
    
    /**
     * Дизактивировать чекбокс
     */
    'disable': (function() {
        if (this.enabled) {
            this.enabled = false;
            this.node.parent().hide();
        }
    }),
    
    /**
     * Выбрана ли погрузка
     */
    'isChecked': (function() {
        return (this.enabled && this.checked);
    }),
    
    /**
     * Указать тип мусора
     *
     * @param string snow
     */
    'setGarbageType': (function(type) {
        this.isSnow = (type == "snow");
        this.checkEnabled();
    }),
    
    /**
     * Указать количество контейнеров
     *
     * @param int count
     */
    'setCountContainers': (function(count) {
        this.count = count;
        this.checkEnabled();
    }),
    
    /**
     * Проверка включённости/выключенности
     */
    'checkEnabled': (function() {
        if (this.isSnow && (this.count >= 3)) {
            this.enable();
        } else {
            this.disable();
        }
    }),
    
    /**
     * Повесить слушатель события выбора чекбокса
     */
    'addEventListener': (function(listener) {
        this.listeners.push(listener);
    }),
    
    /**
     * Вызов всех слушаетелей события
     */
    'runListeners': (function() {
        var listeners = this.listeners;
        var len       = listeners.length;
        var checked   = this.isChecked();
        for (var i = 0; i < len; i++) {
            listeners[i](checked);
        }
    }),

    /**
     * Обработка щелчка на чекбоксе
     */
    'onClick_bind': (function(e) {
        this.checked = this.node[0].checked;
        this.runListeners();
    }),

    'eoc': null
});

