// Basic function to show different status messages when sending messages via contact form
function formMessage(css_class, message) {
	$('#form-message')
		.html('<span class="' + css_class + '">' + message + '</span>')
		.children().fadeOut(10000, function() {
			$(this).remove();
		});
}

$(document).ready(function()
{
	$('a.new-window').attr('target', '_blank');

	// Scroll and highlight #contact as required
	$('a[href=#contact]').click(function(e){
		/**
		 * Need a variable to log whether the h1 animation has been completed.
		 * Supporting all browsers requires using both html and body as scroll 
		 * targets, so without marking animation as complete, the animation 
		 * runs twice (once for html, once for body)
		 */
		var done = 0;
		$('html, body').animate({
			scrollTop: $('#contact').offset().top
			}, 2000, function() {
				// now animate the text size to draw attention
				if (done == 0) {
					var originalFontSize = $('#contact h1').css('fontSize');
					$('#contact h1')
						.animate({fontSize : '36px'}, 'slow')
						.animate({fontSize : originalFontSize}, 'slow');
						done = 1;
				}
			});
		e.preventDefault();
	});


	// Hide expandable paragraphs by default
	$('.expandable').each(function() {
		$(this).hide();
	});
	
	// Try to fix jumpy behaviour...
	/*
	$('.expandable').each(function() {
		var height = $(this).height();
		$(this).css({height : height});
		$(this).hide();
	});
	*/

	// would generally use slideToggle here but need to adjust background-position too
	$('.expand-link').toggle(
		function() {
			$(this).next().slideDown('medium');
			$(this).css({backgroundPosition : '-240px -488px'});
		},
		function() {
			$(this).css({backgroundPosition : '-490px -240px'});
			$(this).next().slideUp('medium');
		}
	);
	
	// Remove the input hints when the user clicks on the field
	$('.has-default-value').each(function() {
		$(this).focus(function() {
			if (this.value == this.defaultValue) {
				this.value = '';
				// this.select();
			}
			if (this.value != this.defaultValue) {
				this.select();
			}
		});
		$(this).blur(function() {
			if ($.trim(this.value) == '') {
				this.value = (this.defaultValue ? this.defaultValue : '');
			}
		});
	});

	$('#submit').click(function(e){
		
		var errors = 0;

		$('.has-default-value').each(function(){
			if ($.trim(this.value) == '' || this.value == this.defaultValue) {
				errors++;
			}
		});

		if (errors > 0) {
			formMessage('error', 'All fields are required.');
		}
		else {
			formMessage('notice', 'Please wait...');
			
			$.ajax({
				url: 'contact.php',
				type: 'POST',
				data: $('#contact-form').serialize(),
				cache: false,
				dataType: 'json',
				success: function(data, textStatus, XMLHttpRequest) {
					switch (data.success) {
						case false:
							formMessage('error', 'Message sending failed (please double-check the fields above).');
							break;
						case true:
							formMessage('success', 'Message sent successfully.');
							$('#contact-form')[0].reset();
							break;
						default:
							formMessage('notice', 'Ninja gorillas ate your message (an unexpected error occurred)');
					}
				}
			});
		}
		e.preventDefault();
	});
});
