
var idElem = new Array();
var altura = new Array();
var alturaTitulo = new Array();
var flag = new Array();

$(function(){

	var nova_botao = "#criar_nova";
	var nova_compr = $(nova_botao).width();
	$(nova_botao).width(94);
    $(nova_botao + " a").mouseover(function(){
	   $(nova_botao).animate({width:nova_compr},400);
    });
	$(nova_botao + " a").mouseout(function(){
	   $(nova_botao).animate({width:94},400);
    });


	altura[0] = $(".texto_discussao").height()+12;
	alturaTitulo[0] = $(".titulo_discussao").height();
	alturaConteudoEsquerdo = $("#conteudo_esquerdo").height();
	alturaConteudoCentral = $("#conteudo_central").height();
	alturaDiscussao = $("#discussao").height();
	flag[0] = false;
	var idStr = "";
		
	$(".resposta").each(function (c) {
		idStr = $(this).attr("id");
		idElem[c+1] = idStr.substr(5,idStr.length-5); 	
		altura[c+1] = $(this).height();
		flag[c+1] = false;		
		$(this).height(0);
	});
		
	var textoTamanho =  $(".texto_discussao > .comu_texto").height();

	if(textoTamanho<60){$(".texto_discussao").height(textoTamanho)}
	else{
	  $(".texto_discussao").height(60);
	  $("#expandir_fundo_discussao").show();
	  $("#expandir_discussao").show();

	  $(".texto_discussao").click(function(){
	
		if($(".texto_discussao").height()<altura[0]){
			$(".texto_discussao").animate({height:altura[0]},400,function(){
				flag[0] = true;
				$("#expandir_discussao").html("CLIQUE PARA REDUZIR&nbsp; &#9650;");
				$("#expandir_fundo_discussao").hide();
				baixaConteudo(altura[0]);
			});
		}	
		else{
			 $(".texto_discussao").animate({height:36},400,function(){
			 		flag[0] = false;
					$("#expandir_discussao").html("CLIQUE PARA EXPANDIR&nbsp; &#9660;");
					$("#expandir_fundo_discussao").show();
					sobeConteudo(altura[0]);
			  });
		}	
	  });
	
	}
	
	
});	
	
function identifica(ident){
	var id = 0;
	for(var c=1;c<=idElem.length;c++){
		if( idElem[c]==ident ){
			id = c;
			break;		
		}
	}

	return id;
}	
	
function anime(ident){
	var casaStr = "#casa" + ident;
	var idStr = "#texto" + ident;
	var titStr = "#titulo" + ident;
	var linhaStr = "#linha" + ident;
	var embaixo = false;
	var id = identifica(ident);
	var alturaElem = altura[id];
	embaixo = flag[id];	
	
	if($(idStr).height()==alturaElem&&!embaixo){
		embaixo = true;
		baixaConteudo(alturaElem);
	}
	else if($(idStr).height()<alturaElem){
		$(idStr).animate({height:alturaElem},400,function(){
			$(idStr).click(function(){
				$(idStr).animate({height:0},400);
				sobeConteudo(alturaElem);
				flag[id] = embaixo = false;
			});
		});		
		baixaConteudo(alturaElem);
		embaixo = true;
	}	
	else{
		$(idStr).animate({height:0},400);
		sobeConteudo(alturaElem);
		embaixo = false;
	}	
	
	if(id>0){flag[id] = embaixo}

}	
	
function baixaConteudo(tam){
		$("#pagina").height($("#pagina").height()+tam);
		$("#conteudo_esquerdo").height($("#conteudo_esquerdo").height()+tam);
		$("#conteudo_forum").height($("#conteudo_forum").height()+tam);
		$("#discussao").height($("#discussao").height()+tam);
		$("#conteudo_direito_forum").height($("#conteudo_direito_forum").height()+tam);	
}
	
	
function sobeConteudo(tam){
		$("#pagina").height($("#pagina").height()-tam);
		$("#conteudo_esquerdo").height($("#conteudo_esquerdo").height()-tam);
		$("#conteudo_forum").height($("#conteudo_forum").height()-tam);
		$("#discussao").height($("#discussao").height()-tam);
		$("#conteudo_direito_forum").height($("#conteudo_direito_forum").height()-tam);	
}
	
