/*
 *	This file contains all the functionality needed for making subscription requestes to The Loop.
 */
function validateEmail(email){
	var regExp = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	return regExp.test(email);
}

function loopSubmit(){
	// http://domanistudios.com/looper.php","?address="+document.getElementById('emailaddress').value
	if(validateEmail($('#email').val())){
		$.post(
			'http://domanistudios.com/looper.php',
			{ address: $('#email').val() },
			function(data, textStatus){
				if(textStatus == 'success'){
					$('#theLoop').css('background','transparent');
					$('#loopForm').fadeOut('normal', function(){
						$(this).remove();
						$('<div id="thanks">Thanks, we&#39;ll keep you in the loop.</span>').hide().insertBefore('#copyright').fadeIn();
					});
				} else {
					alert('How embarasing. An error occurred when we tried to add you to the mailing list.\n\nPlease try again and if it doesn\'t work this time, feel free to call us and mock us mercilessly.');
				}
			}
		);
	} else {
		alert('The address you entered does not seem to be valid.\nPlease verify the address you entered and try again.')
	}
}

$(document).ready(function(){
	var emailTxt = 'sign-up for our e-mailer';
	
	$('#email').val(emailTxt);
	
	$('#email').focus(function(event){
		if($(this).val() == emailTxt){
			$(this).val('');
		}
	});
	
	$('#email').blur(function(event){
		// If the email field is blank reset the message, otherwise set a 1 minute timeout before resetting the message.
		var delay;
		$('#email').val() == '' ? delay = 0 : delay = 60000;
		setTimeout(function(){ $('#email').val(emailTxt); }, delay);
	});
	
	$('#goButton').click(function(event){
		loopSubmit();
	});
	
	$('#loopForm').submit(function(event){
		loopSubmit();
		return false;
	});
});
