
var getTitle;
$(document).ready(function(){
 
	var mailingListFormDefaulText;

	var subForm = function(){
		$('#submitLink').blur();
		
		var baseUrl = '/wp/emailSignup.php'; 
		var email = $('#listForm').attr("value");
		var doneProcessing = false;
		var badEmail = false;
		
		if(email == mailingListFormDefaulText || email == ""){
				$('#listSignUpMessage').text("You need to put something in the box. Just sayin'.");
				$('#listSignUpMessage').show();
				
				setTimeout(function() {
				    $("#listSignUpMessage").fadeOut(1000, function(){$("#listSignUpMessage").hide('slow')});
				}, 6000);
				return;
			} else if(!validate(email)){
				$('#listSignUpMessage').text("You need to put a valid email in the box. Just sayin'.");
				$('#listSignUpMessage').show();
				setTimeout(function() {
				    $("#listSignUpMessage").fadeOut(1000, function(){$("#listSignUpMessage").hide('slow')});
				}, 6000);
				return;
			} 
		
		$('#listForm').hide();
		
		$.ajax({
		    type: 'get',
			url: baseUrl,
			data: {email: email},
			success: function(){
				doneProcessing = true;
				$('#listSignUpMessage').text("Thank you! We will add you to the list.");
				setTimeout(function() {
				    $("#listSignupStuff").fadeOut(1000, function(){$("#listSignupStuff").hide('slow')});
				}, 6000);
				
			},
			error: function(){
				$('#listSignUpMessage').text("Oops! Something went wrong. Sorry! This is embarrassing.");
			}
		});
		$('#listSignUpMessage').show();
		animateProcessingText = (function(){
			var step = 0;
			var steps = ["Processing", "Processing.", "Processing..", "Processing..."];
			return function(){
				$('#listSignUpMessage').show().text(steps[step++ % steps.length]);
				setTimeout(function() {
					if(!doneProcessing){
						animateProcessingText();	
					}
				}, 200);
			}
		})();
		animateProcessingText();
        $("#submitLink").hide();
	}

	$("#submitLink").live('click', subForm);

	mailingListFormDefaulText = $('#listForm').attr("value");
	$('#listForm').live(
		'click',
		function(){
			if($('#listForm').attr("value") == mailingListFormDefaulText){
				$('#listForm').attr("value", "");
			}
			$("#submitLink").show();
		}
	)
	
	$('#listForm').live('keypress', function(e){
		var code = (e.keyCode ? e.keyCode : e.which);
		if(code == 13) { //Enter keycode
		  subForm();
		}
	 
	});
	
});

function validate(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

   if(reg.test(email) == false) {
      return false;
   } else {return true;}
}
