function vaiParaPaginaDaBusca( pagina ){
    formularioDeBusca = document.getElementById( 'form_busca' );
    formularioDeBusca.action = formularioDeBusca.action + pagina;
    formularioDeBusca.submit();
}

function modificaQuantidadeDaBusca(){
    formularioDeBusca = document.getElementById( 'form_busca' );
    formularioDeBusca.action = "./";
    formularioDeBusca.submit();
}

function mostrarDescricaoDaRegiao( siteUrl, regiao ){
    var url = siteUrl + "microrregiao/json/" + regiao;
    	
	$('#infoMapa').css( "display", "block" ) ;
	$('#infoMapaDetalhe').css( "display", "block" );	
	
	$.getJSON( url,
		function( data ){		
			$( '#areaNome' ).html( data.nome );
			$( '#areaNumero' ).html( data.numeroDaArea + ". "  );
			$( '#cidadePolo' ).html( data.cidadePolo );
			$( '#presidente' ).html( data.presidente );
			$( '#prefeitoMunicipal' ).html( data.prefeitoMunicipal );
			$( '#enderecoSede' ).html( data.enderecoSede + " - CEP: " + data.cepDaSede );
			$( '#secreatarioExecutivo' ).html( data.secretarioExecutivo );
			$( '#email' ).html( data.email );
			$( '#municipiosIntegrantes' ).html( data.municipiosIntegrantes );
    	
			$('#infoMapa').css( "display", "block" ) ;
			$('#infoMapaDetalhe').css( "display", "block" );
		}
    );
}

function mostrarBiografia( siteUrl ){
    var url = siteUrl + "html/json/biografia";
	$( '#texto_bio' ).html( "<img src=\"" + siteUrl + "img/ajax-loader.gif\"/>" );
	$.getJSON( url,
		function( data ){
			$( '#texto_bio' ).html( data.texto );
        }
    );
}

function mostrarOutrosContatos( siteUrl ){
    var url = siteUrl + "html/json/contatos";
	$( '#faleConosco' ).html( "<img src=\"" + siteUrl + "img/ajax-loader.gif\"/>" );
	$.getJSON( url,
		function( data ){
			$( '#faleConosco' ).html( data.texto );
        }
    );
}

function mostrarUltimoPostDoBlog( siteUrl ){
    var url = siteUrl + 'blog';
    var hoje = new Date();
    
	$( '#blogData' ).html( hoje.getDate() + "/" + ( hoje.getMonth() + 1 ) + "/" + hoje.getFullYear() );
	$( '#blogTitulo' ).html( "" );
	$( '#blogTexto' ).html( "Carregando &Uacute;ltimo Post. <img src=\"" + siteUrl + "img/ajax-loader.gif\"/>" );
	
	$.getJSON( url, function( data ){
		if( data ){
			$( '#blogData' ).html( "" );
			$( '#blogTitulo' ).html( "" );
			$( '#blogTexto' ).html( "Aqui neste espa&ccedil;o o internauta pode conhecer as opini&otilde;es do senador sobre os fatos do dia, fazer perguntas e registrar coment&aacute;rios, cr&iacute;ticas, den&uacute;ncias e sugest&otilde;es.  Al&eacute;m das postagens di&aacute;rias e das enquetes, o blog tamb&eacute;m tem espa&ccedil;o para uma coluna escrita a v&aacute;rias m&atilde;os. O blog &eacute; um espa&ccedil;o democr&aacute;tico e a sua participa&ccedil;&atilde;o &eacute; muito importante. " );		
		}else{		
   			var hoje = new Date();
			$( '#blogData' ).html( hoje.getDate() + "/" + ( hoje.getMonth() + 1 ) + "/" + hoje.getFullYear() );
			$( '#blogTitulo' ).html( "Nenhum Post Encontrado" );
			$( '#blogTexto' ).html( "Nenhum Post Encontrado no Blog do Senador." );
		}
	} );
}

function irParaPaginaDaCombo( comboBox ){
	if( comboBox.value != "" ){
		window.open( comboBox.value, '_blank' );
	}
}

