﻿/*-------------------------------GLOBAL VARIABLES------------------------------------*/  

var isBusy = false;
var actual = 0;

var txtloading = '				<div id=\"loading\">A Carregar...</div>';

/*-----------------------------------------------------------------------------------------------*/  

try{
    var xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

function carrega(n) {
	if (isBusy) {
		return;
		//xmlhttp.onreadystatechange = function () {}
		//xmlhttp.abort();
	}
	
    //Exibe o texto carregando no div conteúdo
	DeleteChildren( document.getElementById("divConteudo").firstChild );
	document.getElementById("divConteudo").innerHTML = txtloading;
	
    //Guarda a página escolhida na variável actual
    actual=n;
	
    //Abre a url
	//window.setTimeout('xmlhttp.open("GET", "funcoes.php?n="+n,true)', 4000);
    xmlhttp.open("GET", "funcoes.php?n=" + n, true);
	isBusy = true;
	
    //Executada quando o navegador obtiver o código
    xmlhttp.onreadystatechange = function() {

        if (xmlhttp.readyState==4) {
            //Lê o texto
            var texto = xmlhttp.responseText;

            //Desfaz o urlencode
            texto = texto.replace(/\+/g," ");
            texto = unescape(texto);

            //Exibe o texto no div conteúdo
			DeleteChildren( document.getElementById("divConteudo").firstChild );
			document.getElementById("divConteudo").innerHTML = texto;
			
			//Obtém os links do menu
			var menu  = document.getElementById("divMenu");
			var links = menu.getElementsByTagName("a");

            //Limpa as classes do menu
            for(var i=0; i<links.length; i++)
                links[i].className = "";

			//Marca o selecionado
            links[actual].className = "selected";

			dhtmlHistory.add("n:"+actual, actual);

			isBusy = false;
        }
    }
    xmlhttp.send(null);
}

// Apaga todo o conteudo de um elemento, por causa do IE6, que teima em
// nao aceitar o innerHTML com div com conteudo
function DeleteChildren(node){
	if(node){
		for(var x = node.childNodes.length - 1; x >= 0; x--){
			var childNode = node.childNodes[x];
			
			if(childNode.hasChildNodes()){
				DeleteChildren(childNode);
			}
			
			node.removeChild(childNode);

			if(childNode.outerHTML){
				childNode.outerHTML = '';
			}
			childNode=null;   
		}
		node=null;
	}
}

 
function menuclick(e){
    //Correção para eventos quebrados da Microsoft
    if(typeof(e)=='undefined')
		var e=window.event;
		
    source = e.target ? e.target : e.srcElement;
	
    //Correção para o bug do Konqueror/Safari
    if(source.nodeType == 3)
		source=source.parentNode;

    //Obtém o número quebrando a url
    n = source.getAttribute("href").replace(/.*=/,"");

	// Chama o tracker do Google Analytics
	pageTracker._trackPageview(source.getAttribute("title"));

    //Chama o carrega
    carrega( parseInt(n) );

	dhtmlHistory.add("n:"+n, source.getAttribute("title"));
	
    //Cancela o click (evita a navegação)
    return false
}

function homeclick(e){
    //Correção para eventos quebrados da Microsoft
    if (typeof(e) == 'undefined') var e = window.event;
	
    source = this;
	
    //Obtém o número quebrando a url
    n = source.getAttribute("href").replace(/.*=/,"");
	
    //Chama o carrega
    carrega(parseInt(n));

    //Cancela o click (evita a navegação)
    return false;
}


function historyChange(newLocation, historyData) {
	// if there is no location then display the default, which is the inbox
	if (newLocation == "") {
		newLocation = "n:0";
	}
   
	// extract the section to display from the location change; newLocation will begin with the word "n:" 
	newLocation = newLocation.replace(/n\:/, "");
	
	// update the browser to respond to this DHTML history change
	carrega(newLocation);
};

	
function init(){
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(historyChange);

    //Obtém os links do menu
    var menu  = document.getElementById("menu");
    var links = menu.getElementsByTagName("a");

    //Atribui o evento
    for(var i=0; i<links.length; i++)
        links[i].onclick = menuclick;
		
    document.getElementById("menuHome").onclick = homeclick;
	
	
	// determine what our initial location is
	// by retrieving it from the browser's
	// location after the hash
	var currentLocation = dhtmlHistory.getCurrentLocation();

	// if there is no location then display
	// the default, which is the inbox
	if (currentLocation != "") {
		// extract the section to display from
		// the initial location 
		currentLocation = currentLocation.replace(/n\:/, "");

		// display this initial location
		carrega(currentLocation);
	}
}

if(xmlhttp) window.onload = init;
