$(function(){
	if ($("#contact_form").length) setValidate();
	
	var links = $("a.contact-ajax");
	if (links.length) {
		links.click(function(){
			ajaxFailed = true;
			$("#contact_form").attr("id","contact_form1");
			$.ajax({
			   type: "POST",
			   url: "/assets/scripts/contact/contact_form.php",
			   data: "",
			   async: false,
			   success: function(msg){
				   var form = $(msg);
				   
				   form.modal({onClose: closeModal, onOpen: openModal, persist: false, onShow: onshowModal});
				   
				   ajaxFailed = false;
			   },
			   error: function(obj,text,error) { 	$("#contact_form1").attr("id","contact_form"); }
		 	});
			
			return ajaxFailed;
		});
	}
});

function openModal (dialog) {
	dialog.overlay.fadeIn('slow', function () {
		dialog.container.slideDown('slow', function () {
			dialog.data.fadeIn('slow');
		});
	});
}

function closeModal (dialog) {
	dialog.data.fadeOut('slow', function () {
		dialog.container.slideUp('slow', function () {
			dialog.overlay.fadeOut('slow', function () {
				$.modal.close(); // must call this!
				//reset contact form id
				$("#contact_form1").attr("id","contact_form");
			});
		});
	});
}

function onshowModal (dialog) {
	if($("#contact_form").length) {
		setValidate(true);
	}
}
/*****************************************
 Helper Fcns
*****************************************/
// Read a page's GET URL variables and return them as an associative array.
function getUrlVars(u)
{
	var url = u=="" ? window.location.href : u;
	var vars = [], hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function setValidate(ajaxme) {
	// validate signup form on keyup and submit
	$("#contact_form").validate({
		rules: {
			name: "required",
			email: {
				required: "#contact-email:selected",
				email: true
			},
			phone: {
				required: "#contact-phone:selected"
			},
			inquiry: {
				required: true,
			}
		},
		messages: {
			name: "Please enter your name",
			inquiry: "Please enter your request",
			email: "Please enter a valid email address"
		},
		submitHandler: function(form) {
			if (ajaxme) {
				var myform = $("#contact_form");

				var data_string = myform.serialize();
				// user feedback:
				myform.append($('<p class="sending">Please wait while we send your message...</p>')); 
				$("#contact_send").hide();
				name = $("#name").val();			
				myform.fadeTo(500,.5);
				
				$.ajax({
				   type: "POST",
				   url: "/assets/scripts/contact/contactprocess.php",
				   data: data_string+"&page="+window.location.href,
				   async: false,
				   success: function(msg){
					$("#contact-form-container h2").after(
						$("<p class='success'>Thank you for your interest. You'll hear back from us within two business days.</p>")); 
				   },
				   error: function(obj,text,error) {
					$("#contact-form-container h2").after(
						$("<p class='error'>Something went wrong when we tried to send your message. You can try again later "
							+ "or contact us another way.</p>")); 
				   }
				 });
	
				myform.remove();
	
				return false; // We don't want to submit the form since AJAX handled it
				
			} else form.submit();
			
		 }

	});
}

