/**
 * Форма заказа
 *
 * @property CalcClass calc
 * @property jQuery node
 * @property jQuery[] inputs
 * @property jQuery nodeButton
 * @property jQuery nodeError 
 * @property bool visible
 */
CalcClass.FromClass = go.Class({

    '__construct': (function(calc, node) {
        this.calc    = calc;
        this.node    = $(node);
        this.inputs  = this.node.find("input");        
        this.nodeButton = this.node.find(".button_in");
        this.nodeError  = this.node.find(".form-error");
        this.visible = false;
        $("body").click(this.onClickBody);
        this.node.click(this.onClickNode);
        this.inputs.keypress(this.onKeyPressInput);
        this.node.find("textarea").keypress(this.onKeyPressTA);
        this.nodeButton.click(this.onClickButton);
    }),
    
    'show': (function() {
        if (!this.visible) {
            this.visible = true;
            this.node.addClass("active");
            this.inputs.eq(0).focus();
        }
    }),
    
    'hide': (function() {
        if (this.visible) {
            this.visible = false;
            this.node.removeClass("active");
        }
    }),
    
    'onClickBody_bind': (function() {
        this.hide();
    }),
    
    'onClickNode_bind': (function(e) {
        return false;
    }),
    
    'onKeyPressInput_bind': (function(e) {
        if (e.keyCode == 27) {
            this.hide();
        } else if (e.keyCode == 13) {
            this.submit();
        }
    }),
    
    'onKeyPressTA_bind': (function(e) {
        if (e.keyCode == 27) {
            this.hide();
        } 
    }),    
    
    'onClickButton_bind': (function(e) {
        this.submit();
    }),
    
    'submit': (function() {
        var phone = this.inputs.eq(1).val().replace(/^\s+/, "").replace(/\s+$/);
        var email = this.inputs.eq(2).val().replace(/^\s+/, "").replace(/\s+$/);
        if (!(phone || email)) {
            this.nodeError.show();
            return;
        }
        $("#form-order-data").val($.toJSON(this.getData()));
        this.node.find("form").submit();    
    }),
    
    'getData': (function() {
        return {
            'garbage'    : this.calc.garbageType.getCurrentType(),
            'containers' : this.calc.containers.getCurrentContainer(),
            'volume'     : Math.round(this.calc.slider.volume),
            'map'        : this.calc.map.current.name,
            'loading'    : this.calc.withLoading.isChecked()
        };
    }),

    'eoc': null
});