function ListaDePerguntas( siteUrl, urlDaLista ){
	this.urlDaLista = siteUrl + urlDaLista;
	this.posicaoAtual = 0;
	this.lista = new Array();
	$( '#perguntas' ).html( "<img src=\"" + siteUrl + "img/ajax-loader.gif\"/>" );
	
	$.getJSON( this.urlDaLista, 
		function( data ){
			listaDePerguntas.lista = data;
			listaDePerguntas.mostrarLista();
		} );
	
	this.mostrarAnteriores = function(){
		if( this.lista.length == 0 )
			return;
	
		this.posicaoAtual -= 4;
		if( this.posicaoAtual < 0 ){
			this.posicaoAtual = 0;
		}
		this.mostrarLista();
	}
	
	this.mostrarProximas = function(){
		if( this.lista.length == 0 )
			return;
			
		this.posicaoAtual += 4;
		if( this.posicaoAtual >= this.lista.length ){
			this.posicaoAtual = ( Math.floor( ( this.lista.length - 1 ) / 4 ) ) * 4;
		}
		this.mostrarLista();	
	}
	
	this.mostrarLista = function(){
		var html = "<ul>";
		var pergunta = null;
		for( i = this.posicaoAtual; i < ( this.posicaoAtual + 4 ) && i < this.lista.length; i++ ){
			pergunta = this.lista[ i ];
			html += "<li><span class=\"numero\">" + ( i + 1 ) + ".</span>";			
			html += "<a href=\"#faleConosco\" onClick=\"listaDePerguntas.mostrarPergunta( " + i + " )\">";
			html += pergunta.texto + "</a></li>";
		}  
		html += "</ul>";
		$( '#perguntas' ).html( html );
	}
	
	this.mostrarPergunta = function( indice ){
		var pergunta = this.lista[ indice ];
		$( '#pergunta' ).html( pergunta.texto ); 
		$( '#resposta' ).html( pergunta.resposta );
		$( '#campoEsquerdoPeR' ).css( "display", "block" );
	}
}

var mudancasDeTexto = 0;

function restaurarTexto( idDoElemento ){
	var elemento = document.getElementById( idDoElemento );
	var filhos = elemento.getElementsByTagName( "*" );
	
	for( i = 0; i < filhos.length; i++ ){
		if( filhos[i].style.fontSize ){
			var tamanhoAtual = parseInt( filhos[i].style.fontSize.replace( "px","" ) );
		} else {
			tamanhoAtual = 12;
		}
		filhos[ i ].style.fontSize = ( tamanhoAtual - ( 2 * mudancasDeTexto ) ) + "px";
	}
	mudancasDeTexto = 0;
}

function aumentarTexto( idDoElemento ){
	var elemento = document.getElementById( idDoElemento );
	var filhos = elemento.getElementsByTagName( "*" );
	
	if( mudancasDeTexto == 8 )
		return;
		
	mudancasDeTexto++;
	
	for( i = 0; i < filhos.length; i++ ){
		if( filhos[i].style.fontSize ){
			var tamanhoAtual = parseInt( filhos[i].style.fontSize.replace( "px","" ) );
		} else {
			tamanhoAtual = 12;
		}
		filhos[ i ].style.fontSize = ( tamanhoAtual + 2 ) + "px";
	}
}

function diminuirTexto( idDoElemento ){
	var elemento = document.getElementById( idDoElemento );
	var filhos = elemento.getElementsByTagName( "*" );
	
	if( mudancasDeTexto == -4 )
		return;
		
	mudancasDeTexto--;
	
	for( i = 0; i < filhos.length; i++ ){
		if( filhos[i].style.fontSize ){
			var tamanhoAtual = parseInt( filhos[i].style.fontSize.replace( "px","" ) );
		} else {
			tamanhoAtual = 12;
		}
		filhos[ i ].style.fontSize = ( tamanhoAtual - 2 ) + "px";
	}
}


function mostrarFormularioDeEnviarEmail() {
	var formEmail = document.getElementById( 'formEmail' );
	formEmail.style.display = "block";
	formEmail.style.position = "absolute";
	formEmail.style.top = "145px";
	formEmail.style.right= "30px";
	formEmail.style.zIndex= "10";
}
	
function fecharFormularioDeEnviarEmail() {
	var formEmail = document.getElementById( 'formEmail' );
	var formulario = $('enviarPorEmail');

	for( var i = 0; i < formulario.elements.length; i++ ){
		formulario.elements[ i ].value = "";
	}
	formEmail.style.display = "none";
}
	
function enviarPorEmail(){
	var formulario = $( '#enviarPorEmail');
	var boxDoFormulario = $( '#formEmail' );
	var ajaxLoader = $( '#ajaxLoader' );
	
	formulario.ajaxSubmit(
		{ success: 
			function( responseText, statusText ){
				alert( responseText );
				$( '#ajaxLoader' ).css( "display", "none" );
				if( responseText == 'E-mail enviado com sucesso.' ){
					fecharFormularioDeEnviarEmail();
				}
			} 		
		}
	);
	
	boxDoFormulario.css( "height", "550px" ); 
	ajaxLoader.css( "display", "block" );
}	

function alterarElemento( idElemento ){
	var elemento = document.getElementById( idElemento );
	if( elemento.style.display == "none" ){
		elemento.style.display = "block";
	}else{
		elemento.style.display = "none";
	}
}

