
(function() {
    
    var Y = Nb.Lib;
    
    var EVT_SUCCESS = 'success';

    Nb.FeUser.Gui.LoginBoxGuestForm = function()
    {
        Nb.FeUser.Gui.LoginBoxGuestForm.superclass.constructor.apply(this, arguments);
        
        var passwordLostButton = Y.one('#login-box-guest-password-lost-button');
        
        passwordLostButton.on('click', this._onPasswordLostClick, this);
        
        this.on('ajaxSubmitComplete', this._onAjaxSubmitComplete, this)
        
        this.passwordLostLayer = null;
        this.activationEmailLayer = null;
    }
    
    Nb.FeUser.Gui.LoginBoxGuestForm.NAME = 'loginBoxGuestForm';
    
    Nb.FeUser.Gui.LoginBoxGuestForm.ATTRS = {
        promotion : {
            value : ''
        }
    }
    
    // activation-email-button
    
    Y.extend(Nb.FeUser.Gui.LoginBoxGuestForm, Nb.Core.Form, {
        _onPasswordLostClick : function(event)
        {
            event.halt();
            
            Y.io('/index.php?ajax=1&ext=fe_user_gui&controller=PasswordLost&method=renderXhr&promotion=' + this.get('promotion'), {
                method : 'post',
                on : {
                    complete: this._onPasswordLostGetContentComplete
                },
                context: this
            });
        },
        _onPasswordLostGetContentComplete : function(transactionId, response)
        {
            var response = Y.JSON.parse(response.responseText);
            
            if (!this.passwordLostLayer)
            {
                this.passwordLostLayer = new Nb.Wr.UI.Layer();
            }

            this.passwordLostLayer.set('header', response.data.header);
            this.passwordLostLayer.set('content', response.data.content);
            
            if (response.data.colorSchema)
            {
                this.passwordLostLayer.set('colorSchema', response.data.colorSchema);
            }
            
            if (response.data.width)
            {
                this.passwordLostLayer.set('width', response.data.width);
            }
            
            this.passwordLostLayer.show();
            
            for (var i = 0; i < response.data.components.length; i++)
            {
                var componentConfig = Y.JSON.parse(response.data.components[0]);
                
                componentConfig.config.layer = this.passwordLostLayer;

                var type = Nb.Core.Util.getType(componentConfig.type);
                var component = new type(componentConfig.config);
                
                Nb.Wr.Controller.addComponent(componentConfig.name, component);
            }
            
            
        },
        _onAjaxSubmitComplete : function()
        {
            // Config
            var config = {
                refresh : true
            }
            
            // Stop refresh i.e. e.details[0].refresh = false;
            this.fire(EVT_SUCCESS, config);
            
            if (config.refresh)
            {
                location.href = location.href;
            }
        },
        _onActivationEmailClick : function(event)
        {
            event.halt();
            
            var controls = this.get('controls');
            var formData = {};
            for(var i = 0; i < controls.length; i++)
            {
                var name = controls[i].get('name');
                var value = controls[i].get('value');
                formData[name] = value;
            }

            Y.io('/index.php?ajax=1&ext=fe_user_gui&controller=AccountActivation&method=sendXhr&promotion=' + this.get('promotion'), {
                method : 'post',
                on : {
                    complete: this._onAccountActivationSendComplete
                },
                context: this,
                data: Nb.Core.Util.getQueryString(formData)
            });
            
            
        },
        _onAccountActivationSendComplete : function(transactionId, response)
        {
            var response = Y.JSON.parse(response.responseText);

            if (!this.activationEmailLayer)
            {
                this.activationEmailLayer = new Nb.Wr.UI.Layer();
            }
            
            this.activationEmailLayer.set('header', response.data.header);
            this.activationEmailLayer.set('content', response.data.content);
            
            if (response.data.colorSchema)
            {
                this.activationEmailLayer.set('colorSchema', response.data.colorSchema);
            }
            
            if (response.data.width)
            {
                this.activationEmailLayer.set('colorSchema', response.data.width);
            }
            
            this.activationEmailLayer.show();
        
        },
        showTooltip : function()
        {
            Nb.FeUser.Gui.LoginBoxGuestForm.superclass.showTooltip.apply(this, arguments);
            
            var ActivationEmail = Y.one('#activation-email-button');
            
            if (ActivationEmail)
            {
                ActivationEmail.on('click', this._onActivationEmailClick, this);
            }
        }
        
    });
})();

