$(document).ready(function(){
	//focando no primeiro input
	$("#form-contato input:first").focus();
	
	//Destacando campos requeridos
	$(".validate .required").parent().find("label").prepend('<span title="Campo obrigatório." class="marcador-requerido">*</span>');
	
	//Validando formulario;
	$(".validate").submit( function(){
		var erros = 0;
		$.each( $(".required"), function(){
			removeErro( $(this) );
			if ( !valida( $(this) ) ){
				erros++;
			}
		} );
		return erros > 0 ? false : true;
	} );
	
	//verifica se o campo é vazio, retorna true ou false
	function valida(elemento){
		var valido = true;
		var msgErro = 'Campo de preenchimento obrigatório.';
		
		if ( elemento.val() === "" ){
			valido = false;
		}
		else {
			if( elemento.hasClass("email") ){
				var textEmail = elemento.val();
				if ( textEmail.search(/(\w[\w\.\+]+)@(.+)\.(\w+)$/) < 0 ){
					valido = false;
					msgErro = 'E-mail inválido.';
				}
			}
		}
		if ( !valido ){
			adicionaErro(elemento, msgErro);
		}
		
		return valido;
	}
	//adiciona mensagens de erro ao elemento, parametro { elemento, mensagem }
	function adicionaErro(elemento, msg){
		elemento.addClass('error');
		elemento.wrap('<div class="erro-wrapper"></div>');
		elemento.parent().append('<span class="info-error">'+msg+'</span>');
	}
	
	//remove as mensagens de erro de um elemento
	function removeErro(elemento){
		elemento.removeClass('error');
		elemento.parent().find("span.info-error").remove();
	}
	
	//quando dar foco, deixar a mensagem transparente
	$(".required").focus(function(){
		$(this).parent().find(".info-error").animate({ opacity: "0.2"} );
	});
	$(".required").blur(function(){
		$(this).parent().find(".info-error").animate({ opacity: "1"} );
		if ( $(this).val() !== "" ){
			removeError( $(this) );
		}
	});
	//quando clicar na mensagem de erro, dar foco no input
	$(".info-error").live("click",function(){
		$(this).parent().find(".required").focus();
	});
	//validar sempre que mudar o valor
	$(".required").change(function(){
		removeErro( $(this) );
		valida( $(this) );
	});
	
	//ao preencher o campo captcha verificar se o valor digitado esta correto
	/*$("#recaptcha_response_field").change(function(){
		$.post("http://api-verify.recaptcha.net/verify",
		{
			privatekey:	'6LebywoAAAAAAO_Vb0jq9oygHX-tGyBsE9vJkQoj',
			remoteip:	$("#ip").val(),
			challenge:	$("[name=recaptcha_challenge_field]").val(),
			response:	$("#recaptcha_response_field").val()
		},
		function(){
			console.log();
		}, "json");
	});*/
});