DNow.LoginDialog = Class.create( aw.ui.Dialog, {

    initialize: function ( properties, events ) {

        Object.extend( properties, {
            'title': 'Login',
            'centerable': true
        });

        this.inheritCSS = true;

        aw.ui.Dialog.prototype.initialize.apply( this, arguments );

        if (this.componentBus) {
            this._onLoginFailure_fx = this._onLoginFailure.bind( this );
            this.componentBus.registerEvent( 'onloginfailure', this._onLoginFailure_fx );

            this._onLoginSuccess_fx = this._onLoginSuccess.bind( this );
            this.componentBus.registerEvent( 'onloginsuccess', this._onLoginSuccess_fx );
        }
    },

    draw: function ( ) {
        aw.ui.Dialog.prototype.draw.apply( this );

        var contents = this.canvas.down('div.contents');
        if (!contents)
            return null;

        contents.insert( this._local_templates['ui'] );

        var username = this.readCookie( 'username' );

        this._userTextbox = new aw.ui.Control.TextBox({
            'label': 'User Name:',
            'value': this.readCookie( 'username' ),
            'container': contents.down('div.inputEmail'),
            'hasFocus': ((username) && (username !== '')) ? false : true,
            'cssClass': 'loginField'
        },{
            'enter': this._onLoginClick.bind( this )
        });

        this._passTextbox = new aw.ui.Control.TextBox({
            'label': 'Password:',
            'type': 'password',
            'container': contents.down('div.inputPassword'),
            'hasFocus': ((username) && (username !== '')) ? true : false,
            'cssClass': 'loginField'
        },{
            'enter': this._onLoginClick.bind( this )
        });

        this._rememberEmailCheck = new aw.ui.Control.Checkbox({
            'label': 'Remember User Name',
            'name': 'rememberUser',
            'value': true
        });

        /*new aw.ui.Control.Label({
            'container': contents.down('div.passwordLabel'),
            'label': '&#160;',
            'value': '(Password field is case sensitive)'
        });*/

        aw.EventManager.register( contents.down('span.forgotPassword'), 'click', this._onForgotPasswordClick.bind( this ) );
        //aw.EventManager.register( contents.down('span.signup'), 'click', this._onSignupClick.bind( this ) );

        this._renderButton( 'Login', this._onLoginClick.bind( this ) );
        this._renderButton( 'Cancel', this.close.bind( this ) );

        this.centerDialog();

        return this.canvas;
    },

    erase: function ( ) {
        if (this.componentBus) {
            if (this._onLoginFailure_fx) {
                this.componentBus.unregisterEvent( 'onloginfailure', this._onLoginFailure_fx );
                this._onLoginFailure_fx = null;
            }

            if (this._onLoginSuccess_fx) {
                this.componentBus.unregisterEvent( 'onloginsuccess', this._onLoginSuccess_fx );
                this._onLoginSuccess_fx = null;
            }
        }

        aw.ui.Dialog.prototype.erase.apply( this );
    },

    _destroyLoadingDialog: function ( ) {
        if (this._loadingDialog) {
            this._loadingDialog.close( );
            this._loadingDialog.destroy( );
            delete this._loadingDialog;
        }
    },

    _onForgotPasswordClick: function ( ) {
        new aw.ui.Dialog.ForgotPassword({ 'componentBus': this.componentBus, 'defaultEmail': this._userTextbox.getValue( ) });
    },

    _onLoginClick: function ( ) {
        if (!this.componentBus)
            return;

        this.componentBus.loadUser( this._userTextbox.getValue( ), this._passTextbox.getValue( ) );

        this._destroyLoadingDialog( );
        this._loadingDialog = new aw.ui.Dialog.Loading({'message':'Logging in...','centerable':true});
    },

    _onLoginFailure: function ( message ) {
        this._userTextbox.blur( );
        this._passTextbox.blur( );
        this._destroyLoadingDialog( );
        new aw.ui.Dialog.Error({'title':'Login Error','content':message});
    },

    _onLoginSuccess: function ( ) {
        this._destroyLoadingDialog( );
        this.destroy( );
        this.close( );
    },

    _onSignupClick: function ( ) {
        var dnow = this.componentBus.getComponentReferences( 'DNow' );
        if ((dnow) && (dnow.length > 0)) {
            dnow[0]._onSignup( );
            this.close( );
        }
    },

    _local_templates: {
        'ui': '<div class="login">' +
                '<h3>Please enter your email and password below to login.</h3>' +
                '<div class="inputEmail"></div>' +
                '<div class="inputPassword"></div>' +
                '<div class="rememberUser"></div>' +
                '<div class="links">' +
                    //'<span class="forgotPassword">Forgot Password?</span> &#160; | &#160; <span class="signup">Need an account?</span>' +
                    '<span class="forgotPassword">Forgot Password?</span>' +
                '</div>' +
            '</div>'
    },

    CLASS_NAME: 'DNow.LoginDialog'
});
