/***************************
*
* main.js => Fonctions JS
*
****************************/

var live = 'http://www.domain-shaker.com/';

$(function()
{
	
	/***************************
	* Bouton TLD
	***************************/
	$('.tld_bouton').mousedown( function(e)
	{
		//	Decale a droite : OUI
		if( $(this).css( 'left' ) == '0px' )
		{
			$(this).animate({ left: 34 }, 200);
			$(this).parent().find( 'input' ).attr( 'checked', 'checked' );
		}
		//	Decale a gauche : NON
		else
		{
			$(this).animate({ left: 0 }, 200);
			$(this).parent().find( 'input' ).removeAttr( 'checked' );
		}
	});
	
	
	/***************************
	* Au clic sur le bouton
	***************************/
	$('#shake_it').click( function(e)
	{
		e.preventDefault();
		goShake();
	});
	
	
	/***************************
	* A l'envoi du formulaire
	***************************/
	$('form').submit( function(e)
	{
		e.preventDefault();
		goShake();
	});
	
	
	/***************************
	* Shake ton Boul !!
	***************************/
	function goShake()
	{
		//	Recuperation variables
		var mot1 = $('input[name=mot_1]').val();
		var mot2 = $('input[name=mot_2]').val();
		var mot3 = $('input[name=mot_3]').val();
		var tlds = '';
		var error = '';
		if( $('input[name=tld_com]').attr( 'checked' )) tlds += 'com,';
		if( $('input[name=tld_fr]').attr( 'checked' )) tlds += 'fr,';
		if( $('input[name=tld_net]').attr( 'checked' )) tlds += 'net,';
		if( $('input[name=tld_org]').attr( 'checked' )) tlds += 'org,';
		if( $('input[name=tld_eu]').attr( 'checked' )) tlds += 'eu,';
		//	Verification mots
		if( mot1 == '' && mot2 == '' && mot3 == '' )
			error += "Vous devez saisir au moins un mot cle\n";
		//	Verification tlds
		if( tlds == '' )
			error += "Vous devez choisir au moins une extension";
		//	Slide & Action
		if( error == '' )
		{
			$('#shake_bouton').fadeOut( 100, function()
			{
				$('#shake_loader').fadeIn( 100, function()
				{
					$('#resultats_global').slideUp( "fast" );

					//	Envoi Ajax
					$.ajax({
						type: 'POST',
						url: live + 'inc/ajax/ajax.shake.php',
						async: true,
						data: 'mot_1='+mot1+'&mot_2='+mot2+'&mot_3='+mot3+'&tlds='+tlds,
						success: function(response)
						{
							//	Remplissage Html
							$('#resultats_inte').html( response );
							$('#resultats_global').slideDown( "slow" );
							$('#shake_loader').fadeOut( 100 );
							$('#shake_bouton').fadeIn( 100 );
						},
						error: function(XMLHttpRequest, textStatus, errorThrown) {
							alert("TECHNICAL ERROR: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
						}
					});
				});
			});
			
		}
		else
			alert( error );
	}
});
