var acms = acms || { };

acms.events.on(acms.events.FORM_INITIALIZED, function() {
	
	if (acms.loggedIn === false) {
	
		var login = Ext.create('acms.field.TextField', {
				renderTo  : 'login-login-container',
				vtype     : 'email',
				emptyText : acms.ls('USER_MAIL'),
				fieldCls  : 'textfield180',
				focusCls  : '',
				invalidCls : 'textfield180-error',
				width     : 180,
				height    : 26,
				allowBlank : false,
				style     : {
					'margin-right' : '5px',
					'margin-top'   : '5px'
				},
				listeners : {
					afterrender : function(comp) {
						comp.inputEl.removeCls('x-form-text');
						comp.el.up('.x-reset').addCls('left');
					}
				}
			});
		    password = Ext.create('acms.field.TextField', {
				renderTo  : 'login-login-container',
				cls       : 'left',
				fieldCls  : 'textfield180',
				invalidCls : 'textfield180-error',
				emptyText : acms.ls('USER_PASSWORD'),
				allowBlank : false,
				focusCls  : '',
				width     : 180,
				height    : 26,
				style     : {
					'margin-top'   : '5px'
				},
				listeners : {
					afterrender : function(comp) {
						comp.inputEl.removeCls('x-form-text');
						comp.el.up('.x-reset').addCls('left');
					},
					focus : function(comp) {
						comp.inputEl.dom.type = 'password';
					},
					blur : function(comp) {
						if (comp.getValue() === null) {
							comp.inputEl.dom.type = 'text';
						}
					}
				}
		});
	
		Ext.get('login-submit').on('click', function() {
			if (!login.validate()) {
				acms.Msg.error(acms.ls('ERROR'), acms.ls('INVALID_MAIL'));
			} else if (!password.validate()) {
				acms.Msg.error(acms.ls('ERROR'), acms.ls('INVALID_PASSWORD'));
			} else {
				Ext.Ajax.request({
					url : acms.path + '/index.php',
					method : 'POST',
					params : {
						page     : 'login',
						action   : 'login',
						language : acms.locale,
						layout   : 'json',
						f_login  : login.getValue(),
						f_password : password.getValue()
					},
					success : function (response) {
						var data = Ext.decode(response.responseText)
						
						if (data.success === true) {
							acms.Msg.info(data.title || acms.ls('SUCCESS'), data.message || acms.ls('SUCCESS'));
							window.location.reload();
						} else {
							acms.Msg.error(data.title || acms.ls('ERROR'), data.message || acms.ls('LOGIN_FAILED'));
						}
					},
					failure : function (form, action) {
						var data = Ext.decode(response.responseText)
						acms.Msg.error(data.title || acms.ls('ERROR'), data.message || acms.ls('LOGIN_FAILED'));
					}
				})
			}
		});
	}
	
	// Load phplive status
	Ext.Ajax.request({
		url     : acms.mkUrl('/' + acms.lgetCode() + '/phplive/status.php?l=admin&x=1&deptid=1'),
		method  : 'GET',
		success : function (response) {
			var data = Ext.decode(response.responseText),
			    online = acms.ls(data.is_online === true ? 'ONLINE' : 'OFFLINE'),
			    link = Ext.get('phplive_link');
			
			link.dom.innerHTML = online;
			link.dom.title = online;
			if (data.is_online === true) {
				link.dom.href = acms.mkUrl('/' + acms.lgetCode() + '/phplive/request.php?l=admin&x=1&deptid=1');
			}
		}
	});
	
	// If present load newsletter form
	(function() {
		var news = {
			before : Ext.get('newsletter-before'),
			after  : Ext.get('newsletter-after'),
			submit : Ext.get('newsletter-submit'),
			name   : Ext.get('newsletter-name'),
			mail   : Ext.get('newsletter-mail')
		},
		    initDef = function (comp) {
			var value = '',
			    defValue = comp.dom.value;
			
			comp.getValue = function () {
				return value;
			};
			
			comp.on('blur', function () {
				value = this.dom.value;
				if (value.length === 0) {
					this.dom.value = defValue;
				}
			}, comp);
			
			comp.on('focus', function () {
				if (this.dom.value === defValue) {
					this.dom.value = '';
				}
			});
		};
		
		if (news.before && news.after && news.submit && news.name && news.mail) {
			news.before.setVisibilityMode(Ext.core.Element.DISPLAY);
			news.after.setVisibilityMode(Ext.core.Element.DISPLAY);
			news.after.hide();
			initDef(news.name);
			initDef(news.mail);
			
			news.submit.on('click', function() {
				if (news.name.getValue().length === 0) {
					acms.Msg.error(acms.ls('ERROR'), acms.ls('MISSING_NAME'));
				} else if (news.mail.getValue().length === 0) {
					acms.Msg.error(acms.ls('ERROR'), acms.ls('MISSING_MAIL'));
				} else {
					Ext.Ajax.request({
						url    : acms.path + '/index.php',
						method : 'POST',
						params : {
							page   : 'subscribe',
							action : 'subscribe',
							name   : news.name.getValue(),
							mail   : news.mail.getValue(),
							layout : 'json',
							language : acms.locale
						},
						success : function (response) {
							var data = Ext.decode(response.responseText);
							if (data.success === true) {
								news.before.hide();
								news.after.show();
							} else {
								acms.Msg.error(acms.ls('ERROR'), data.message || acms.ls('ERROR'));
							}
						},
						failure : function (response) {
							try {
								var data = Ext.decode(response.responseText);
								acms.Msg.error(acms.ls('ERROR'), data.message || acms.ls('ERROR'));
							} catch (e) {
								acms.Msg.error(acms.ls('UNEXPECTED_ERROR'), e.message);
							}
						}
					});
				}
			});
		}
	}());
});

