$(document).ready(function(){
	$('#bt-responder,#linkResposta').click(function() {
		if (autenticarCookiePergunta()) {
			$("#bt-responder").next().slideToggle('slow')
			.siblings('.frmResponder:visible').slideToggle('slow')
			.siblings('form').next('.frmResponder:visible').prev();
		}
	});
})

$().ready(function() {
	$("#frmResponder").validate({
		rules: {
			txtResposta: {required: true,minlength: 20	}
		},
		messages: {
			txtResposta: {required: "Coloque sua resposta.",	minlength: "Sua resposta deve ter pelo menos 20 caracteres"}
		}
							
	});
	
});

$().ready(function() {
	$("#frmdenuncia").validate({
		rules: {
			stipoDenuncia: "required",
			motivo_denuncia: {required: true,minlength: 10}
		},
		messages: {
			stipoDenuncia: "Informe o tipo da denuncia.",
			motivo_denuncia: {required: "Preencha o Motivo da Denuncia.",minlength: "Necessário ao menos 10 caracteres"}
		}
							
	});
	
});

$().ready(function() {
$("#denuncie").click(function(){
	$("#box-denunciar").slideToggle("slow");
	return false;
	});
	$(".botoes-pergunta li .fav").click(function() {
		adicionarFavorito();
	});
});

function addFavorito(){
	adicionarFavorito();
}
function escolherMelhorResposta(cod_resposta, item) {
	$.getJSON('/inc/asp/escolhe-melhor-resposta.asp', {codResposta: cod_resposta}, function(data) {
		switch (data[0]) {
			case 'sucesso':
				$(item).parent().children("span").html('Melhor resposta definida com sucesso.');
				
			break;
			case 'erro':
				$(item).parent().children("span").html('Houve um problema ao definir a melhor resposta.');
			break;
		}
	});
}


function confirmadeletaPergunta(cod_resposta_deletar){
	$("#confirmaDeleta").slideToggle("slow");
}

function deletaPergunta(cod_resposta_deletar, item ) {
	$.getJSON('/tira-duvidas/tira-duvidas.asp', {cod_resposta_deletar: cod_resposta_deletar}, function(data) {
		switch (data[0]) {
			case 'sucesso':
				//$('#'+item).html('Sua Pergunta foi Deletada com sucesso.');
				document.location.href = 'todas';
			break;
			case 'erro':
				$('#'+item).html('Houve um problema ao deletar sua Pergunta.');
			break;
		}
	});
}
function adicionarFavorito() {
	alterarFavorito($("#codPergunta").val(),'add');
}
function removerFavorito() {
	alterarFavorito($("#codPergunta").val(),'del');
}

function alterarFavorito(cod_pergunta, acao) {
	if (autenticarCookiePergunta()) {
		$.getJSON('/inc/asp/favorito-acao.asp', {codPergunta: cod_pergunta, acao: acao}, function (data) {
			//var nome = prompt("Digite seu nome:");
			var frase = "";
			switch(data[0]) {
				case 'sucesso':
					if (acao=='del') frase = "Pergunta removida com sucesso dos favoritos.";
					else   frase = "Pergunta adicionada com sucesso aos favoritos.";
					break;
				case 'ja-tem':
					if (acao=='del') frase = "Pergunta não está nos seus favoritos.";
					else  frase = "Pergunta já está nos seus favoritos.";
					break;
				case 'erro':
					frase = "Houve um erro e a pergunta não foi pros seus favoritos.";
					break;
				case 'nao-logado':
					//$(".botoes-pergunta li .fav").parent().children("span").html('Pergunta adicionada com sucesso aos favoritos.<a class="bt-close" href="javascript:void(0);" onclick="javascript:$(this).parent().css("display","block");">Fechar</a>');
					var janelaAbrir = 'http://www.baixaki.com.br/tira-duvidas/';
					if (document.location!=null) {
						janelaAbrir = document.location;
					}
					abrirJanela(janelaAbrir,1);
					break;
			}
			$(".box-fav").css("display","block");
			$(".box-fav span").html(frase+' <a class="bt-close" href="javascript:void(0);" onclick="javascript:$(\'.box-fav\').css(\'display\',\'none\');">Fechar</a>')
			setTimeout('$(".box-fav").css("display","none");',3000);
		});
	}
}
function selecionarFiltro(campo) {
	var codPergunta = $("#codPergunta").val();
	var ord = $(campo).val();
	var url = '/tira-duvidas/'+codPergunta;
	if (ord!='') url += '?ord='+ord;
	document.location.href= url;
}
function mostrarMelhorResposta(cod_resposta, cod_usuario) {
	var cod = (getCookie("nzncoduser") == null) ? "0" : getCookie("nzncoduser");
	cod = cod.split("&",1) ;
	var div_escolher = '';
	if (cod==cod_usuario) div_escolher = '<div class="botoes-dono"><a onclick="javascript:escolherMelhorResposta(\''+cod_resposta+'\', this)" href="javascript:void(0);">Escolher como melhor resposta</a><span/></div>';
	document.write(div_escolher);
}

