// JavaScript Document

var xmlhttp = null;
	
try {

	xmlhttp = new XMLHttpRequest();

} catch(ee) {

	try {
		
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		
	} catch(e) {

		try {
			
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			
		} catch(E) {
			
			xmlhttp = false;
			
		}
	}

}

function go_to(pagina)
{
	if(pagina.indexOf("?") > -1)
	{
		var array_complemento = (pagina.split("?"));
		var complemento = array_complemento[1];
		var page_temp = array_complemento[0];
	
		var array_extensao = (page_temp.split("."));
		var extensao = array_extensao[1];
		var page = array_extensao[0];
		
		//alert("page = " + page + " e extensao = " + extensao);
	}
	else
	{
		var array = (pagina.split("."));
		var extensao = array[1];
		var page = array[0];
		
		//alert("page = " + page + " e extensao = " + extensao);
	}
		
	//Exibe o texto carregando no div conteúdo
	var conteudo = document.getElementById("conteudo");
	conteudo.innerHTML = '<center><div style="padding-top:100px; _padding-bottom:100px; font-family:Arial, sans-serif; font-size:11px;"><img src="images/carregando.gif"><BR>carregando...</div></center>';
	
	//alert(extensao);
	
	if(extensao == 'html')
		xmlhttp.open("POST", "index.php?p=" + page, true);
	else if(extensao == 'php')
		xmlhttp.open("POST", pagina, true);
	
	xmlhttp.onreadystatechange = function() {

		if (xmlhttp.readyState == 4){

			var conteudo = document.getElementById("conteudo");
			conteudo.innerHTML = xmlhttp.responseText;
			pageTracker._trackPageview(pagina); //code

		}
	}
	
	xmlhttp.send(null);
}


/********************************************** HISTORICO *****************************************************/

var appHash = "";
var appIndex = "";
var appDefault = "conteudo.html";
var isIe = (navigator.userAgent.toLowerCase().indexOf("msie") > -1) ? true : false;

function $(id) { return document.getElementById(id); }

function historico(newHash) {
	if(isIe === true)
		$("control-iframe").setAttribute("src", "smarty/templates/control.html?id=" + newHash);
	else if(newHash != appIndex)
		window.location.hash = newHash;
	else
		go2index();
}

function handleHistory() {
	var browserHash = (hash = window.location.href.split("#")[1]) ? hash : "";
	
	if(browserHash != appHash) {
		if(browserHash != "") {				
			go_to(browserHash);
			appHash = browserHash;
			historico(browserHash);
		}
		else {
			clearInterval(checkInterval);
			window.location.hash = appHash;
			historico(appIndex);
		}
	}
}

function createIFrame() {
	var iFrame = document.createElement("iframe");
	iFrame.setAttribute("src", "smarty/templates/control.html?id=" + (hash = window.location.href.split("#")[1]) ? hash : appDefault);
	iFrame.setAttribute("id", "control-iframe");
	iFrame.style.display = "none";
	document.body.appendChild(iFrame);
}

function initialize() {
	if(isIe === true)
		createIFrame();
	checkBookmark();
	checkInterval = setInterval(handleHistory, 100);
}

function checkBookmark() {
	var browserHash = (hash = window.location.href.split("#")[1]) ? hash : "";
	window.location.hash = (browserHash == "") ? appDefault : browserHash;
}

function go2index() {
	window.location = window.location.href.split("#")[0] + "#";
	window.location.reload();
}

/********************************************** FIM *****************************************************/



function setInterval_imagem_cet_rio()
{
	window.setInterval('atualiza_imagem_cet_rio()', 90000);
}

function atualiza_imagem_cet_rio()
{
	document.getElementById('img_cet_rio').src = 'http://transito.rio.rj.gov.br/imagens1/92.jpg'; 
}

function carregaPagina(tag)
{
	xmlhttp.open("GET", 'menu_post.php?id_tag=' + tag, true);
	
	xmlhttp.onreadystatechange = function() {

		if (xmlhttp.readyState == 4){

			var conteudo = document.getElementById("menu_post");
			conteudo.innerHTML = xmlhttp.responseText;

		}
	}
	
	xmlhttp.send(null);
}

function carregaConteudo(id_post)
{
	var link = "post.php?id_post=" + id_post;
	
	historico(link);
}

function carregaMostrarTodos(controle)
{
	var link = "mostrar_todos.php?visivel=" + controle;
	
	historico(link);
}

function contato()
{
	var link = "faleconosco.php";
	
	historico(link);
}
/*************************************** COMENTARIOS *************************************/

function validaComentario()
{	
	if (document.frm_comentario.nome.value == 0){
		alert("Preencha o nome");
		document.frm_comentario.nome.focus();
		return false;
	}

/*	if (document.frm_comentario.email.value != 0 && document.frm_comentario.email.value.indexOf("@") == -1){
		alert("Email inválido");
		document.frm_comentario.email.focus();
		return false;
	}*/
	
	if (document.frm_comentario.mensagem.value == 0){
		alert("Cometário em branco");
		document.frm_comentario.mensagem.focus();
		return false;
	}
	
	document.frm_comentario.co_operacao.value = 1;

	document.frm_comentario.action = "comentario.php"; 
	document.frm_comentario.submit();
	
	return true;

}

function replaceCarriageReturn(emailBody, replaceWith)
{
	//emailBody = retira_acentos(emailBody);
	emailBody = escape(emailBody); //encode all characters in text area to find carriage return character
	
	for(i=0; i < emailBody.length; i++) {
		//loop through string, replacing carriage return encoding with HTML break tag
		if(emailBody.indexOf("%0D%0A") > -1) {
			//Windows encodes returns as \r\n hex
			emailBody=emailBody.replace("%0D%0A",replaceWith)
		}
		else if(emailBody.indexOf("%0A") > -1) {
			//Unix encodes returns as \n hex
			emailBody=emailBody.replace("%0A",replaceWith)
		}
		else if(emailBody.indexOf("%0D") > -1) {
			//Macintosh encodes returns as \r hex
			emailBody=emailBody.replace("%0D",replaceWith)
		}
	}
	emailBody=unescape(emailBody) //decode all characters in text area back
	//alert(emailBody);
	
	return emailBody;
	//validaComentarios(emailBody);
}