var CLoginForm = CFormHandler.extend({
	FormSlider: null,
	RightTabs: null,

	options: {
		onlogin: Class.empty,
		onlogout: Class.empty
	},

	initialize: function()
	{
		this.parent({
			assets: {
				FormSpinner: 'resources/images/form_spinner.gif',
				HeaderSpinner: 'resources/images/header_load.gif'
			},
			
			keymap: {
				'GlobalLoginForm:esc': this.hideLoginForm.bind(this),
				'GlobalLoginForm:enter': this.login.bind(this)
			}
		});
		
		this.initLoginControls();
	},
	
	initLoginControls: function()
	{
		this.ReBindKeymap();
		this.FormSlider = new Fx.Slide('GlobalLoginForm', {duration: 300});
		this.FormSlider.hide();
		$('GlobalLoginFormContainer').Visible();
		this.Tabs = $ES('li.Right', 'Tabs');
	},
	
	processEscape: function(EventObj)
	{
		console.log(EventObj);
		this.hideLoginForm();
	},
	
	showLoginForm: function()
	{
		$('GlobalLoginEmail').focus();
		this.Tabs.each(function(element){element.hide()});
		this.FormSlider.slideIn();
		return false;
	},
	
	hideLoginForm: function()
	{
		this.FormSlider.slideOut();
		this.Tabs.each(function(element){element.show()});
		this.Blur();
		return false;
	},
	
	login: function()
	{
		if (this.GetLock('Login'))
			return false;
			
		this.SetLock('Login');
		this.Blur();
		this.ShowSpinner( 'PostSpinner', 'GlobalLoginElements', 'FormSpinner', {});

		var PostBody = "Login=" + encodeURIComponent($('GlobalLoginEmail').getValue());
		PostBody += "&Password=" + encodeURIComponent($('GlobalLoginPassword').getValue());
		PostBody += "&Tab=" + encodeURIComponent($('CurrentTabId').getValue());
	
		new Phpr_Request({
			handler:'ev{GlobalLogin_OnLogin}',
			remote: 1,
			postBody: PostBody,
			postBodyOnly: true,
			update: 'multi',
			errorBlockId: 'GlobalLoginError',
			onFailure: this.displayFormError.bind(this),
			onBeforeComplete: function() {
				this.HideSpinner('PostSpinner');
				this.RemoveLock('Login')
			}.bind(this),
			onAfterError: function()
			{
				$('GlobalLoginPassword').focus();
			},
			onSuccess: function()
			{
				this.fireEvent('onlogin');
				$('GlobalLoginEmail').value = "";
				$('GlobalLoginPassword').value = "";
			}.bind(this)
		});
		
		return false;
	},
	
	logout: function()
	{
		this.ShowSpinner( 'LogoutSpinner', 'GlobalLogoutButton', 'HeaderSpinner', {});

		new Phpr_Request({
			handler:'ev{GlobalLogin_OnLogout}',
			postBody: {Tab: $('CurrentTabId').getValue()},
			postBodyOnly: true,
			remote: 1,
			update: 'multi',
			onBeforeComplete: function() {
				this.HideSpinner('LogoutSpinner');
			}.bind(this),
			onSuccess: function()
			{
				this.fireEvent('onlogout');
			}.bind(this)
		});
		
		return false;
	}	
});

CLoginForm.implement(new Events, new Options);

var LoginForm = null;

window.addEvent('domready', function() {
	LoginForm = new CLoginForm();
});