/**
 * Login Javascript
 * Handles authentication
 *
 * @author Erik St. Martin <alakriti@gmail.com>
 * @package CDs-2-Go
 * @version 2.0.0
 * @copyright 2007 CDs-2-Go
 */
App.Login = {
    init : function(){
        Ext.QuickTips.init();
        Ext.form.Field.prototype.msgTarget = "side";

        var loginForm = new Ext.form.Form({
            method: "post",
            labelWidth: 85,
            buttonAlign: "right",
            url: "auth/index",
            baseParams: {view: 'json', locale: (App.Registry.get('locale') ? App.Registry.get('locale') : 'en')},
            labelAlign: "right",
            waitMsgTarget: Ext.get('loginDialog')
        });

        loginForm.fieldset(
	       {legend: (App.Registry.get('loginLegend') ? App.Registry.get('loginLegend') : 'Login Information')},
	       new Ext.form.TextField({
                name: "username",
                fieldLabel: (App.Registry.get('usernameLabel') ? App.Registry.get('usernameLabel') : 'Username'),
                width: 200,
                autocomplete: 'off',
                allowBlank: false
            }),
            new Ext.form.TextField({
                name: "password",
                fieldLabel: (App.Registry.get('passwordLabel') ? App.Registry.get('passwordLabel') : 'Password'),
                inputType: "password",
                width: 200,
                autocomplete: 'off',
                allowBlank: false
            })
        );

        // render form
        loginForm.render("loginForm");

        // create dialog
        var dialog = new Ext.LayoutDialog("loginDialog", {
                modal: true,
                closable: false,
                resizable: false,
                width:375,
                height: (App.Registry.get('logout') ? 225 : 200),
                shadow:true,
                minWidth:375,
                minHeight: (App.Registry.get('logout') ? 225 : 200),
                proxyDrag:true,
                title:(App.Registry.get('appName') ? App.Registry.get('appName') : 'My Project Center'),
                center:{initialSize: 200}

        });

        var submit = function() {
            loginForm.submit({
                waitMsg: (App.Registry.get('waitLabel') ? App.Registry.get('waitLabel') : 'Please Wait...'),
                reset: true,
                scope:loginForm,
                success: function (form, action) {
			    	if(action.result){
			    		dialog.hide();
			      		App.Util.processResponse(action.result);
			    	}
			    },
			    failure: function (form, action) {
			    	if(action.result){
			      		App.Util.processResponse(action.result);
						if(action.result.loginMessage){
							var loginMessage = Ext.get('loginMessage');
							if(loginMessage){
								loginMessage.dom.innerHTML = action.result.loginMessage;
								dialog.resizeTo(375,225);
							}
						}
					}
			    }
           });
        }

        // add buttons to dialog
        dialog.addKeyListener(13, function(){submit();}, this);
        dialog.setDefaultButton(dialog.addButton((App.Registry.get('loginLabel') ? App.Registry.get('loginLabel') : 'Log in'), function(){submit();}));

        dialog.addButton((App.Registry.get('resetLabel') ? App.Registry.get('resetLabel') : 'Reset'), function() {
        		loginForm.reset();
        		var loginMessage = Ext.get('loginMessage');
				if(loginMessage){
					loginMessage.dom.innerHTML = '';
				}
				dialog.resizeTo(375,200);
       		});

        // create dialog layout
        var layout = dialog.getLayout();
        layout.beginUpdate();
        layout.add('center', new Ext.ContentPanel('center', {title: (App.Registry.get('loginLabel') ? App.Registry.get('loginLabel') : 'Log in'), fitToFrame:true}));
        layout.endUpdate();

        dialog.show();
    }
}

Ext.EventManager.onDocumentReady(App.Login.init, App.Login, true);
