$(document).ready(function() {
	
	$("div.preloader").css("display", "none");
	
	var pergAtualC = $.cookie("pergAtual");
	var pergAtual = $.getURLParam("perg");
	var qtdPerg = parseInt($("#qtdPerguntas").text());
	var totalPontos = $.cookie("totalPontos");
	var resultado = false;

	/*
	 * Se não marcar a pergunta Zera
	 */
	if (pergAtual == null && qtdPerg != parseInt(pergAtual)) {
		
		pergAtual = 0;
		totalPontos = 0;
		$.cookie("totalPontos", 0);
	}
	else {
		pergAtual = parseInt(pergAtual);
		pergAtualC = parseInt(pergAtualC);
		totalPontos = parseInt(totalPontos);
		
		if(pergAtual == qtdPerg) resultado = true;
		
		if(pergAtual != pergAtualC) window.location = window.location.pathname;
	}
	
	if (!resultado) {
		/**
		 * Mostra a Pergunta e coloca as ações
		 */
		$("dl.testeLista:eq(0)").find("a.aResposta").each(function(i){
		
			var valor = $(this).attr('rel');
			valor = parseInt(valor);
			
			// URL para Proxima pergunta
			var urlPergunta =  window.location.pathname + "?perg=" + (pergAtual + 1);
			
			$(this).attr('href', urlPergunta);
			
			// Para o Click do A
			$(this).click(function(e) {
				e.preventDefault();
			});
			
			// Click no DD
			$(this).parent().parent().click(function(e) {
				//e.preventDefault();

				// Coloca o total
				$.cookie("totalPontos", totalPontos + valor);
				
				// Check se não é a ultima Pergunta
				$.cookie("pergAtual", (pergAtual + 1));
				
				window.location = urlPergunta;
			});			
		});
		
		$("dl.testeLista:eq(0)").fadeIn("def");
		
		// Contador
		$("p.contador").css("display", "block");
		$("p.contador").html((pergAtual+1)+" de "+qtdPerg);
	}
	// RESULTADO
	else {
		// Limpa
		$.cookie("pergAtual", null);
		$.cookie("totalPontos", 0);
		$("p.contador").css("display", "none");
		
		$("dd.resultado_txt").each(function (i) {
			var margem = $(this).attr('rel').split(":");
			var margemI = margem[0];
			var margemF = margem[1];
			
			// Mostra
			if(totalPontos >= margemI && totalPontos <= margemF) {
				$(this).fadeIn("def");
				$("dt.resultado").fadeIn("def");
			}
		});
	}
});

