﻿/*
	Author: Osvaldas Valutis, www.osvaldas.info - freelancer web / logo / identity / print designer and web developer
	Website: usabilityhawk.com
	Date: October, 2009
*/



/* Accordion */

var accordion = function()
{
	var question			= $( 'a.accordion-question' );
	var answerClass			= 'div.accordion-answer';
	var selectedClass		= 'selected'


	question.click( function()
	{
		$( answerClass, $( this ).parent().get( 0 ) ).slideToggle( 300, 'easeInOutExpo' );

		$( this ).toggleClass( selectedClass );


		return false;
	});
};

$( window ).load( accordion );



/* slideshow */

var slideShow = function()
{
	var navItem				= $( 'ul#slideshow-nav li a' );
	var slideItem			= 'ul#slideshow-slides li';
	var selectedClass		= 'selected';



	navItem.click( function()
	{
		index = navItem.index( this );

		$( slideItem ).each( function()
		{
			if( !$( this ).is( ':hidden' ) )
				$( this ).fadeOut( 300, function()
				{
					$( slideItem + ':eq(' + index + ')' ).fadeIn( 300 );
				});
		});

		$( navItem ).removeClass( selectedClass );
		$( this ).addClass( selectedClass );


		return false;
	});
};

$( window ).load( slideShow );



/* data submission forms */

var forms = function()
{
	$( 'form.ajax' ).submit( function()
	{
		var thisForm = 		this;
		var responseObj =	$( 'div.response', this );
		var inputs = 		[];


		responseObj.html( '<p><strong>Please wait...</strong></p>' ).fadeIn( 500 ).fadeOut( 500 ).fadeIn( 500 );


		$( 'input[type!=radio], textarea, select', this ).each( function() 
		{
			inputs.push( this.name + '=' + escape( this.value ) );
		});

		$( 'input[type=radio]:checked', this ).each( function()
		{
			if( this.value != null )
				inputs.push( this.name + '=' + this.value );
		});

		$( 'html, body' ).animate( {scrollTop: responseObj.offset().top - 80 }, 500, 'easeInOutExpo' );


		jQuery.ajax(
		{
			url: 			this.action,
			type:			this.method,
			data: 			inputs.join( '&' ),
			dataType:		'json',
			timeout: 		10000,

			error: function( request, status, thrown ) 
			{
				responseObj.fadeOut( 150, function()
				{
					responseObj.fadeIn( 300 ).html( '<p class="fail">Sorry - error (' + status + '). Try again one more time!</p>' );
				});
			},

			success: function( data ) 
			{
				if( data.cleanForm == true )
				{
					$( 'input, select', thisForm ).each( function()
					{
						if( this.type != 'submit' && this.type != 'radio' && this.type && 'select' )
							$( this ).val( '' );

						if( this.type == 'radio' )
							$( this ).attr( 'checked', false );

						if( this.type == 'select' )
							$( this ).attr( 'selected', false );
					});
				}

				if( data.hideForm == true )
					$( 'div.item', thisForm ).fadeOut( 1000, 'easeInOutExpo' );

				responseObj.fadeOut( 150, function()
				{
					responseObj.fadeIn( 300 ).html( data.response );
				});

				if( data.redirectTo != false )
					window.location = data.redirectTo;
			}
		});

		return false;
	});
};

$( window ).load( forms );

