jQuery(document).ready(function(){
 var _in_flight = false;
 
 $('#contact-form').ajaxForm({
     dataType: 'json',
     beforeSerialize: function (form, options) {
       $('.form-field').val(function () {
         var val = $(this).val();
         var alt = $(this).attr('alt');
         if (val == alt)
         {
           return '';
         }
         else
         {
           return val;
         }
       });
     },
     beforeSubmit: function() {
       if (_in_flight) {
         return false;
       }
       
       _in_flight = true;
       return true;
     },
     complete: function(XMLHttpRequest, textStatus) {
       // fill in the suggestions
       $('.form-field').val(function () {
         var val = $(this).val();
         var alt = $(this).attr('alt');
         if (val == '')
         {
           return alt;
         }
         else
         {
           return val;
         }
       });
       
       // We're done here.
       _in_flight = false;
     },
     success: function (data, textStatus, XMLHttpRequest) {
       if (!data['success']){
         // shake the form
         $('#contact-form').effect('shake', { times:2, distance:10 }, 100);
       }
       else
       {
         // change the submit button's style and make it not clickable
         var contactSubmit = $('#contact-submit');
         contactSubmit.addClass('contact-thank-you');
         contactSubmit.unbind('click');
         contactSubmit.click(function() {
           return false;
         });

         var formFields = $('#contact-form input, #contact-form textarea');
         formFields.attr('disabled', 'disabled');
         formFields.addClass('disabled-fields');
       }
       
       // We're done here too.
       _in_flight = false;
     }
   });

});
