$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

    $('#contact-form').submit(function(){
        $form = $(this);

        // first, remove existing errors
        $form.find('.error').hide();

        // check that all text fields have been filled in
        $.each($form.find('input[type="text"]'), function(i, input) {
            var $input = $(input);
            if ( $input.val() == '' ) {
                $input.focus().siblings('.error').show();
            }
        });
		
		// check that all text fields have been filled in
        $.each($form.find('textarea'), function(i, textarea) {
            var $textarea = $(textarea);
            if ( $textarea.val() == '' ) {
                $textarea.focus().siblings('.error').show();
            }
        });

        // errors were found
        if ( $form.find('.error:visible').length > 0 ) {
            return false;
        }

        $.post($form.attr('action'), $form.serialize(), function(data){
            $('#contact-form').fadeOut();
			$('#message').fadeIn();
			$('#message').delay(3000).fadeOut();
			$('#contact-form').delay(4000).fadeIn();
			$('#email-friend').delay(4000).fadeOut();
			$('#social-btn-close').delay(4000).fadeOut();
			$('#social-btn').delay(4000).fadeIn('fast');
        });

        // prevent browser navigation
        return false;
    });
});
