/*
 * @author Fábio Maciel <fabiojanga@gmail.com>
 * @date: 14/01/2011
 */

function trim(str){
	while(str.indexOf(" ") != -1){
	  str = str.replace(" ","");
	}
    return str;
}

function caixa_alta(palavra){
	document.getElementById('palavra').value = palavra.value.toUpperCase();
}

function executaEvento(nomeDiv, arquivo, mensagem) {
    enviaRequisicao(nomeDiv, arquivo, mensagem);
}

function getKey(event) {
    return event?(event.keyCode?event.keyCode:(event.which?event.which:event.charCode)):null;
}

function buscarEnter(obj) {
    if( getKey(obj) == 13){      	
    	buscar();    	
    }   
}

function submeterComentario(caminho) {
    var youtube_id = document.getElementById('youtube_id');
    var nome = document.getElementById('nome');
    var email = document.getElementById('email');
    var coment = document.getElementById('coment');

    if(trim(nome.value) == ""){
        alert('O campo nome é obrigatório.');
        nome.focus();
        return;
    }

    if(trim(email.value) == ""){
        alert('O campo e-mail é obrigatório.');
        email.focus();
        return;
    }

    if(trim(coment.value) == ""){
        alert('O campo comentário é obrigatório.');
        coment.focus();
        return;
    }

    var url = 'componente/comentario/listar.html.php?youtube_id='+youtube_id.value+'&nome='+nome.value+'&email='+email.value+'&coment='+coment.value;
    enviaRequisicao('comentarios',caminho+url,'Carregando...');
}

function votarGostei(caminho, video_id){	
    var url = 'componente/gostar/listar.html.php?video_id='+video_id+'&gostei=1';
    enviaRequisicao('gostar', caminho+url, 'Carregando...');
}

function votarNaoGostei(caminho, video_id){
    var url = 'componente/gostar/listar.html.php?video_id='+video_id+'&nao_gostei=1';
    enviaRequisicao('gostar', caminho+url, 'Carregando...');
}

//var tamanho = "11px";
function aumentarLetra(i){
	tamanho = document.getElementById("tamanho_"+i).value;
	switch (tamanho) {
		case "17px":
			tamanho = "17px";
			break;
		case "15px":
			tamanho = "17px";
			break;
		case "13px":
			tamanho = "15px";
			break;
		case "11px":				
			tamanho = "13px";
			break
		case "9px":
			tamanho = "11px";
			break
	}
	elemento = document.getElementById("letra_"+i);
	document.getElementById("tamanho_"+i).value = tamanho;
	elemento.style.fontSize = tamanho;
}

// diminuir o tamanho da letra
function diminuirLetra(i){
	tamanho = document.getElementById("tamanho_"+i).value;
	switch (tamanho) {
		case "9px":
			tamanho = "9px";
			break;
		case "11px":				
			tamanho = "9px";
			break;
		case "13px":
			tamanho = "11px";
			break;
		case "15px":
			tamanho = "13px";
			break;
		case "17px":
			tamanho = "15px";
			break;
	}
	elemento = document.getElementById("letra_"+i);
	document.getElementById("tamanho_"+i).value = tamanho;
	elemento.style.fontSize = tamanho;
}

function abreComentario(noticia_id){
	var url = document.getElementById('url').value;
	location.href = url+"blog/"+noticia_id;
	//enviaRequisicao('colLeft',url+'componente/blog/listar.html.php?noticia_id='+noticia_id+'&comentario=true','Carregando...');
}

function setarCamposComentarios(){
	var campos = "nome="+encodeURI(document.getElementById('nomeComentario').value);
	campos += "&email="+encodeURI(document.getElementById('emailComentario').value);
	campos +="&comentario="+encodeURI(document.getElementById('comentarioTexto').value);
	//campos +="&palavra="+document.getElementById('palavra').value;
	campos +="&noticia_id="+document.getElementById('noticia').value;
	return campos;
}

