var email_help_text = 'Enter email address'


function handle_response(data, textStatus, XMLHttpRequest){
	//data should contain an array of [success, errors_array/thank_you_html]
	if(data[0]){ //if the form was saved
		//clear the form
		$("#signup_form #id_email").val('');
		//remove any error message
		$("#signup_form .error").remove();
		//display the lightbox
		$('body').append(data[1]);
		//set lightbox to disappear when the page is clicked anywhere
		//$("#signup_continue").click
		$(".thanks-mess").click(
			function(){
				$(this).remove();
			}
		);
	}
	else{
		//remove any previous error message
		$("#signup_form .error").remove();
		//display the new error message
		$("#signup_form").prepend('<div class="error">'+ data[1]['email'][0] +'</div>');
	}
}


$(document).ready(
	function(){
		//add help text to the <input>
		$("#signup_form #id_email").focus(
			function(){
				jq_this = $(this);
				jq_this.removeClass('help');
				if(jq_this.val() == email_help_text){
					jq_this.val('');
				}
			}
		);
		$("#signup_form #id_email").blur(
			function(){
				jq_this = $(this);
				jq_this.removeClass('help');
				if(jq_this.val().match(/^\s*$/)){
					jq_this.val(email_help_text);
				}
				if(jq_this.val() == email_help_text){
					jq_this.addClass('help');
				}
			}
		);
		$("#signup_form #id_email").blur();
		
		//set submit ajax stuff
		$("#signup_form").submit(
			function(){
				data = {'email': $("#signup_form #id_email").val()};
				$.ajax({
					type: "POST",
					url: signup_ajax_url,
					data:data,
					context: document.body,
					dataType:'json',
					success: handle_response
				});
				return false;
			}
		);
	}
);
