$(document).ready(function() {
	$("#contact_link").colorbox({
		inline: true,
		width: 400,
		height: 425,
		title: " ",
		onClosed: function() {
			$("#contact_form")
				.find(":input")
					.val(null)
				.end()
				.find("#contact_message")
					.hide()
				.end()
				.find("form")
					.show();
		}
	});
	
	$("#contact_form_ajax").submit(function() {
		var action = $(this).attr("action"), data = $(this).serialize();
		$.ajax({
		  url: action,
		  type: "post",
		  data: data,
		  dataType: "json",
		  beforeSend: function() {
			  $("<img src='images/loading.gif' class='loading_image' />").insertAfter("#contact_form_submit");
		  },
		  success: function(response){
			  if ( !response.success ) {
			  	$("#contact_message")					
					.removeClass("success")
					.addClass("error")
					.html(response.message)
					.slideDown();
			  } else {
				  $("#contact_form_ajax, #contact_message").fadeOut(function() {
					  $.colorbox.resize({ innerHeight:75, innerWidth:400 });
					  $("#contact_message")
					  	.removeClass("error")
						.addClass("success")
						.html(response.message)
						.fadeIn();
					  setTimeout(function() { 
					  	$.colorbox.close(); 
					}, 2500);
				  });
			  }
			  $(".loading_image").remove();
		  }
		});
		return false;
	});
});