function ListaDeBusca( siteUrl, idDoFormularioDeBusca ){
	this.siteUrl = siteUrl;
	this.categorias = new Array();	
	this.categoriaAtual = "";
	this.numeroPorPagina = 15;
	
	this.formulario = $( "#" + idDoFormularioDeBusca );		
	this.formulario.ajaxSubmit(
		{ dataType: 'json',
		  type:     'POST',
		  success: 
			function( data ){
				busca.categorias = data;
				if( busca.categorias.length != 0 ){
					$( '#caixaLoader' ).html( "" );
					$( '#caixaLoader' ).css( "display", "none" );
					
					busca.mostrarListaDeCategorias();
					busca.mostrarCategoria( 0 );
				}else{				
					$( '#caixaLoader' ).html( "<h3>Nenhum Resultado Encontrado</h3>" );
				}
			} 		
		}
	);	
	
	this.executarBusca = function(){
		$( '#caixaLoader' ).css ( "display", "block" );
		$( '#caixaLoader' ).html( "Procurando... <img src=\"" + this.siteUrl + "img/ajax-loader.gif\"/>" );
		
		$( '#listaDeCategorias' ).html( "" );	
		$( '#resultadosDaBusca' ).html( "" );
		$( "#caixaDeBusca" ).css ( "display", "block" );
						
		return false; 
	}
		
	this.mostrarListaDeCategorias = function(){
		var html = "<h3>Categorias</h3><ul>";
		var contador = 0;
		var categoria = null;
		
		for( i = 0; i < this.categorias.length; i++ ){	
			categoria = this.categorias[ i ];
			html += "<li><a href=\"#ancoraDeBusca\" onClick=\"busca.mostrarCategoria( " + i + ");\">" + categoria.nome + "</a></li>";
			contador++;
		}
		
		html += "</ul>";
		
		$( '#listaDeCategorias' ).html( html );
	}
        
	this.mostrarAnteriores = function(){
		this.categorias[ this.categoriaAtual ].posicaoAtual -= this.numeroPorPagina;
		if( this.categorias[ this.categoriaAtual ].posicaoAtual < 0 ){
			this.categorias[ this.categoriaAtual ].posicaoAtual = 0;
		}
		this.mostrarCategoria( this.categoriaAtual );
	}
	
	this.mostrarProximas = function(){
		this.categorias[ this.categoriaAtual ].posicaoAtual += this.numeroPorPagina;
		if( this.categorias[ this.categoriaAtual ].posicaoAtual >= this.categorias[ this.categoriaAtual ].objetos.length ){
			this.categorias[ this.categoriaAtual ].posicaoAtual = ( Math.floor( ( this.categorias[ this.categoriaAtual ].objetos.length - 1 ) / this.numeroPorPagina ) ) * this.numeroPorPagina;
		}
		this.mostrarCategoria( this.categoriaAtual );
	}
	
	this.mostrarCategoria = function( indiceDaCategoria ){
		var html = "";
		var categoria = this.categorias[ indiceDaCategoria ];
		
		this.categoriaAtual = indiceDaCategoria;
		
		html += "<h3>" + categoria.nome;
		html += " (" + ( categoria.posicaoAtual + 1 ) + "-";
		if( ( categoria.posicaoAtual + this.numeroPorPagina + 1 ) < categoria.objetos.length ){
			html += "" + ( categoria.posicaoAtual + this.numeroPorPagina );
		}else{
			html += "" + ( categoria.objetos.length );
		}
		html += " de " + categoria.objetos.length +  ")</h3>";

		html += "<ul>";

		for( i = categoria.posicaoAtual; i < ( categoria.posicaoAtual + this.numeroPorPagina ) && i < categoria.objetos.length; i++ ){		
			objeto = categoria.objetos[ i ];
			if( objeto ){
				html += "<li>" + ( i + 1 ) + ". ";
				html += "<a target=\"_blank\" href=\""+ this.siteUrl + categoria.componente + "/visualizar/" + objeto.id + "\">";
				html += objeto.nome;
				html += "</a></li>";
			}
		}		
		html += "</ul>";
		html += "<div id=\"paginasBusca\">";
		
		if( categoria.posicaoAtual > 0 ){
			html += "<a href=\"#ancoraDeBusca\" onClick=\"busca.mostrarAnteriores();\">&lt;</a>&nbsp;";
		}else{
			html += "&nbsp;&nbsp;&nbsp";
		}
		if( categoria.posicaoAtual < categoria.objetos.length - this.numeroPorPagina ){
			html += "<a href=\"#ancoraDeBusca\" onClick=\"busca.mostrarProximas();\">&gt;</a>";
		}
		
		html += "</div>";
		$( '#resultadosDaBusca' ).html( html );
	}
}

function fecharCaixaDeBusca(){
	$( '#caixaDeBusca' ).css( "display", "none" );
}