var QtTopLoginModule = function(moduleId,tt,existingUsername) {
	
	this.tt = tt;
	this.init(moduleId,existingUsername);
	
	
//	this.initHttpChannel();
};
QtTopLoginModule.prototype = {
	cfg: {},
	init: function(moduleId,existingUsername) {
		
		this.el = Ext.get('qt-toplogin-module-'+moduleId);
		this.usernameEl = this.el.child('.username-field');
		this.passwordEl = this.el.child('.password-field');
		this.rememberMeEl = this.el.child('.remember-me');
		this.errorEl = this.el.child('.error-td');

		if (Ext.util.Cookies.get('username')) {
			existingUsername = Ext.util.Cookies.get('username');
		}
		
		this.username = new Ext.form.TextField({
			width: 140,
			height: 20,
			cls: 'field',
			renderTo: this.usernameEl,
			enableKeyEvents: true,
			hidden: existingUsername?true:false,
			value: existingUsername,
			listeners: {
			   	keydown: {
			    	fn: function(field,e) {
				    	 if (e.getKey() == Ext.EventObject.ENTER) {
				      		this.makeLogin();
			    		}
			    	},
			    	scope: this
			   	}
	  		}
		});
		
		if (existingUsername) {
			
			this.changeUsernameButton = Ext.get('qt-top-login-change-email');
			this.existingUsernameEl = Ext.get(Ext.DomHelper.append(Ext.get(this.usernameEl),{
				tag:'div', 
				cls:'existing-username', 
				html:existingUsername
			}));
			this.existingUsernameEl.enableDisplayMode();

			this.changeUsernameButton.removeClass('hidden');
			
			this.existingUsernameEl.on('click',function(){
				this.username.setValue('');
				this.username.show();
				this.username.focus();
				this.existingUsernameEl.hide();
				this.changeUsernameButton.addClass('hidden');
			},this)
			this.changeUsernameButton.on('click',function(){
				this.username.setValue('');
				this.username.show();
				this.username.focus();
				this.existingUsernameEl.hide();
				this.changeUsernameButton.addClass('hidden');
			},this)
		}
		
		this.password = new Ext.form.TextField({
			inputType: 'password',
			width: 110,
			height: 20,
			cls: 'field',
			renderTo: this.passwordEl,
			enableKeyEvents: true,
			listeners: {
			   	keydown: {
			    	fn: function(field,e) {
				    	 if (e.getKey() == Ext.EventObject.ENTER) {
				      		this.makeLogin();
			    		}
			    	},
			    	scope: this
			   	}
	  		}
		});
		this.rememberMe = new Ext.form.Checkbox({
			boxLabel: this.tt.remember_me,
			renderTo: this.rememberMeEl,
			checked: true
		});
		
		this.login = this.el.child('.login-button');
		
		this.login.on('click',function() {
			
			this.makeLogin();
			
		},this);
		
		
	},
	buttonStartLoading: function(button) {
		var parent = button.parent();
		parent.addClass('loading');
	},
	buttonStopLoading: function(button) {
		var parent = button.parent();
		parent.removeClass('loading');
	},
	showError: function(error) {
		this.errorEl.addClass('error');
		this.errorEl.dom.title = error;
	},
	hideError: function() {
		if (!this.errorEl.hasClass('error')) return;
		this.errorEl.removeClass('error');
		this.errorEl.dom.title = '';
	},
	makeLogin: function() {
		
		this.buttonStartLoading(this.login);
		this.hideError();
		
		Ext.Ajax.request({
			url: '/users/login',
			scope: this,
			success: function(response, options){
				var succ = Ext.decode(response.responseText);
				
				if(succ.success) {
					if (window.location.pathname=='/' || window.location.pathname=='/content/home') {
						window.location = '/quiettime/journal';
					} else {
						window.location.reload()
					}
				} else {
					this.buttonStopLoading(this.login);
					this.showError(this.tt.error+': '+this.tt.invalid_login_credentials);
				}
			},
			params: { ext:'true','data[User][username]': this.username.getValue(),'data[User][password]':this.password.getValue(),'data[User][remember]':this.rememberMe.getValue()?"on":"off"}
		});
	}
};
