jQuery(document).ready(function () { 

	$("#contactMsg").addClass('ui-state-error ui-corner-all');

	$("#submitButton").hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);

	$('#contactFullname').focus();


	$('#contactInterest').change( function() {
		var selectedOption = $(this).find('option:selected').val();
		
		if (selectedOption == 'Other') {
			$('#contactInterestOtherSlider').slideDown(600);
			$('#contactInterestOther').focus();
		}
		else
			$('#contactInterestOtherSlider').hide(200);
	});

	
	$('#contactBlock').keyup( function(event) {
		if(event.keyCode == 13 && event.srcElement.id != 'contactMessage') {
			$('#submitButton').click();
		};
	});


	/* Send Contact Function */
	$('#submitButton').click( function() {
	
		var contactFullname			= $('#contactFullname'),
			contactInterest			= $('#contactInterest').find('option:selected'),
			contactInterestOther	= $('#contactInterestOther'),
			contactEmail			= $('#contactEmail'),
			contactMessage			= $('#contactMessage'),
			contactMsg				= $('#contactMsg');

		var validFullname = (contactFullname.val().length > 2);

		if (!validFullname) {
			contactMsg.html('Please, if you wouldn\'t mind introducing yourself. &nbsp;There\'s no need to be formal.').slideDown(300, function() { contactFullname.effect("highlight", {}, 3000).focus(); });
			return false;
		}

		var validEmail = ((contactEmail.val().indexOf(".") > 2) && (contactEmail.val().indexOf("@") > 0));
			
		if (!validEmail) {
			contactMsg.html('Please add a valid Email Address so we can get back to you.').slideDown(300, function() { contactEmail.effect("highlight", {}, 3000).focus(); });
			return false;
		}

		var validInterestSelection = (contactInterest.val() != 'Please Select');

		if (!validInterestSelection) {
			contactMsg.html('Please select an Interest, so we may better assist you.').slideDown(300, function() { $('#contactInterestHeader').effect("highlight", {}, 3000); });
			return false;
		}

		if (contactInterest.val() == 'Other') {
			var validInterestOther = (contactInterestOther.val() != '');
	
			if (!validInterestOther) {
				contactMsg.html('Please tell us what you\'re interested in! &nbsp;It helps us better direct your message.').slideDown(300, function() { contactInterestOther.effect("highlight", {}, 3000).focus(); });
				return false;
			}
		}

		var validMessage = (contactMessage.val().length > 2);

		if (!validMessage) {
			contactMsg.html('Just a quick little message, please! &nbsp;We promise to read it.').slideDown(300, function() { contactMessage.effect("highlight", {}, 3000).focus(); });
			return false;
		}


		/* legit */
		contactMsg.slideUp(300);

		$('#contactFullname, #contactEmail, #contactInterest, #contactMessage').attr('disabled', 'disabled');
		$('#submitButton').attr('disabled', 'disabled');
					
		$('#contactBlock').slideUp(300);
		$('#submitButton').slideUp(300);

		$.ajax({
			type: "POST",
			url: "send-contact.php",
			data: {
				fullname : contactFullname.val(),
				interest : contactInterest.val(),
				interestOther : contactInterestOther.val(),
				email: contactEmail.val(),
				message: contactMessage.val()
			},
			success: function(response) {
			
				console.log(response);
			
				if (response == 'suxxxess') {
					contactMsg.removeClass('ui-state-error').addClass('ui-state-highlight').html('Your Message has been Sent, Thanks!').slideDown(300);

					contactMsg.click( function() {
						location.href = '/';
					});

				} else {
					contactMsg.html('Something terribly weird just happened!').slideDown(300);
				}
			}
		});

	});
});
