$(document).ready( function()
	{
		deobfuscateEmails();
		initSignupForm();
	}
);


function deobfuscateEmails()
{
	$(".replaceAt").replaceWith("@");
	$(".obfuscate").each(function(i){
		this.href = "mailto:" + this.innerHTML;
	});
}


function initSignupForm()
{
	signup_form = $("div#signup").html();
	$("#signup_form #submit").click(function(){
		$("div#response").html('<p><span class="signup_red">Sending email, please wait...</span></p>');
		$.ajax({
			type: "POST",
			url:  "/signup",
			data: "email=" + $("input#email").val(),
			success: function(result){
				// The "result" variable should contain the email address if successful, "error" or "blank" if not.
				if (result == "error") {
					$("div#response").html('<p><span class="signup_red">There was an error submitting that email address.</span></p>').show();
					$("div#signup").html(signup_form);
				} else if (result == "blank") {
					$("div#response").html('<p><span class="signup_red">Oops! Please enter an email address.</span></p>');
					$("div#signup").html(signup_form);
				} else {
					$("div#response").html('<p><span class="signup_green">Your email has been submitted. Thanks!</span></p>');
					$("div#signup").html('');
				}
				initSignupForm();
			}
		});
		return false;
	});
}
