window.onload=function()
{
	criaElementos();
	verificaInt=setInterval("verifica()", 100);
}

page="";
//page: controla do que está sendo exibido;
prefix="[page]";
//prefix: prefixo responsável por indicar à página que o hash contém um valor para ser usado como requisição ajax. Previne contra valores digitados acidentalmente no hash (depois do # da url);
index="homeBackState";
//index: é esse valor que indica quando a página volta ao estado inicial;
is_ie=(navigator.userAgent.toLowerCase().indexOf("msie")!=-1)?true:false;
//is_ie: verifica se o navegador é o Internet Explorer.

//Cria o iFrame dinamicamente.
function criaElementos()
{
	var theBody=document.getElementsByTagName("body")[0];
	var iframe=document.createElement("iframe");
	iframe.setAttribute("src", URLsite + "regional/controle.htm?id="+prefix+index);
	iframe.setAttribute("id", "bmr-ajaxControl");
	iframe.style.display="none";
	theBody.appendChild(iframe);
}
	
//Essa função atua como encapsulamento, é ela que é chamada quando se verifica que o estado da página é o inicial. Aí ela remonta a URL sem os hashes e atualiza.	
function volta_index()
{
	//location="http://"+location.host+location.pathname+"#";
	// location.reload();
}

//Essa função é responsável por mudar a fonte do iFrame no IE ou mudar o Hash em outros navegadores.
function ajax(pag)
{
	var iframe=document.getElementById('bmr-ajaxControl');
	if(is_ie)
	{
		iframe.src= URLsite + "regional/controle.htm?id="+prefix+pag;
	}
	else
	{
		if(pag!=index)
		{
			location.hash=prefix+pag;
		}
		else
		{
			volta_index();
			//location.hash=prefix+pag;
		}
	}
}


function verifica()
{
	var hash=(typeof((hash=location.href.split("#")[1]))!="undefined"?hash:"");
	if(hash!=page)
	{
		if(hash!="" && hash.indexOf(prefix)!=-1)
		{
			carregaConteudo(hash.replace(prefix,""), 'ajax', true);
			page=hash; ajax(hash.replace(prefix,""));
		}
		else
		{
			clearInterval(verificaInt);
            //alert(prefix+page);
			location.hash=page; //Index
			
			ajax(index);
		}
	}
}
