/*global Ext, window, acms */
/*jslint confusion: true, sloppy: true */

Ext.onReady(function () {
	var wnd_id  = Ext.id(),
        form_id = Ext.id();

	acms.LoginWindow = Ext.create('Ext.window.Window', {
		id     : wnd_id,
		modal  : true,
		//title  : 'Bejelentkezés',
		preventHeader : true,
		border : 35,
		bodyBorder : false,
		shadow : false,
		layout : 'fit',
		items  : [{
			xtype : 'form',
			id    : form_id,
			border : 0,
			bodyPadding : 10,
			defaults : {
				width : 396
			},
			listeners : {
				afterrender : function (comp) {
					comp.items.first().focus();
				}
			},
			items : [{
				xtype : 'mailfield',
				name  : 'f_login',
				allowBlank : false,
				fieldLabel : acms.ls('MAIL'),
				enableKeyEvents : true,
				listeners : {
					keypress : function (comp, e) {
						if (e.keyCode === 13) {
							Ext.ComponentMgr.get(form_id).items.getAt(1).focus();
						}
					}
				}
			}, {
				xtype : 'atextfield',
				name  : 'f_password',
				allowBlank : false,
				inputType  : 'password',
				fieldLabel : acms.ls('PASSWORD'),
				enableKeyEvents : true,
				listeners : {
					keypress : function (comp, e) {
						if (e.keyCode === 13) {
							Ext.ComponentMgr.get(form_id).getDockedItems()[0].items.getAt(1).handler();
						}
					}
				}
			}],
			buttons : [{
				text : acms.ls('CANCEL'),
				handler : function () {
					acms.LoginWindow.hide();
				}
			}, {
				text : acms.ls('LOGIN'),
				handler : function () {

					acms.LoginWindow.hide();
					acms.LoginWindow.getForm().submit({
						url : acms.path + '/index.php',
						method : 'POST',
						params : {
							page     : 'login',
							action   : 'login',
							language : acms.locale,
							layout   : 'json'
						},
						success : function (form, action) {
							var data = acms.Fun.getActionData(action);

							acms.Msg.info(data.title || 'Siker!', data.message || 'Regisztráció sikeres!<br/>Hamarosan e-mail címére érekzek a további utasítások!');
							acms.LoginWindow.items.first().getForm().reset();
							window.location.reload();
						},
						failure : function (form, action) {
							acms.Fun.showFailureMsg(action, { }, function () {
								acms.LoginWindow.show();
							});
						}
					});
				}
			}]
		}],
		listeners : {
			afterrender : function (comp) {
				var el = comp.getEl(),
				    north = el.down('.x-resizable-handle-north'),
				    south = el.down('.x-resizable-handle-south'),
				    east = el.down('.x-resizable-handle-east'),
				    west = el.down('.x-resizable-handle-west');

				north.setWidth(north.getWidth() - 70);
				south.setWidth(south.getWidth() - 70);
				east.setHeight(east.getHeight() - 70);
				west.setHeight(west.getHeight() - 70);
			}
		}
	});
	acms.LoginWindow.getForm = function () {
		return this.items.first().getForm();
	};
});

