/**
 * Класс для работы с формами "В корзину" в меню
 * @namespace DesignCafe.Basket
 */

DesignCafe.Basket.CForms = function() {


    /** @type {Array} Массив форм на странице */
    var forms = new Array();

    var _chInput;

    var dTimeout, dInterval;

    /**
     * Найти форму по Id
     * @param {Number} id Идентификатор
     */
    function getFormById(id) {
        for (var i = 0; i < forms.length; i++)
            if (forms[i].objectId == id)
                return forms[i];
        return null;
    }

    /**
     * Хэндлер отправки формы
     */
    function _submitHandler() {
        DesginCafe.Basket.addToBasket();
        return false;
    }

    return {
        /**
         * Инициализировать формы
         * @param {Object} items хэш элементво (id : count)
         */
        init : function(items) {
            $('#center .product form')
                .each(function() {
                    var cForm = new DesignCafe.Basket.CForm(this);
                    if ((cForm.objectId) && (items[ cForm.objectId ]))
                        cForm.updateState(items[ cForm.objectId ]);
                    forms.push(cForm);
                })
                .find('input:text')
                    .inputArrowCounter();
        },

        /**
         * Обновить форму по id
         * @param {Number} id
         * @param {Number} count
         */
        updateFormById : function(id, count) {
            var form = getFormById(id);
            if (form)
                form.updateState(count);
        },

        getFormById : getFormById
    }
};

/**
 * Форма в меню
 * @param {Object} formEl
 */
DesignCafe.Basket.CForm = function(formEl) {
    var self = this;

    /** @type {Object} DOM-элемент формы */
    this.form = formEl;

    /** @type {Number} идентифиткатор объекта */
    this.objectId = 0;

    /** @type {Object} DOM-элемент инпут количества */
    this.countInput = null;

    for (var i = 0; i < this.form.elements.length; i++) {
        if (this.form.elements[i].name == 'id') {
            this.objectId = this.form.elements[i].value;
        } else if (this.form.elements[i].name == 'count') {
            this.countInput = this.form.elements[i];
        }
    }
    this.form.onsubmit = function() {
        self.submit();
        return false;
    }
};

DesignCafe.Basket.CForm.prototype = {
    updateState : function(count) {
        if (count > 0)
            this.message('Вы заказали: ' + count);
        else
            this.message('');
        this.countInput.value = 1;
        this.enable();
    },
    setLoadingState : function() {
        this.message('Подождите ...');
    },
    submit : function() {
        if (this.disabled)
            return;
        this.setLoadingState();
        this.disable();
        DesignCafe.Basket.add(this.objectId, parseInt(this.countInput.value) || 0);
    },
    disable : function() {
        this.disabled = true;
    },
    enable : function() {
        this.disabled = false;
    },
    message : function(text) {
        if (!this._$message)
            this._$message = $(this.form.parentNode).siblings('.message');
        this._$message.html(text).show();
    }
};