function baixaPouco(ident){

	var alturaElem = 0;
	var embaixo = true;
	var id = identifica(ident);
	alturaElem = altura[id];
	embaixo = flag[id];
	
	if(!embaixo){
		var idStr = "#texto" + ident;
		var hfim = 46;
		if(alturaElem<hfim){hfim=alturaElem}
		$(idStr).animate({height:hfim},100);
	}
}

function sobe(ident){
	var alturaElem = 0;
	var embaixo = true;
	var id = identifica(ident);
	alturaElem = altura[id];
	embaixo = flag[id];
	if(!embaixo){
		var idStr = "#texto" + ident;
		$(idStr).animate({height:0},100);
	}
}	

function responder(id){

	var userID = $("#dados_usuario input" ).val();
	
	if(userID>0){

		$("#frmResp" + id )[0].user.value =  userID;
		$("#frmResp" + id )[0].content.value =  "";
		var titulo = "";
	
		if(id==0){	
			titulo = "RE: " + $(".titulo_discussao_texto").html();
		}
		else{
			titulo = "RE: " + $("#titulo" + id + " .titulo_resposta_texto").text();
		}
		if(titulo.length>100){titulo=titulo.substr(0,100)}
		$("#frmResp" + id )[0].titel.value = titulo;

		if(!flag[identifica(id)]){anime(id)}
	
		baixaConteudo(200);
		
		var comp = 600;
		if(ie6<7){comp=300;}
		else{
			if(id>0){ comp = $("#texto" + id ).width()-40;}
		}
		$("#den" + id).hide(400,function(){
			$("#resp" + id).animate({height:200},400).animate({width:comp},400,function(){
				$("#resp" + id + " .form_resposta").fadeIn(400);
				$("#resp" + id + " .botao_fecha").fadeIn(400);		
				if(!ie6){$("#frmResp" + id + " > textarea").width(comp-30).focus();}
			});
			var altur = $("#linha" + id).height()+176;
			$("#linha" + id).animate({height:altur},400);
		});
		$("#resp" + id + " > .botao_resposta_texto").fadeOut(400);
		
	}
	else{

		$("#resp" + id + " > .botao_resposta_texto").fadeOut(400);
		
		baixaConteudo(200);

		var altur = $("#linha" + id).height()+76;
		$("#linha" + id).animate({height:altur},100);
		$("#resp" + id).animate({height:100},400).animate({width:304},400,function(){
			$("#resp" + id + " .enviando").html('identifique-se primeiro:<br />Fa&ccedil;a o login ou <a href="http://www.surfguru.com.br/cadastro/novo.asp">cadastre-se</a>.').fadeIn(400);
			$("#resp" + id + " .botao_fecha").fadeIn(400);		
		});	
		
	}
}


function denunciar(id){
	
		eval("document.frmDen" + id + ".nome").value =  "";
		eval("document.frmDen" + id + ".email").value =  "";
		eval("document.frmDen" + id + ".content").value =  "";

		baixaConteudo(200);
			
		$("#resp" + id).hide(400,function(){
		    var altur = $("#linha" + id).height()+366;
		  	$("#linha" + id).animate({height:altur},400);

			$("#den" + id).animate({height:390},400).animate({width:304},400,function(){
				$("#den" + id + " .form_resposta").fadeIn(400);
				$("#den" + id + " .botao_fecha").fadeIn(400);		
			});
		});
		$("#den" + id + " > .botao_resposta_texto").fadeOut(400);
		
}


function fechaFormDen(id){

	sobeConteudo(200);

	$("#den" + id +' .botao_fecha').fadeOut(400);
	$("#den" + id +' .enviando').hide();

	$("#den" + id + " .form_resposta").fadeOut(400,function(){
		$("#linha" + id).animate({height:30},400);
		$("#den" + id).animate({height:22},400).animate({width:85},400,function(){
			$("#den" + id + " .botao_resposta_texto").fadeIn(400,function(){
				$("#resp" + id).show(400);
			});
		});
	});
	$("#texto" + id + " .resposta_texto").focus();

}