function submeterComentarioPost(){
	var nome = document.getElementById('nomeComentario');
	var email = document.getElementById('emailComentario');
	var comentario = document.getElementById('comentarioTexto');
	var form = document.getElementById('comments');
	
	if(trim(nome.value) == ""){	
		alert("Informe o nome");
		nome.focus();
		return;
	}
	if(trim(email.value) == ""){
		alert("Informe o email");
		email.focus();
		return;	
	}else{	
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value))){ 
			alert("Informe o email");
			email.focus(); 
	   	    return;
	    }
	} 	
	if(trim(comentario.value) == ""){
		alert("Informe o comentário");
		comentario.focus();
		return;
	}

	form.submit();
}


function submeterComentarioBlog(){
	var nome = document.getElementById('nomeComentario');
	var email = document.getElementById('emailComentario');
	var comentario = document.getElementById('comentarioTexto');
	//var palavra = document.getElementById('palavra');
	var url = document.getElementById('url').value;
	
	if(trim(nome.value) == ""){	
		alert("Informe o nome");
		nome.focus();
		return;
	}
	if(trim(email.value) == ""){
		alert("Informe o email");
		email.focus();
		return;	
	}else{	
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value))){ 
			alert("Informe o email");
			email.focus(); 
	   	    return;
	    }
	} 	
	if(trim(comentario.value) == ""){
		alert("Informe o comentário");
		comentario.focus();
		return;
	}
	/*
	if(trim(palavra.value) == ""){	
		alert("Palavra incorreta");
		palavra.focus();
		return;
	}	
	*/
	campos = setarCamposComentarios();	
	enviaRequisicao('boxComentarios', url+'componente/blog/formComentario.html.php?'+campos,'Carregando...');
}

function abreEnviarAmigo(indice, id, maior){
	valorAtual = id;
	for (i=0;i<=maior;i++){
		if (parseInt(i)!=parseInt(indice)){
			if (eval("document.getElementById('divEnviarParaAmigoStatus"+i+"')")!=null){
				eval("document.getElementById('divEnviarParaAmigoStatus"+i+"').value=0");
				eval("document.getElementById('divEnviarParaAmigo"+i+"').innerHTML = ''");
			}
		}
	}

	var url = document.getElementById('url').value;
	status = eval("document.getElementById('divEnviarParaAmigoStatus"+indice+"').value");
	
	if (parseInt(status)==0){
		eval("document.getElementById('divEnviarParaAmigoStatus"+indice+"').value=1");
		caminho = url+'componente/blog/formIndicacao.html.php?indice='+indice+'&noticia_id='+id;
		enviaRequisicao('divEnviarParaAmigo'+indice,caminho,'Carregando...');
	}else{
		eval("document.getElementById('divEnviarParaAmigoStatus"+indice+"').value=0");
		document.getElementById('divEnviarParaAmigo'+indice).innerHTML = '';
	}
}

function setarEnviarEmailAmigo(){
	var campos = "nomeRemetente="+encodeURI(document.getElementById('nomeRemetente').value);
	campos += "&emailRemetente="+encodeURI(document.getElementById('emailRemetente').value);
	campos += "&nomeDestinatario="+encodeURI(document.getElementById('nomeDestinatario').value);
	campos += "&emailDestinatario="+encodeURI(document.getElementById('emailDestinatario').value);
	campos += "&palavra="+document.getElementById('palavra').value;
	return campos;
}

