var Auth = Auth || {};

Auth.lightbox = null;
Auth.flashId = null;
Auth.onSuccess = null;
Auth.refreshPage = false;
Auth.pane = 'login';
Auth.loggedIn = false;

Auth.FBConnect = function()
{
	Ajax('/fbconnect', {success: function(json)
		{
			if (!json.success)
			{
				alert("We're sorry, Facebook is not answering our calls right now. Please try again later.");
			}
			else
			{
				Auth.success(json);
			}
		}
	});
}

// return Flash object with Auth.FlashId
Auth.Flash = function()
{
	if (Auth.flashId)
	{
		if (window.document[Auth.flashId]) {
			return window.document[Auth.flashId];
		}
		if (navigator.appName.indexOf("Microsoft Internet") == -1) {
			if (document.embeds && document.embeds[Auth.flashId])
				return document.embeds[Auth.flashId];
		}
		else
		{
			return document.getElementById(Auth.flashId);
		}
	}
}

Auth.display = function(pane)
{
	doubleTrack('GO_Nav_Login_Create_account');
	if (pane != null) {
		Auth.pane = pane;
	}
	
	if (Auth.lightbox)
	{
		Auth.lightbox.show();
		Auth.lightbox.center();
		return false;
	}

	Ajax.HTML(
		'/login',
		{
			success: function(html)
			{
				Auth.lightbox = new WK.Lightbox(html);
				Auth.lightbox.onHide(function()
				{
					if (Auth.Flash())
					{
						Auth.Flash().AuthCallback("FAILURE", {});
					}
					Auth.onSuccess = false;
				});
				
				// assign ajax submit
				Auth.lightbox.onShow(function()
				{
					Auth.formReady();
					Auth.registrationFormReady();
					Auth.passwordFormReady();
				});

				Auth.lightbox.show();
				Auth.navigation(Auth.pane);
	
			}
		}
	);
	
	return false;
}
Auth.formReady = function()
{
	$('#registerPane').hide();
	$('#forgotPassword').hide();
	$('.buttons .login').addClass('selected');

	$('#authForm').submit(function()
	{
		Auth.submit(this);
		return false;
	}).attr('action', '');
	
	// forgot password link
	var forgotPass = $(document.createElement('a'));
	forgotPass.attr('href', '#').attr('id', 'forgotPasswordLink').html('Forgot password');
	forgotPass.insertBefore("#authForm div.submit");
	forgotPass.click(function()
	{
		$('#authentication').hide();
		$('#forgotPassword').show();
	});
	
	$('#authForm input[type="text"]:first').focus();
}

Auth.registrationFormReady = function()
{
	$('#registrationForm').submit(function()
	{
		Auth.registrationSubmit(this);
		return false;
	}).attr('action', '');
}
Auth.passwordFormReady = function()
{
	$('#passwordForm').submit(function()
	{
		Auth.passwordSubmit(this);
		return false;
	}).attr('action', '');
	
	// forgotten password cancel link
	//Auth.passwordLink().insertAfter("#passwordForm input:submit");
}
Auth.passwordLink = function()
{
	var cancel = $(document.createElement('a'));
	cancel.attr('href', '#').attr('id', 'cancelForgotPasswordLink').html('Cancel');
	cancel.click(function()
	{
		$('#authentication').show();
		$('#forgotPassword').hide();
	});
	return cancel;
}

Auth.submit = function(form)
{
	var data = $(form).serialize();
	Ajax(
		'/login',
		{
			'data': data,
			'success': function(json)
			{
				if (!json.success)
				{
					$('#authForm').replaceWith(json.html);
					Auth.formReady();
					Auth.lightbox.center();
				}
				else
				{
					Auth.success(json);
				}
			}
		}
	);
}

Auth.registrationSubmit = function(form)
{
	var data = $(form).serialize();
	Ajax(
		'/register',
		{
			'data': data,
			'success': function(json)
			{
				if (!json.success)
				{
					$('#registrationForm').replaceWith(json.html);
					Auth.registrationFormReady();
					Auth.lightbox.center();
				}
				else
				{
					Auth.success(json);
				}
			}
		}
	);
}

Auth.passwordSubmit = function(form)
{
	var data = $(form).serialize();
	Ajax(
		'/forgot/password',
		{
			'data': data,
			'success': function(json)
			{
				$('#forgotPassword').replaceWith(json.html);
				if (!json.success)
				{
					Auth.passwordFormReady();
					Auth.lightbox.center();
				}
				else
				{
					Auth.passwordLink().insertAfter('#emailSent');
				}
			}
		}
	);
}

Auth.success = function(json)
{
	if (Auth.refreshPage != false) {
		location.replace(Auth.refreshPage);
	}
	Auth.lightbox.onHide(null).destroy();
	if (json.mainMenu)
	{
		$('#mainMenu').replaceWith(json.mainMenu);
		MainMenu.init();
		FollowMe.init();
	}

	if (Auth.Flash())
	{
		Auth.Flash().AuthCallback("SUCCESS", json.user);
	}
	Auth.lightbox = null;

	$('body').addClass('loggedIn');
	Auth.loggedIn = true;
	
	if (typeof(Auth.onSuccess) == 'function')
	{
		Auth.onSuccess();
		Auth.onSuccess = null;
	}
}

Auth.navigation = function(btn) {
	$('.buttons li a').removeClass('selected');
	$('#forgotPassword').hide();

	switch (btn) {
		case 'login':
			$('#authentication').show();
			$('#registerPane').hide();
			$('.buttons .login').addClass('selected');
		break;
		case 'register':
			$('#authentication').hide();
			$('#registerPane').show();
			$('.buttons .newaccount').addClass('selected');
		break;
		case 'loginfb':
			// facebook connect
			$('#authentication').hide();
			$('#registerPane').hide();
			$('.buttons .loginfb').addClass('selected');
		break;
	}
	Auth.lightbox.center();
}

Account = {};
Account.display = function(showRules)
{
	if (Auth.lightbox)
	{
		return Auth.lightbox.show();
	}

	Ajax.HTML(
		'/account/edit',
		{
			success: function(html)
			{
				Auth.lightbox = new WK.Lightbox(html);
				
				// assign ajax submit
				Auth.lightbox.onShow(Account.formReady);
				if (showRules == true) {
					Auth.lightbox._modal = true;
				}
				Auth.lightbox.show();
				if (showRules == true) {
					Account.showRules();
				}
			}
		}
	);
}

Account.formReady = function()
{
	$('#editRegistrationForm').submit(function()
	{
		Account.submit(this);
		return false;
	}).attr('action', '');
	
	// add cancel link
	var cancel = $(document.createElement('a'));
	cancel.attr('href', '#').attr('id', 'cancelEditAccountLink').html('Cancel');
	cancel.click(function()
	{
		Auth.lightbox.hide();
	});
	//cancel.insertAfter('#editRegistrationForm .submit input');
	
	$('#editRegistrationForm input[type="text"]:first').focus();
}

Account.showRules = function()
{
	$('#agreeRules').show();
	$('#passwordPane').hide();
}

Account.submit = function(form)
{
	var data = $(form).serialize();
	Ajax(
		'/account/edit',
		{
			'data': data,
			'success': function(json)
			{
				if (!json.success)
				{
					$('#editAccount').replaceWith(json.html);
					Account.formReady();
				}
				else
				{
					Auth.success(json);
				}
			}
		}
	);
}