
$(document).ready(function(){
	$(".imgwrap").css('opacity', 0); //hide images while loading
	
	jQuery.preLoadImages("images/email-entry-bg.png","/images/email-entry-bg.png","images/email-entry-input.png","/images/email-entry-input.png");

	$(".blur").hide();
	$(".overTop").hide();
	clicked = false;
	overTopOpen = false;
	shouldAnim = true;
	
	$(window).load(function(){
		//when all images loaded, unhide images and start animation	
		$(".imgwrap").css('opacity', 1); 
		animLoopIn();

		//after 1 second, start the tagline fade in which will take 6 seconds
		setTimeout(
			function(){
				$(".copy").animate({opacity: .99},6000, "easeInOutSine");	
			}
		, 1000);
		
	});

	//Show and hide email box
	$(".clickarea").click(function() {
		if (overTopOpen == false){
			showOverTop();
		} else {
			hideOverTop();
		}
	});
	
	$(".mlist").click(function(e){
		showOverTop();
		e.preventDefault();
	});
	
	
	//Hide or show default input text
	defaultText="Join our Mailing List";
	$("#email-field").val(defaultText);
	$("#email-field").focus(function(){
		if ($(this).val() == defaultText){
			$(this).val("");
		}
	});
	$("#email-field").blur(function(){
		if ($(this).val() == ""){
			$(this).val(defaultText);
		}
	});	
	
	//Ajax Submit of signup form - from campaign monitor
	$("#subForm").submit(function() {	
		
		// First, disable the form from submitting
		$('#subForm').submit(function() { return false; });
		
		// Grab form action
		formAction = $("form#subForm").attr("action");
		
		// Hacking together id for email field
		// Replace the xxxxx below:
		// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
		id = "hrihuj";
		//emailId = id + "-" + id;
		emailId = "email-field";
		// Validate email address with regex
		if (!checkEmail(emailId)) 
		{
			alert("Please enter a valid email address");
			return false;
		}
		
		// Serialize form values to be submitted with POST
		var str = $("form#subForm").serialize();
		
		// Add form action to end of serialized data
		final = str + "&action=" + formAction;
		
		// Submit the form via ajax
		$.ajax({
			url: "proxy.php",
			type: "POST",
			data: final,
			success: function(data){
				//Check to make sure that the email was accepted
//				alert(data);
				if (data.search(/invalid/i) != -1) {
					alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
				}
				else if (data.indexOf("successfully subscribed") == -1){
				
					alert('Unable to connect to Campaign Monitor. Please try again');
				
				} else {	
					$(".email .slide-wrap").animate({
					    marginLeft: '-225px'
					 }, 250);
					 $(".email .slide-wrap #subForm").animate({opacity: "0"},250);
					 
				}
			}		
		});
	
	return false;		
	});
});

showOverTop = function(){
	time = 500;
	$(".overTop #email-field").blur();
	$(".blur").fadeIn(time);
	$(".overTop").fadeIn(time, function(){
	/*	setTimeout(
			function(){
				$("#email-field").focus();					
			}
		, 2000); */
	});			
	overTopOpen = true;
	shouldAnim = false;
};


hideOverTop = function(){
	time = 500;
	$(".blur").fadeOut(time);
	$(".overTop").fadeOut(time);
	overTopOpen = false;
	shouldAnim = true;
	animLoopIn();	
};

animLoopIn = function(){
	easeIn = "easeInSine";
	easeOut = "easeOutSine";	
	speed = 850;
	$(".frame-1").animate({opacity: 0}, speed, easeIn, function(){
		$(".frame-2").animate({opacity: 0}, speed, easeOut, function(){
			if (shouldAnim == true){ animLoopOut(); }
		});		
	});
};

animLoopOut = function(){
	easeIn = "easeInSine";
	easeOut = "easeOutSine";	
	speed = 850;
	$(".frame-2").animate({opacity: 1}, speed, easeIn, function(){
		$(".frame-1").animate({opacity: 1}, speed, easeOut, function(){
			if (shouldAnim == true){ animLoopIn(); }
		});
	});
};


function checkEmail(email)
{	
	var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var emailVal = $("#" + email).val();
	return pattern.test(emailVal);
}


(function($) {
  var cache = [];
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)	