function submeteDen(id){

		 var idForm = "#frmDen" + id;
		 var idResp =  "#den" + id + "  .enviando";
		 var idBotao =  "#den" + id + " .botao_fecha";
		 var erro = false;
		 
 		 var nome = eval("document.frmDen" + id + ".nome").value;
		 var email = eval("document.frmDen" + id + ".email").value;
		 var texto = eval("document.frmDen" + id + ".content").value;
		 var frm = eval("document.frmDen" + id + ".natureza");
		 var natur = "";

		 if(nome.length<3){alert("Digite o seu nome");erro=true;}
		 
		 if(!erro){if(email.length<2){alert("Digite o seu email");erro=true;}}
		 
		 if(!erro){if(!valida(email)){alert("Digite corretamente o seu email");erro=true;}}

		 for (var i=0; i < frm.length; i++){
   			if (frm[i].checked){ natur = frm[i].value; break; }
		 }

		if(!erro){if(natur.length<2){ alert("Especifique a natureza de sua denuncia."); erro=true; } }
		if(!erro){if(texto.length<2&&natur=="outro"){ alert("Especifique a natureza de sua denuncia em observações."); erro=true; } }
		if(!erro){if(texto.length>4000){ alert("O texto em observaçoões é muito grande."); erro=true; } }
		 
		if(!erro){
		 
		 	$(idBotao).fadeOut(400);
			$("#den" + id).animate({height:100},400);
			$("#linha" + id).animate({height:108},400);
				
			$(idForm).fadeOut(400,function(){
		
   	  		$(idResp).html('Enviando sua denúncia <br/> Aguarde...').fadeIn(400);
			var urlPost = "processo/processaDenuncia.asp";
							
			$.post(urlPost, $(idForm).serialize() , function(resposta){
							
				   resposta = jQuery.trim(resposta);

				   if(resposta.substr(5,2)=='ok'){
						$(idResp).html('Denúncia enviada com sucesso.');
						$(idBotao).fadeIn(200);
					}
					else{
						$(idResp).html('Desculpe: houve um problema! <br/> tente novamente depois.');
						$(idBotao).fadeIn(200);
					}
			 		});
			});			
		
		}						

}


function fechaForm(id){

	sobeConteudo(200);

	$("#resp" + id +' .botao_fecha').fadeOut(400);
	$("#resp" + id +' .enviando').hide();

	$("#resp" + id + " .form_resposta").fadeOut(400,function(){
		$("#linha" + id).animate({height:30},400);
		$("#resp" + id).animate({height:22},400).animate({width:210},400,function(){
			$("#resp" + id + " .botao_resposta_texto").fadeIn(400,function(){
				$("#den" + id).show(400);
			});
		});
	});

}

function submete(id){
		 var idForm = "#frmResp" + id;
		 var idResp =  "#resp" + id + "  .enviando";
		 var idBotao =  "#resp" + id + " .botao_fecha";
		 
		 if($(idForm + " textarea").val().length<2){alert("Digite a sua mensagem")}
		 else{
		 
		 	$(idBotao).fadeOut(400);
			$("#resp" + id).animate({height:100},400);
			$("#linha" + id).animate({height:108},400);
				
			$(idForm).fadeOut(400,function(){
		
   	  		$(idResp).html('Enviando sua mensagem <br/> Aguarde...').fadeIn(400);
			var urlPost = "processo/processaResposta.asp";
							
			$.post(urlPost, $(idForm).serialize() , function(resposta){
							
				   resposta = jQuery.trim(resposta);

				   if(resposta.substr(5,2)=='ok'){
						$(idResp).html('Mensagem enviada com sucesso.<br/>Aguardando a liberação pelo moderador.');
						$(idBotao).fadeIn(200);
					}
					else{
						$(idResp).html('Desculpe: houve um problema! <br/> tente novamente depois.');
						$(idBotao).fadeIn(200);
					}
			 		});
			});			
		
		}						

}

function nova(id){
	
	var userID = $("#dados_usuario input" ).val();
	
	if(userID>0){
		document.location.href="nova-discussao.asp?id=" + id ;
	}
	else{
		alert("Faça o login ou cadastre-se para poder participar.")
	}
	
}
