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

jQuery(document).ready(function($) {
	
	// AJAX Form Submit ========================================
	$("#subForm input:submit").click(function() {	
		
		// First, disable the form from submitting
		$('form#subForm').submit(function() { return false; });
		
		// Grab form action
		formAction = $("form#subForm").attr("action");
		
		// Hacking together id for email field
		// Replace the xxxxx belowkey: "value", 
		// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
		id = "flutlr";
		emailId = id + "-" + id;
		
		// Validate email address with regex
		if (!checkEmail(emailId)) 
		{
			alert("Please enter a valid email address");
			return;
		}
		
		// 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: "/shop/js/jquery/proxy.php",
			type: "POST",
			data: final,
			success: function(data){
				//Check to make sure that the email was accepted
				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 {
					$("form#subForm").hide(); // If successfully submitted hides the form
					$("span.thanks").fadeIn("fast");  // Shows "thanks! we'll keep you posted" div
				}
			}
		});
	});
	
	// HOME PAGE ---------------------------------
	$('div.home_gallery').cycle({
		prev: '.prev_slide',
		next: '.next_slide'
	});
	
	$('.pause_slide').click(function() { 
        $('.home_gallery').cycle('pause');
        $(this).hide();
        $('.play_slide').show();
        return false;
    });
    $('.play_slide').click(function() { 
        $('.home_gallery').cycle('resume');
        $(this).hide();
        $('.pause_slide').show();
        return false; 
    });
	
	// CONTACT PAGE -------------------------------
	// Hide all contact info at first
	$('.page_copy .contact').hide();
	
	// Click funtion to show contact info
	$('body.contact .page_copy h3').click(function() {
		$('.page_copy .contact').slideUp();
		$(this).next().slideDown();
	});
	
	// LOOKBOOK -------------------------------
	var $container = $('ul.lookbook');

	$container.imagesLoaded( function(){
	  $container.masonry({
	    itemSelector : '.lookbook_cover'
	  });
	});
	
	// VIDEOS -------------------------------
	$('ul.video_grid li a').click(function() {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'			: 640,
			'height'		: 480,
			'href'			: this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?title=0&byline=0&portrait=0&clip_id=$1'),
			'type'			: 'swf'
		});
		return false;
	});
	
});