Ext.onReady(function () {
	var wnd_id  = Ext.id(),
	    form_id = Ext.id();

	acms.RegistrationWindow = Ext.create('Ext.window.Window', {
		id     : wnd_id,
		modal  : true,
		//title  : 'Registration',
		preventHeader : true,
		border : 35,
		bodyBorder : false,
		shadow : false,
		layout : 'fit',
		items  : [{
			xtype : 'form',
			id    : form_id,
			bodyPadding : 10,
			defaults : {
				width : 396
			},
			items : [{
				xtype : 'atextfield',
				name  : 'User[FirstName]',
				allowBlank : false,
				fieldLabel : 'Keresztnév'
			}, {
				xtype : 'atextfield',
				name  : 'User[MiddleName]',
				allowBlank : true,
				fieldLabel : 'Középső név'
			}, {
				xtype : 'atextfield',
				name  : 'User[LastName]',
				allowBlank : false,
				fieldLabel : 'Utónév'
			}, {
				xtype : 'atextfield',
				name  : 'User[Title]',
				allowBlank : true,
				fieldLabel : 'Előtag'
			}, {
				xtype : 'mailfield',
				name  : 'User[Mail]',
				allowBlank : false,
				fieldLabel : 'E-mail cím'
			}, {
				xtype : 'atextfield',
				name  : 'User[Password]',
				inputType  : 'password',
				allowBlank : false,
				fieldLabel : 'Jelszó'
			}, {
				xtype : 'atextfield',
				name  : 'User[Password2]',
				inputType  : 'password',
				allowBlank : false,
				fieldLabel : 'Jelszó megimétlése'
			}, {
				xtype : 'atextfield',
				name  : 'User[Telephone]',
				regex : '\\+?[0-9]{10-12}',
				allowBlank : true,
				fieldLabel : 'Telefonszám'
			}, {
				xtype : 'atextfield',
				name  : 'User[MobilePhone]',
				regex : '\\+?[0-9]{10-12}',
				allowBlank : true,
				fieldLabel : 'Mobil telefonszám'
			}, {
				xtype : 'whichfield',
				name  : 'User[WhichPhone]',
				data  : [['Wired', 'Vezetékes'], ['Mobile', 'Mobil']],
				allowBlank : false,
				fieldLabel : 'Megjelenítendő telefonszám'
			}, {
				xtype : 'acmscombo',
				name  : 'User[Sex]',
				data  : [['Male', 'Férfi'], ['Female', 'Nő']],
				allowBlank : false,
				fieldLabel : 'Nem'
			}, {
				xtype  : 'datefield',
				name   : 'User[BirthDate]',
				format : 'Y-m-d',
				allowBlank : false,
				fieldLabel : 'Születési dátum'
			}, {
				xtype : 'boolfield',
				name  : 'User[EnergyBoost]',
				allowBlank : false,
				fieldLabel : 'Használ tömegnövelőt?'
			}, {
				xtype : 'atextfield',
				name  : 'User[FavoriteBrand]',
				allowBlank : true,
				fieldLabel : 'Kedvenc márka'
			}, {
				xtype : 'atextfield',
				name  : 'User[Goals]',
				allowBlank : true,
				fieldLabel : 'Cél(ok)'
			}],
			buttons : [{
				text : 'Mégsem',
				handler : function () {
					acms.RegistrationWindow.hide();
				}
			}, {
				text : 'Regisztráció',
				handler : function () {

					acms.RegistrationWindow.hide();
					acms.RegistrationWindow.items.first().getForm().submit({
						url : acms.path + '/index.php',
						method : 'POST',
						params : {
							language : acms.locale,
							page     : 'register',
							layout   : 'json'
						},
						success : function (form, action) {
							var data = acms.Fun.getActionData(action);

							acms.Msg.info(data.title || 'Siker!', data.message || 'Regisztráció sikeres!<br/>Hamarosan e-mail címére érekzek a további utasítások!');
							acms.RegistrationWindow.items.first().getForm().reset();
						},
						failure : function (form, action) {
							acms.Fun.showFailureMsg(action, { }, function () {
								acms.RegistrationWindow.show();
							});
						}
					});
				}
			}]
		}],
		listeners : {
			afterrender : function (comp) {
				var el = comp.getEl(),
				    north = el.down('.x-resizable-handle-north'),
				    south = el.down('.x-resizable-handle-south'),
				    east = el.down('.x-resizable-handle-east'),
				    west = el.down('.x-resizable-handle-west');

				north.setWidth(north.getWidth() - 70);
				south.setWidth(south.getWidth() - 70);
				east.setHeight(east.getHeight() - 70);
				west.setHeight(west.getHeight() - 70);
			}
		}
	});
});
