/**
 * Выбор типа мусора (SELECT)
 * "household" - бытовой
 * "debris"    - строительный
 * "bulky"     - крупногабаритный
 * "snow"      - снег
 *
 * @property jQuery node
 *           элемент SELECT
 * @property string current
 *           текущий тип ("household" или др.)
 * @property function[] listeners
 *           слушатели изменения типа мусора (function(string current))
 */
CalcClass.GarbageTypeClass = go.Class({

    /**
     * Конструктор
     *
     * @param jQuery node
     *        ссылка на элемент SELECT
     * @param bool reset
     *        сбрасывать ли SELECT в начальное состояние
     */
    '__construct': (function(node, reset) {
        this.node      = $(node);
        if (reset) {
            this.reset();
        }        
        this.current   = this.node.val();
        this.listeners = [];
        this.node.bind("change", this.onChange);
    }),
    
    /**
     * Повесить обработчик изменения типа
     *
     * @param function listener
     */
    'addEventListener': (function(listener) {
        this.listeners.push(listener);
    }),
    
    /**
     * Узнать текущий тип
     *
     * @return string
     */
    'getCurrentType': (function() {
        return this.current;
    }),
    
    /**
     * Установить новый тип
     *
     * @param string type
     */
    'setType': (function(type) {
        if (type != this.current) {
            this.current = type;
            this.runHandlers();
        }
    }),
    
    /**
     * Запустить все слушалки
     */
    'runHandlers': (function() {
        var listeners = this.listeners;
        var len       = listeners.length;
        var current   = this.getCurrentType();
        for (var i = 0; i < len; i++) {
            var listener = listeners[i];
            listener(current);
        }
    }),
    
    /**
     * Сбросить SELECT в начальное состояние
     */
    'reset': (function() {
        this.node.val("household");
    }),
    
    /**
     * Обработчик изменения в SELECT'е
     */
    'onChange_bind': (function() {
        this.setType(this.node.val());
    }),

    'eoc': null
});

