/**
 * The javascript that drives the contact form
 */

$( document ).ready( function()
{
	// Plug into the contact link click event
	$( '#contact_link' ).click( function()
	{
		$( '#contact_form' ).fadeIn();
	} );
	
	// Plug into the send button's click event
	$( '#contact_submit' ).click( function()
	{
		// The place to store the data for the ajax request
		var ajaxData = {};

		// Make sure the user filled out all the fields
		var bValid = true;
		var errorMessage = '';
		
		$( '#contact_form #contact_name, #contact_form #contact_email, #contact_form #contact_message' ).each( function()
		{
			if( ! bValid ) return;

			switch( this.id )
			{
				case 'contact_name':
					if( this.value.length == 0 )
					{
						errorMessage = 'You must enter your name';
						bValid = false;
					}
					else
						ajaxData.name = this.value;
					break;

				case 'contact_email':
					if( this.value.length == 0 || this.value.indexOf( '@' ) == -1 )
					{
						errorMessage = 'You must enter your email address';
						bValid = false;
					}
					else
						ajaxData.email = this.value;
					break;

				case 'contact_message':
					if( this.value.length == 0 )
					{
						errorMessage = 'You must enter a message';
						bValid = false;
					}
					else
						ajaxData.message = this.value;
					break;
			}// End switch id
		} );
		
		if( bValid )
		{
			// Ajax the data to the server to actually send the request
			$.post( '/contact/send', ajaxData, function( d )
			{
				if( d.status && d.status.length > 0 )
				{
					if( d.status == 'success' )
					{
						// Let the user know their message was send, and hide the contact form
						showStatus( 'Sent' );

						setTimeout( function()
						{
							// Hide the contact form and clear out all the fields
							$( '#contact_form' ).fadeOut().find( 'input, textarea' ).each( function()
							{
								this.value = '';
							} );
							
							clearStatus();
						}, 1000 );
					}
					else
					{
						// Show the error messages to the user
						if( d.errors && d.errors.length > 0 )
						{
							// Combine the error message into a single string and show it to the user
							var errorMessage = '';
							for( var e in d.errors )
								errorMessage += d.errors[e] + '  ';
							
							showError( errorMessage );
						}
					}
				}
			}, 'json' );

			showStatus( 'Sending...' );
		}
		else
		{
			showError( errorMessage );
		}
	} );
	
	// Plug into the cancel button click eventz
	$( '#contact_cancel' ).click( function()
	{
		// Hide the contact form and clear out all the fields
		$( '#contact_form' ).hide().find( 'input, textarea' ).each( function()
		{
			this.value = '';
		} );
		
		// Clear out any error messages that exist
		var errorContainer = $( '#contact_form #info' )[0];
		while( errorContainer.firstChild )
			errorContainer.removeChild( errorContainer.firstChild );
	} );
	
	// Clears out the status display
	function clearStatus()
	{
		$( '#contact_form #info' ).each( function()
		{
			while( this.firstChild )
				this.removeChild( this.firstChild );
		} );
	}// End function clearStatus
	
	// A function that will show a status message on the contact form
	function showStatus( message )
	{
		// Get the info div and clear it out, and add the new message
		$( '#contact_form #info' ).each( function()
		{
			while( this.firstChild )
				this.removeChild( this.firstChild );
		} ).html( message ).removeClass().addClass( 'info' );
	}// End function showStatus
	
	
	// A function that will show an error message on the contact form
	function showError( message )
	{
		// Get the info div and clear it out, and add the error
		$( '#contact_form #info' ).each( function()
		{
			while( this.firstChild )
				this.removeChild( this.firstChild );
		} ).html( message ).removeClass().addClass( 'error' );
	}// End function showError
} );
