$(function() {

	//var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	//document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

	// Ajax Form
	$("#subscribeform").submit(function() {
	
		$("#submit").attr('disabled', 'disabled');
	
		$("#confirmation").fadeOut(function() {
			
			// First, disable the form from submitting
			//$('#subscribeform').submit(function() { return false; });
			
			// Grab form action
			formAction = $("form#subscribeform").attr("action");
			
			// Replace the xxxxx below:
			// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
			emailId = "urdykd";
			emailId = emailId.replace("/", "");
			emailId = emailId + "-" + emailId;
			
			// Validate email address with regex
			if (!checkEmail(emailId))
			{
				$("#confirmation").text('Please enter a valid email address').fadeIn();
				$("#submit").removeAttr('disabled');
				return;
			}
			
			$("#confirmation").text('Loading...').fadeIn();
			
			// Serialize form values to be submitted with POST
			var str = $("form#subscribeform").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){
					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.');
		            	$("#submit").removeAttr('disabled');
					} else {
						// If successfully submitted hides the form
						//$("#subscribe").hide();
						// Shows "Thanks for subscribing" div
						$("#confirmation").text('Thank you for subscribing!').fadeIn();
						$("#confirmation").tabIndex = -1;
			            $("#confirmation").focus(); // For screen reader accessibility
			            $("#submit").removeAttr('disabled');
		            }
					
					// Fire off Google Analytics fake pageview
					var pageTracker = _gat._getTracker("UA-12039605-1");
					pageTracker._trackPageview("/newsletter_signup");
				}
			});
					
		});
		
		return false;

	});
	
	// Overlabel
	$('.overlabel, #searchform label').overlabel();
	
	// Cycle screenshots
	$('#iphone-demo-cycler').cycle({
		fx:			'fade',
		timeout:	5000,
		speedIn:	0,
		SpeedOut:	1000
	});
	
	// Tracking for Google Analytics
	$('#cta-download').click(function() {
		recordOutboundLink(this, 'Download', 'iOS');
		return false;
	});
	
});


// Email Validation for Campaignmonitor AJAX Form

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);
}


// Tracking for Google Analytics

function recordOutboundLink(link, category, action) {
	try {
		var pageTracker=_gat._getTracker("UA-12039605-1");
		pageTracker._trackEvent(category, action);
		setTimeout('document.location = "' + link.href + '"', 100)
	}catch(err){}
}
