Ext.SSL_SECURE_URL="resources/images/default/s.gif"; 
Ext.BLANK_IMAGE_URL="resources/images/default/s.gif";

Ext.namespace('Nettley');

Login = function(){
	var win,
		form,
		submitUrl = '/ajax/login';

	function onSubmit(){
		form.submit({
			waitMsg: Login.title.msgWait,
			waitTitle: Login.title.msgTitle,
			reset: true,
			success: Login.Success,
			scope: Login
		});
	}
	
	return{
		Init:function(){
			Ext.QuickTips.init();
			
	        var title = {login: 'Login/Email',
       		   password: 'Password',
       		   msgTitle: 'Please wait...',
       		   msgWait: 'Please wait...',
			   winTitle: 'Login',
       		   btnLogin: 'Login'	
		    };
			
			var logoPanel = new Ext.Panel({
				baseCls: 'x-plain',
				id: 'login-logo',
		        region: 'center'
			});
			
			var formPanel = new Ext.form.FormPanel({
		        baseCls: 'x-plain',
		        baseParams: {
		        	module: 'login'
		        },
		        defaults: {
		        	width: 200
		        },
		        defaultType: 'textfield',
		        frame: false,
		        height: 70,
		        id: 'login-form',
		        items: [{
		            fieldLabel: this.title.login,
		            name: 'user',
		            value:  get_cookie('memberName')
		        },{
		            fieldLabel: this.title.password,
		            inputType: 'password',
		            name: 'pass',
		            value: ''
		        }],
		        labelWidth:120,
		        region: 'south',
		        url: submitUrl
		    });
		
		   win = new Ext.Window({
		        buttons: [{
		        	handler: onSubmit,
		        	scope: Login,
		            text: this.title.btnLogin
		        }],
		        buttonAlign: 'right',
		        closable: false,
		        draggable: false,
		        height: 250,
		        id: 'login-win',
		        keys: {
		        	key: [13], // enter key
			        fn: onSubmit,
			        scope:this
		        },
		        layout: 'border',
		        minHeight: 250,
		        minWidth: 530,
		        plain: false,
		        resizable: false,
		        items: [
		        	logoPanel,
		        	formPanel
		        ],
				title: this.title.winTitle,
		        width: 530
		    });
			
			form = formPanel.getForm();
			
			formPanel.on('render', function(){
				var f = form.findField('user');
				
				if(f){
					f.focus();
				}
			}, this, {delay: 200});
			
		    win.show();
		},
		
		Success: 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);

				// set the cookie
				set_cookie('sessionId', a.result.sessionId, '', path, '', '' );
				set_cookie('memberName', a.result.name, '', path, '', '' );
				set_cookie('memberId', a.result.id, '', path, '', '' );
				
				// redirect the window
				window.location = path;
			}
		}
	};
}();

Ext.BasicForm.prototype.afterAction=function(action, success){
	this.activeAction = null;
	var o = action.options;
	if(o.waitMsg){
		Ext.MessageBox.updateProgress(1);
		Ext.MessageBox.hide();
	}
	if(success){
		if(o.reset){
			this.reset();
		}
		Ext.callback(o.success, o.scope, [this, action]);
		this.fireEvent('actioncompleted', this, action);
	}else{
		Ext.callback(o.failure, o.scope, [this, action]);
		this.fireEvent('actionfailed', this, action);
	}
};

Ext.onReady(Login.Init, Login, true);