function submeterEnviarEmailAmigo(indice, noticia_id){
	var url = document.getElementById('url').value;
	var nome = document.getElementById('nomeRemetente');
	var email = document.getElementById('emailRemetente');
	var nomeDest = document.getElementById('nomeDestinatario');
	var emailDest = document.getElementById('emailDestinatario');
	var palavra = document.getElementById('palavra');
	
	if(trim(nome.value) == ""){	
		alert("Informe o nome do remetente.");
		nome.focus();
		return;
	}
	
	if(trim(email.value) == ""){
		alert("Informe o e-mail do remetente");
		email.focus();
		return;	
	}else{	
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value))){ 
	       	alert("Email incorreto.");
	        email.focus(); 
	   	    return;
	    }
	}
	if(trim(nomeDest.value) == ""){	
		alert("Informe o nome do destinatário.");
		nomeDest.focus();
		return;
	}
	if(emailDest.value == ""){
		alert("Informe o e-mail do destinatário");
		emailDest.focus();
		return;	
	}else{	
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailDest.value))){ 
	       	alert("Email incorreto.");
	       	emailDest.focus(); 
	   	    return;
	    }
	} 	
	
	if(trim(palavra.value) == ""){	
		alert("Palavra inválida.");
		document.getElementById('palavra').focus();
		return;
	}	
	campos = setarEnviarEmailAmigo();
	campos = campos+'&noticia_id='+noticia_id+'&indice='+indice;
	div = 'divEnviarParaAmigo'+indice;	
	enviaRequisicao(div, url+'componente/blog/formIndicacao.html.php?'+campos, 'Carregando...'); 
}

function setClassFooter() {
	document.getElementById('footer').className = "footer";
}
function buscar(){

	var url = document.getElementById('url').value;	
	var termo = document.getElementById('inputBusca').value;
	var secao = document.getElementById('secaoBusca').value;
	setClassFooter();
		
	if(secao == 'blog') {
		caminho = url+"componente/blog/resultadoBusca.html.php?termo="+encodeURI(termo);
	}else {
		caminho = url+"secao/busca/busca.php?termo="+encodeURI(termo);
	}	
	var mensagem = "<div style='margin-left:370px;padding-top:200px;'><img src='images/icons/loading.gif' style='margin-left:20px'/><br/>Carregando...</div>";
	enviaRequisicao("content", caminho, mensagem);
	return false;
}

function buscarQuadro(termo){
	
	var url = document.getElementById('url').value;	
	//var termo = document.getElementById('inputBusca');
	var secao = document.getElementById('secaoBusca').value;
	setClassFooter();	
	
	if(secao == 'blog') {
		caminho = url+"componente/blog/resultadoBusca.html.php?termo="+encodeURI(termo);
	}else {
		caminho = url+"secao/busca/busca.php?termo="+encodeURI(termo);
	}		
	enviaRequisicao("content", caminho, "Carregando...");
}
function buscarKeywords(termo){
	var url = document.getElementById('url').value;	
	setClassFooter();
	var mensagem = "<div style='margin-left:370px;padding-top:200px;'><img src='images/icons/loading.gif' style='margin-left:20px'/><br/>Carregando...</div>";
	caminho = url+"secao/busca/busca.php?termo="+encodeURI(termo);
	enviaRequisicao("content", caminho, mensagem);
}

function filtrarOrdem(ordenar) {
	var url = document.getElementById('url').value;
	setClassFooter();
	
	caminho = url+"secao/busca/busca.php?ordenar="+ordenar;	
	enviaRequisicao("content", caminho, "Carregando...");
}
function filtrarQuadro(quadro) {
	var url = document.getElementById('url').value;
	setClassFooter();
	
	caminho = url+"secao/busca/busca.php?quadro="+quadro;
	enviaRequisicao("content", caminho, "Carregando...");
}
function filtrar(filtrar) {
	var url = document.getElementById('url').value;
	setClassFooter();
	
	caminho = url+"secao/busca/busca.php?filtrar="+filtrar;
	enviaRequisicao("content", caminho, "Carregando...");
}

function filtrarTodosQuadro(quadro, termo) {
	var url = document.getElementById('url').value;
	setClassFooter();
	
	caminho = url+"secao/busca/busca.php?termo="+termo+"&quadro="+quadro+"&ordenar=exibicao";
	var mensagem = "<div style='margin-left:370px;padding-top:200px;'>Carregando...</div>"
	enviaRequisicao("content", caminho, mensagem);
}

