cs.pro.login = {

	loginWindow: null,

	show: function()
	{
		if(cs.pro.login.loginWindow == null)
		{
			cs.pro.login.loginWindow = new cs.pro.LoginPanel();
		}
		cs.pro.login.loginWindow.show();
		cs.pro.login.loginWindow.getEl().fadeIn();
		cs.pro.login.loginWindow.center();
		cs.pro.login.loginWindow.setPagePosition(cs.pro.login.loginWindow.pageX,cs.pro.login.loginWindow.pageY+20);
	},
	
	submit: function()
	{
		cs.pro.login.loginWindow.formPanel.getForm().submit({
			url: 'json/login/pro',
			method: 'POST',
			waitTitle: cs.pro.i18n.loggingIn,
			waitMsg: cs.pro.i18n.pleaseWait,
			success: cs.pro.login.success,
			failure: cs.pro.login.failure
		});
	},
	
	success: function(form, action)
	{
		cs.pro.login.loginWindow.formPanel.getForm().reset();
		cs.pro.login.loginWindow.hide();
		cs.pro.app.getRole();
	},

	failure: function(form, action)
	{
		switch (action.failureType) {
			case 'client':
				Ext.Msg.show({
					title: cs.pro.i18n.loginFailed,
					msg: cs.pro.i18n.checkFields,
					buttons: Ext.Msg.OK,
					icon: Ext.MessageBox.WARNING,
					closable: false,
					modal: true
				});
				break; 

			default:
				Ext.MessageBox.show({ 
					title: cs.pro.i18n.loginFailed,
					width: 380,
					msg: cs.pro.i18n.checkUsername,
					buttons: Ext.Msg.OK, 
					icon: Ext.MessageBox.ERROR, 
					closable: false, 
					modal: true
				});
		}
	}

};

cs.pro.LoginPanel = Ext.extend(Ext.Window, {

	initComponent: function()
	{
		this.formPanel = new cs.pro.LoginForm();
		this.aboutPanel = new cs.pro.AboutPanel();
		
		Ext.apply(this, {
			title: 'CADspan Pro',
			resizable: false,
			draggable: false,
			closable: false,
			modal: false,
			width: 760,
			height: 300,
			style: 'opacity: 0.0;',
			layout: 'border',
			items: [
				this.aboutPanel,
				this.formPanel
			]
		});

		cs.pro.LoginPanel.superclass.initComponent.apply(this, arguments);
	}
});

cs.pro.LoginForm = Ext.extend(Ext.form.FormPanel, {

	initComponent: function() {

		Ext.apply(this, {

			title: 'Login',
			labelWidth: 180,
			frame: true,
			floating: true,
			bodyStyle: 'padding: 5px 5px 0',
			width: 380,
			autoHeight: true,
			region: 'east',
			defaults: { width: 160 },
			defaultType: 'textfield',

			items: [
				{
					fieldLabel: cs.pro.i18n.usernameOrEmail,
					name:'login',
					allowBlank:false
				},
				{
					fieldLabel: cs.pro.i18n.password,
					name:'password',
					inputType:'password',
					allowBlank:false,
					minLength: 6
				}
			],

			buttons: [{
				text: cs.pro.i18n.login,
				iconCls: 'account',
				handler: cs.pro.login.submit
			},{
				text: cs.pro.i18n.forgot,
				iconCls: 'forgot-password',
				handler: function() {
					window.open('http://www.cadspan.com/account/lost_password');
				}
			},{
				text: cs.pro.i18n.register,
				iconCls: 'register',
				handler: function() {
					window.open('http://www.cadspan.com/account/register');
				}
			}]

		});

		cs.pro.LoginForm.superclass.initComponent.apply(this, arguments);
	}
});


cs.pro.AboutPanel = Ext.extend(Ext.Panel, {

	initComponent: function() {

		Ext.apply(this, {
			region: 'center',
			autoScroll: true,
			frame: true,
			border: true,
			bodyBorder: true,
			autoLoad: 'autoload/pro_upgrade'
		});

		cs.pro.AboutPanel.superclass.initComponent.apply(this, arguments);
	}
});

