$(document).ready(function() {

	/* Hint Text */
	$("body.home input, body.home textarea").hintText().bind('focus', function () {
		$(this).removeClass('error');
	});

	$('#contact-form').submit(function (e) {
	
		var form = $(this);
	
		form.find('.error').removeClass('error');
	
		e.preventDefault();
	
		//Get the data from all the fields
		var fields = {
			name: $('input[name=name]'),
			email: $('input[name=email]'),
			comment: $('textarea[name=comment]')
		};
		
		// Error holder
		var errors = $([]);
		
		// Name
		if (fields.name.val().length == 0) {
			errors.push(fields.name[0]);
		}
		
		// Email
		if (fields.email.val().length == 0 || !/^[^@\s]+@[^.@\s]+(?:\.[^.@\s]+)+$/.test(fields.email.val())) {
			errors.push(fields.email[0]);
		}
		
		// Comment
		if (fields.comment.val().length == 0) {
			errors.push(fields.comment[0]);
		}
			
		// Check for errors and show them
		if (errors.length > 0) {
			errors.addClass('error');
			form.trigger('show-hints');
		}
		// Submit the form via AJAX
		else {
			// show loading message
			$('.loading').show();
			
			$.ajax({
				// Get URL & Method from the form element
				url: form.attr('action'),
				method: form.attr('method'),
				
				// Serialized fields
				data: form.serialize(),
				cache: false,
				
				//success
				success: function (html) {				
					//if process.php returned 1/true (send mail success)
					if (html==1) {					
						//hide the form
						//$('#contact-form fieldset').fadeOut('slow');					

						//show the success comment
						$('.thanks').fadeIn('slow');

					//if process.php returned 0/false (send mail failed)
					}
					else {
						alert('<p class="error">Sorry, unexpected error. Please try again later.</p>');
					}
				}
			});
		
			// disable fields
			form.find('input, textarea, button, select').attr('disabled', 'disabled');
		}
	});
});