﻿
var forms = function() 
{
	$( 'form#contact-form' ).submit(function() 
	{
		var formObj = this;
		var msgObj = $( '#form-response' );

		var hasError = false;

		$.each( $( this ).children( '.required' ), function() 
		{
			if( $.trim( $( this ).children( ':input' ).val() ) == '' ) 
				hasError = true;
		});

		if( hasError ) 
		{
			msgObj.fadeOut( 300, function()
			{
				$( this ).addClass( 'response-fail' ).fadeIn( 300 ).html( '<strong>Oops!</strong><br />Please make sure you have filled the form fully.' );
			});
		}
		else
		{
			msgObj.html( '<strong>Sending...</strong><br />Please wait!' );

			var inputs = [];
			$( ':input', this ).each(function() 
			{
				if( this.type != 'radio' )
					inputs.push( this.name + '=' + escape( this.value ) );
			});

			if( $( 'input[type=radio]:checked' ).val() != null )
				inputs.push( $( 'input[type=radio]:checked' ).attr( 'name' ) + '=' + $( 'input[type=radio]:checked' ).val() );

			jQuery.ajax(
			{
				url: 		this.action,
				type:		this.method,
				data: 		inputs.join( '&' ),
				dataType: 	'json',
				timeout: 	2000,

				error: function()
				{
					msgObj.fadeOut( 300, function()
					{
						$( msgObj ).fadeIn( 300 ).html( '<strong>Sorry, message has <em>not</em> been sent!</strong><br />Try one more time, please.' );
					});
				},
				success: function( data ) 
				{
					msgObj.fadeOut( 300, function()
					{
						if( data.success == true )
							$( this ).addClass( 'response-success' )

						$( this ).fadeIn( 300 ).html( data.response );
					});

					if( data.success == true )
					{
						$.each( $( formObj ).children( '.item' ).children( ':input' ), function()
						{
							if( this.type != 'submit' ) 
								$( this ).val( '' );
						});
					}
				}
			});
		}

		$( 'html, body' ).animate( { scrollTop: $( formObj ).offset().top }, 300 );

		return false;
	});

};

$( window ).load( forms );


var servicesAccordion = function()
{
	var targetClass		= $( '.services-info' );
	var linkClass		= $( '.services-link' );
	var linkSelected	=  'services-link-selected';

	linkClass.click( function()
	{
		var box = $( '#' + this.id + '-box' );

		if( box.is( ':hidden' ) )
		{
			$( this ).addClass( linkSelected );
			box.slideDown( 300 );
		}
		else
		{
			$( this ).removeClass( linkSelected );
			box.slideUp( 300 );
		}

		return false;
	});
};

$( window ).load( servicesAccordion );