/**
 * @author Dmitry Monin <d.monin@netzbewegung.com>
 * @created 11.02.2010
 */

(function() {

    var Y = Nb.Lib;

    Nb.Wr.Form = function(config) {
        Nb.Wr.Form.superclass.constructor.apply(this, arguments);

        this._statusEl = this.form.get('parentNode').get('parentNode').one('.form-status');
        this._hideStatusTimer = null;

        this.on('ajaxValidate', this._onAjaxSend);
        this.on('ajaxSubmit', this._onAjaxSend);

        this.on('ajaxValidateComplete', this._onAjaxComplete);
        this.on('ajaxSubmitComplete', this._onAjaxComplete);

        this.on('ajaxSubmitFail', this._onAjaxFail);


        this._layer = null;
    };

    Nb.Wr.Form.NAME = "intranetForm";
    Nb.Wr.Form.ATTRS = {
        text : {
            value : {}
        }
    };

    Y.extend(Nb.Wr.Form, Nb.Core.Form, {
        _onAjaxSend : function(e) {
            if(this._hideStatusTimer)
            {
                this._hideStatusTimer.cancel();
            }

            this.setStatusText(this.get('text.saving'), true);
        },
        _onAjaxComplete : function(e) {            
            var responseData = e.response.data;
            if(responseData && responseData.statusHtml)
            {
                this.setStatusText(responseData.statusHtml);
            }
            else
            {
                this.setStatusText(null);
            }

            if(responseData && responseData.uid)
            {
                var form = Y.Node.getDOMNode(this.form);
                form.uid.value = responseData.uid;
            }

            if(responseData && responseData.layerText && responseData.redirectUrl)
            {
                this._redirectUrl = responseData.redirectUrl;

                var layer = this._getLayer();
                layer.set('header', this.get('text.saved'));
                layer.set('content', '<div class="layer-padding">' + responseData.layerText + '</div>');
                layer.show({
                    from : this._statusEl
                });

                this._nextBtn.set('href', this._redirectUrl);
            }
        },
        _onAjaxFail : function(e) {
            this.setStatusText(this.get('text.saveFailed'));
        },
        _onBackBtnClick : function(e) {
            this._getLayer().hide();
        },
        _onNextClick : function(e) {
            this._getLayer().hide();
            
            this.setStatusText(this.get('text.forwarding'), true);
            document.location = this._redirectUrl;
        },
        _getLayer : function() {
            if(!this._layer)
            {
                this._layer = new Nb.Core.UI.Layer();
                var buttons = [];

                // Back button
                this._backToFormBtn = new Nb.Core.UI.Layer.LayerButton({
                    name : 'backToForm',
                    text : this.get('text.backToForm')
                });
                this._backToFormBtn.on('click', this._onBackBtnClick, this);
                buttons.push(this._backToFormBtn);

                // Redirect button
                this._nextBtn = new Nb.Core.UI.Layer.LayerButton({
                    name : 'next',
                    text : this.get('text.next'),
                    isPrimary : true
                });
                this._nextBtn.on('click', this._onNextClick, this);
                buttons.push(this._nextBtn);

                this._layer.set('buttons', buttons);
            }

            return this._layer;
        },
        setStatusText : function(text, isLoading)
        {
            if(text)
            {
                this._statusEl.setStyle('display', 'block');
                this._statusEl.set('innerHTML', text);
            }
            else
            {
                this._statusEl.setStyle('display', 'none');
            }

            if(isLoading)
            {
                this._statusEl.addClass('form-status-loading');
            }
            else
            {
                this._statusEl.removeClass('form-status-loading');
            }
        }
    });
        

})();
