// JavaScript Document
$(document).ready(function() {
	// pre-submit callback
	function clearUp(formData, jqForm, options) {
		$('#contactForm .error').hide();
	}
	
	// post-submit callback 
	function showResponse(responseText, statusText, xhr, $form)  { 
	     var returnObj = JSON.parse(responseText);
	     $('#contactForm .error').hide();
	     if (returnObj["errors"].length > 0) {
		     $.each(returnObj["errors"], function() {
		    	 var selector = "label[for*='" + this + "']";
		    	 $('.error', selector).show();	
		     });
	     } else {
		     if (returnObj["success"] == true) {
		    	 $.fancybox.close();
		     } else {
		    	 $("#contactForm").html("<div id='contactStatus' class='failed'><p>An error occured on our end while trying to process your submission. Please try again later.</p></div>");
		     }
	     }
	 }
	
	
	/*
	 * Contact form links
	 */
	$('a.contactLink').click(function() {
		dataURL = "/pages/contact.html";
		$.fancybox({
			'href' : dataURL,
			'autoDimensions': false,
			'width': 640,
			'height': 350,
			'onComplete': function() {
				var options = {
						beforeSubmit: clearUp,
				        success: showResponse  // post-submit callback 
				};
				$('#contact').ajaxForm(options);
			}
		});
		
		return false;
	});	
});
