//On load page, init the timer which check if the there are anchor changes each 300 ms
$().ready(function(){
	setInterval("checkAnchor()", 300);
});
var currentAnchor = null;
//Function which chek if there are anchor changes, if there are, sends the ajax petition
function checkAnchor(){
	//Check if it has changes
	if(currentAnchor != document.location.hash){
		currentAnchor = document.location.hash;
		//if there is not anchor, the loads the default section
		if(!currentAnchor)
			query = "section=home";
		else
		{
			//Creates the  string callback. This converts the url URL/#main&id=2 in URL/?section=main&id=2
			var splits = currentAnchor.substring(1).split('/');
			//Get the section
			var section = splits[1];
			delete splits[1];
                        var codigo = splits[2];
                        delete splits[2];
                        var codigo2 = splits[4];
                        delete splits[4];
			//Create the params string
			var params = splits.join('');
			var query = "section=" + section + "&id=" + codigo + "&id2=" + codigo2;
		}
		//Send the petition
                $("#loading").show();
		$.get("callbacks.php",query, function(data){
                        $("#content").hide().html(data).fadeIn();
                        $("#loading").hide();
		});

	}
}

var navegador = navigator.userAgent.toLowerCase();
var xmlhttp;

function objetoXML() {
    if (navegador.indexOf('msie') != -1) {
        var controle = (navegador.indexOf('msie 5') != -1) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP';
        try {
            xmlhttp = new ActiveXObject(controle);
        } catch (e) { }
    } else {
        xmlhttp = new XMLHttpRequest();
    }
}

function enviarForm(url, campos, destino) {
    var elemento = document.getElementById(destino);
    objetoXML();
    if (!xmlhttp) {
        elemento.innerHTML = 'Impossível iniciar o objeto XMLHttpRequest.';
        return;
    } else {
        elemento.innerHTML = 'Carregando...';
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
            if (xmlhttp.status == 200) {
                elemento.innerHTML = xmlhttp.responseText;
            } else {
                elemento.innerHTML = 'Página não encontrada!';
            }
        }
    }

    xmlhttp.open('POST', url+'?'+campos, true);
    xmlhttp.setRequestHeader('encoding','ISO-8859-1');
    xmlhttp.send(campos);
}




function fecha_banner()
{
    var banner_obj = document.getElementById('banner_popup');
    banner_obj.style.display = 'none';
}

function fecha_banner_timeout()
{
    setTimeout('fecha_banner()', 30000);
}

function abre_banner()
{
    var banner_obj = document.getElementById('banner_popup');

    banner_obj.style.left = '1001px';
    banner_obj.style.top = '1px';

    banner_obj.style.display = '';

    //fecha_banner_timeout();
}
