$(document).ready(function(){

// Load info
var li_s = new Image();
li_s.src = '/img/li_s.gif';

// Display auth items
function auth_reset()
{
	$("[id^='auth_']").hide();
	$("[id^='auth_'][id$='_link']").show();
	$("#auth_info").html('').show();
}
auth_reset();

// Clean forms
$("input[id^='auth_']").click
(
	function(event)
	{
		if (($(this).val() == 'E-mail') || ($(this).val() == 'Password'))
		{			$(this).val('');
		}
	}
);

// Login form
$("#auth_login_link").click
(
	function(event)
	{
		event.preventDefault();

		auth_reset();

		$("#auth_login_link").hide();
		$("#auth_login_email, #auth_login_password, #auth_login_submit").show();
	}
);
// Register form
$("#auth_register_link").click
(
	function(event)
	{
		event.preventDefault();

		auth_reset();

		$("#auth_register_link").hide();
		$("#auth_register_email, #auth_register_submit").show();
	}
);
// Forgot form
$("#auth_forgot_link").click
(
	function(event)
	{
		event.preventDefault();

		auth_reset();

		$("#auth_forgot_link").hide();
		$("#auth_forgot_email, #auth_forgot_submit").show();
	}
);

// Login
$("#auth_login_submit").click
(
	function(event)
	{
		event.preventDefault();

		$("#auth_info").html("");

		if (!$("#auth_login_email").val() || !$("#auth_login_password").val()) return false;

		$("#auth_login_email, #auth_login_password").css('background','#FFF url('+li_s.src+') right center no-repeat');

		$.getJSON
		(
			"/index.php?page=auth&mode=login",
			{
				email: $("#auth_login_email").val(),
				password: $("#auth_login_password").val()
			},
			function(json)
			{
				$("#auth_login_email, #auth_login_password").css('background','#FFF');

				if (json.error)
				{
					$("#auth_info").html(json.error);
				}
				else
				{
					window.location.reload(false);
				}
			}
		);
	}
);
// Register
$("#auth_register_submit").click
(
	function(event)
	{
		event.preventDefault();

		$("#auth_info").html("");

		if (!$("#auth_register_email").val()) return false;

		$("#auth_register_email").css('background','#FFF url('+li_s.src+') right center no-repeat');

		$.getJSON
		(
			"/index.php?page=auth&mode=register",
			{
				email: $("#auth_register_email").val()
			},
			function(json)
			{
				$("#auth_register_email").css('background','#FFF');
				if (json.error)
				{
					$("#auth_info").html(json.error);
				}
				else
				{					window.location.reload(false);
				}
			}
		);
	}
);
// Forgot
$("#auth_forgot_submit").click
(
	function(event)
	{
		event.preventDefault();

		$("#auth_info").html("");

		if (!$("#auth_forgot_email").val()) return false;

		$("#auth_forgot_email").css('background','#FFF url('+li_s.src+') right center no-repeat');

		$.getJSON
		(
			"/index.php?page=auth&mode=forgot",
			{
				email: $("#auth_forgot_email").val()
			},
			function(json)
			{
				$("#auth_forgot_email").css('background','#FFF');
				if (json.error)
				{
					$("#auth_info").html(json.error);
				}
				else
				{
					auth_reset();
					$("#auth_info").html(json.info);
					$("#auth_login_email").val(json.email);
				}
			}
		);
	}
);

// Affiliate link
$('#affiliate_link').click
(
	function()
	{		$(this).select();
	}
);

//
$("#anchor_register").click
(
	function(event)
	{
		auth_reset();

		$("#auth_register_email").css('border', '3px solid red');
		setTimeout(function(){$("#auth_register_email").css('border', 'none');}, 4000);

		$("#auth_register_link").hide();
		$("#auth_register_email, #auth_register_submit").show();
	}
);

});