function filtrarMaisExibidos() {
	var url = document.getElementById('url').value;
	setClassFooter();
	
	caminho = url+"secao/busca/busca.php?termo=todos&ordenar=exibicao";
	enviaRequisicao("content", caminho, "Carregando...");
}


function upload(){
	var nome = document.getElementById('nome');
	var email = document.getElementById('email');
	var endereco = document.getElementById('endereco');
	var msg = document.getElementById('msg');
	
	if(trim(nome.value) == ""){	
		alert("Informe o seu Nome.");
		nome.focus();
		return;
	}
	
	if(trim(email.value) == ""){	
		alert("Informe o seu E-mail.");
		email.focus();
		return;
	} else {
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value))){
			alert("Formato de email incorreto.");
			email.focus();
	   	    return;
	    }
	}
	
	if(trim(endereco.value) == ""){
		alert("Informe o seu Endereço.");
		endereco.focus();
		return;
	}
	if(trim(msg.value) == ""){
		alert("Informe a Mensagem.");
		msg.focus();
		return;
	}
	$('#fileUpload').fileUploadStart();	
}

function abrePagina(link) {
	window.open(link);
}

function submeterContato(){
	var url = document.getElementById('url').value;
	var nome = document.getElementById('nome');
	var email = document.getElementById('email');
	var msg = document.getElementById('msg');
	
	if(trim(nome.value) == ""){	
		alert("Informe o nome.");
		nome.focus();
		return;
	}
	if(email.value == ""){
		alert("Informe o e-mail.");
		email.focus();
		return;	
	}else{	
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value))){ 
			alert("Formato de email incorreto.");
			email.focus(); 
	   	    return;
	    }
	} 	
	if(trim(msg.value) == ""){	
		alert("Informe a mensagem.");
		msg.focus();
		return;
	}	
	var campos = setarCamposContato();
	var mensagem = "<div style='margin-left:370px;padding-top:200px;'><img src='images/icons/loading.gif' style='margin-left:20px'/><br/>Carregando...</div>";
	enviaRequisicao('content', url+'secao/faleConosco/faleConosco.php?'+campos,mensagem);
}
function setarCamposContato(){
	var nome = document.getElementById('nome');
	var email = document.getElementById('email');
	var msg = document.getElementById('msg');
	
	campos = "nome="+encodeURI(nome.value);
	campos += "&email="+encodeURI(email.value);
	campos +="&msg="+encodeURI(msg.value);
	return campos;
}

function abreFotoDestaque(secaoId, fotoId) {
	var url = document.getElementById('url').value;
	
	enviaRequisicao('fotoDestaque', url+'componente/fotos/listarDestaque.html.php?secaoId='+secaoId+'&fotoId='+fotoId, 'Carregando...');
}

function abreFoto(secaoId) {
	var url = document.getElementById('url').value;
	
	enviaRequisicao('content', url+'secao/fotos/fotos.php?secaoId='+secaoId, 'Carregando...');
}

function abreFotoDestaqueBlog(secaoId, fotoId, indice) {
	var url = document.getElementById('url').value;
	
	enviaRequisicao('fotoDestaque'+indice, url+'componente/fotos/listarDestaque.html.php?secaoId='+secaoId+'&fotoId='+fotoId, 'Carregando...');
}

function buscarTags(termo){
	var url = document.getElementById('url').value;	
	caminho = url+"componente/blog/resultadoBusca.html.php?termo="+encodeURI(termo);	
	var mensagem = "<div style='margin-left:370px;padding-top:200px;'><img src='images/icons/loading.gif' style='margin-left:20px'/><br/>Carregando...</div>";
	enviaRequisicao("content", caminho, mensagem);
}

function filtrarTodosDaGalera() {
	var url = document.getElementById('url').value;
	
	caminho = url+"secao/busca/busca.php?termo=todos&ordenar=exibicao&galera=1";
	enviaRequisicao("content", caminho, "Carregando...");
}

