Ext.form.Field.prototype.msgTarget = 'side';

// create namespace
Ext.namespace('gingle');

CheckKeyTriggerField = Ext.extend(Ext.form.TriggerField, 
{
	imageIndex: 1,
	initComponent: function()
	{
		CheckKeyTriggerField.superclass.initComponent.call(this);
		this.triggerConfig = 
		{
			tag: 'span',
			cls: 'checkkey',
			cn: [
			{
				tag: "img",
				src: this.loginUrl + "?checkImage=" + this.imageIndex,
				id: 'checkImage'
			}]
		};
	},
	onTriggerClick: function()
	{
		this.imageIndex = this.imageIndex + 1;
		Ext.get('checkImage').dom.src = this.loginUrl + "?checkImage=" + this.imageIndex;
		
	}
});
Ext.reg('checkkeytrigger', CheckKeyTriggerField);
// create application
gingle.login = function()
{
	var config;
	var loginDlg;
	var loginForm;
	var logoutDlg;
	var logoutForm;
	var loginflag = false;
	// public space
	return {
		hide: function()
		{
			if (loginDlg) 
			{
				loginDlg.hide();
			}
		},
		show: function()
		{
			if (!loginDlg && loginflag == false) 
			{
				loginForm = new Ext.form.FormPanel({
					baseCls: 'x-plain',
					autoHeight: true,
					url: config.url,
					baseParams: config.baseParams,
					frame: true,
					items: [{
						xtype: 'fieldset',
						title: new Date().format('l d F Y - H:i'),
						autoHeight: true,
						html: "<div class='banner'></div>"
					}, {
						xtype: 'fieldset',
						title: '登录域，请您登录',
						autoHeight: true,
						frame: true,
						items: [{
							xtype: 'textfield',
							fieldLabel: '用户名',
							name: 'username',
							id: 'username',
							allowBlank: false,
							width: '200',
							minLength: 2,
							columns: 2
						}, {
							xtype: 'textfield',
							fieldLabel: '密&nbsp;&nbsp;&nbsp;码',
							name: 'password',
							inputType: 'password',
							allowBlank: false,
							width: '200',
							columns: 2,
							minLength: 6
						}, {
							xtype: 'checkkeytrigger',
							fieldLabel: '验证码',
							name: 'loginKey',
							allowBlank: false,
							width: '200',
							minLength: 4,
							maxLength: 4,
							loginUrl: config.url
						}]
					}]
				});
				loginDlg = new Ext.Window({
					width: 390,
					height: 330,
					resizable: false,
					plain: true,
					closeAction: 'hide',
					focus: function()
					{
						Ext.get('username').focus();
					},
					keys: {
						key: 13,
						fn: function(e)
						{
							
							if (loginForm.form.isValid()) 
							{
								Ext.getBody().mask("正在登录，请稍后...", "x-mask-loading");
								loginForm.form.submit({
									reset: false,
									success: function(form,action)
									{
									
										Ext.getBody().unmask();
										config.success(action.result);
										loginDlg.hide();
										loginflag = true;
									},
									failure: function(form,action)
									{	
										Ext.getBody().unmask();
										config.failure(action.result);
										loginDlg.hide();
										loginflag = false;
									},
									scope: loginForm
								});
								
							}
						}
					},
					buttonAlign: 'center',
					bodyStyle: 'padding:5px;',
					title: '登录窗口',
					iconCls: 'icon-login',
					items: loginForm,
					buttons: [{
						text: '登 录',
						handler: function()
						{
							if (loginForm.form.isValid()) 
							{
								Ext.getBody().mask("正在登录，请稍后...", "x-mask-loading");
								loginForm.form.submit({
									reset: false,
									success: function(form,action)
									{
										
										Ext.getBody().unmask();
										config.success(action.result);
										loginDlg.hide();
										loginflag = true;
									},
									failure: function(form,action)
									{
										
										Ext.getBody().unmask();
										config.failure(action.result);
										loginDlg.hide();
										loginflag = false;
									},
									scope: loginForm
								});
								
							}
						}
					}, {
						text: '重 置',
						handler: function()
						{
							loginForm.form.reset();
						}
					},{
						text: '注册',
						handler:function()
						{
							loginDlg.hide();
							eval(window.parent.userRegedit());
						}
					}]
				});
				loginDlg.on('hide', function()
				{
					loginForm.form.reset();
				});
			}
			loginDlg.show();
			
		},
		init: function(conf)
		{
			Ext.QuickTips.init();
			config = conf ||
			{};
			if (!config.url) 
			{
				config.url = '';
			}
			if (!config.success) 
			{
				config.success = function()
				{
					loginDlg.hide();
				};
			}
			if (!config.failure) 
			{
				config.failure = function()
				{
					loginDlg.hide();
				};
			}
			if(!config.baseParams)
			{
				config.baseParams='';
			}
			this.show();
		}
	};
}
();
