
(function() {
    
    var Y = Nb.Lib;

    Nb.FeUser.Gui.LoginBoxUser = function()
    {
        Nb.FeUser.Gui.LoginBoxUser.superclass.constructor.apply(this, arguments);

        var editProfileButton = Y.one('#login-box-user-profile');
        
        if (editProfileButton)
        {
            editProfileButton.on('click', this._onEditProfileButtonClick, this);
        }
        
        this.profileEditLayer = null;
    }
    
    Nb.FeUser.Gui.LoginBoxUser.NAME = 'loginBoxUser';
    
    Nb.FeUser.Gui.LoginBoxUser.ATTRS = {
        promotion : {
            value : ''
        }
    }
    
    Y.extend(Nb.FeUser.Gui.LoginBoxUser, Nb.Core.Component, {
        _onEditProfileButtonClick : function(event)
        {
            event.halt();
            
            Y.io('/index.php?ajax=1&ext=fe_user_gui&controller=ProfileEdit&method=renderXhr&promotion=' + this.get('promotion'), {
                method : 'post',
                on : {
                    complete: this._onGetEditProfileContentComplete
                },
                context: this
            });
        },
        _onGetEditProfileContentComplete : function(transactionId, response)
        {
            var response = Y.JSON.parse(response.responseText);

            if (!this.profileEditLayer)
            {
                this.profileEditLayer = new Nb.Wr.UI.Layer();
            }

            this.profileEditLayer.set('header', response.data.header);
            this.profileEditLayer.set('content', response.data.content);
            
            if (response.data.colorSchema)
            {
                this.profileEditLayer.set('colorSchema', response.data.colorSchema);
            }
            
            if (response.data.width)
            {
                this.profileEditLayer.set('width', response.data.width);
            }
            
            this.profileEditLayer.show();
            
            for (var i = 0; i < response.data.components.length; i++)
            {
                var componentConfig = Y.JSON.parse(response.data.components[0]);
                
                componentConfig.config.layer = this.profileEditLayer;

                var type = Nb.Core.Util.getType(componentConfig.type);
                var component = new type(componentConfig.config);
                
                Nb.Wr.Controller.addComponent(componentConfig.name, component);
            }
        }
    });
})();
