// -*- coding: utf-8 -*-
// :Progetto:  SoL -- Logon panel
// :Creato:    mar 14 ott 2008 11:48:39 CEST
// :Autore:    Lele Gaifax <lele@nautilus.homeip.net>
// :Licenza:   GNU General Public License version 3 or later
//

Ext.SSL_SECURE_URL="js/desktop/images/default/s.gif";
Ext.BLANK_IMAGE_URL="js/desktop/images/default/s.gif";

Login = function() {
    var win, form, submitUrl = 'login/login';

    function onSubmit() {
        this.showMask();
        form.submit({
            reset: true
        });
    }

    return {
        Init:function() {
            var path = window.location.pathname,
                path = path.substring(0, path.lastIndexOf('/') + 1);

            Ext.QuickTips.init();

            var logoPanel = new Ext.Panel({
                baseCls: 'x-plain',
                id: 'login-logo',
                region: 'center'
            });

            var formPanel = new Ext.form.FormPanel({
                baseCls: 'x-plain',
                defaults: {
                    width: 150
                },
                defaultType: 'textfield',
                frame: false,
                height: 70,
                id: 'login-form',
                items: [{
                    fieldLabel: "Username",
                    name: 'user',
                    value: "guest",
                    selectOnFocus: true
                }, {
                    fieldLabel: "Password",
                    inputType: 'password',
                    name: 'password',
                    value: "guest",
                    selectOnFocus: true
                }],
                labelWidth: 90,
                listeners: {
                    'actioncomplete': {
                        fn: this.onActionComplete,
                        scope: this
                    },
                    'actionfailed': {
                        fn: this.onActionFailed,
                        scope: this
                    }
                },
                region: 'south',
                url: submitUrl
            });

            win = new Ext.Window({
                buttons: [{
                    handler: onSubmit,
                    scope: Login,
                    text: "Login"
                }],
                buttonAlign: 'right',
                closable: false,
                draggable: false,
                height: 280,
                id: 'login-win',
                keys: {
                    key: [13], // enter key
                    fn: onSubmit,
                    scope:this
                },
                layout: 'border',
                minHeight: 250,
                minWidth: 400,
                plain: false,
                resizable: false,
                items: [
                    logoPanel,
                    formPanel
                ],
                title: "Authentication",
                width: 400
            });

            form = formPanel.getForm();

            formPanel.on('render', function() {
                var f = form.findField('user');

                if(f) {
                    f.focus();
                }
            }, this, {delay: 200});

            win.show();
        },

        onActionComplete: function(f,a) {
            if(a && a.result) {
                win.destroy(true);

                // get the path
                var path = window.location.pathname,
                    path = path.substring(0, path.lastIndexOf('/') + 1);

                // redirect the window
                window.location = path;
            }
        },

        onActionFailed : function(){
            this.hideMask();
        },

        showMask : function(msg){
            if(!this.pMask){
                // using this.pMask, seems that using this.mask caused conflict
                // when this dialog is modal (uses this.mask also)
                this.pMask = new Ext.LoadMask(win.body, {
                    msg: "Authenticating..."
                });
            }
            if(msg){
                this.pMask.msg = msg;
            }
            this.pMask.show();
            win.buttons[0].disable();
        },

        hideMask : function(){
            this.pMask.hide();
            win.buttons[0].enable();
        }
    };
}();

Ext.onReady(Login.Init, Login, true);

