// Definição de variáveis
var v_cache = new Array;
var v_cont = 0;
var v_indiceAtual = "";
var v_depurar = true;
var v_iframeCarregado = false;
var v_divSaida = "";

//Exibe informações de depuração (se v_depurar estiver habilitado)
/*
function v_depuracao (str) {
	if(v_depurar){
	  var divInformacao = document.getElementById("divInformacao");
	  divInformacao.innerHTML = divInformacao.innerHTML = "<br>" + str;	  
	}
}
*/

//Verifica se precisa atualizar a página
function v_checaEstado() {
  if(v_iframeCarregado == false)
    return;

  var doc = window.frames['frameAuxiliar'].document;
  var novoIndice = doc.getElementById('divContagem').innerHTML;
  if (novoIndice != v_indiceAtual) {
//     var msgDepuracao = "IFRAME alterado. Passou de "+v_indiceAtual+" para "+novoIndice;
	  
	  //Carrega o estado anterior da cache
	  if (v_cache[novoIndice]) {
		var divPrincipal = document.getElementById("principal");
		divPrincipal.innerHTML = v_cache[novoIndice];
//		msgDepuracao += " (acessei a posição "+ novoIndice +" da cache)";
	  }
	  v_indiceAtual = novoIndice;
//	  v_depuracao (msgDepuracao);
  }
}

//Função chamada após o carregamento do IFRAME
function v_carregado (){
  v_iframeCarregado = true;	
}

//Atualiza o IFRAME invisível
function v_carregaFrame() {
  var frameAuxiliar = document.getElementById("frameAuxiliar");
  v_iframeCarregado = false;
  frameAuxiliar.src = "contagem.asp?contador="+ v_cont;
}

//Salva o estado corrente na cache
function v_salvaEstado (){
//alert('aqui!!!');
  var conteudoCache = document.getElementById(v_divSaida);
  v_cont++;
  v_cache[v_cont] = conteudoCache.innerHTML;
//  v_depuracao("Adicionei "+ v_cont +" no cache");
  v_carregaFrame ();
  v_indiceAtual = v_cont;  
}

//Carrega o IFRAME invisível e inicia o timer
function v_inicia (nomeDiv) {
//alert(nomeDiv);
  v_divSaida = nomeDiv;
  v_carregaFrame();
  window.setInterval('v_checaEstado()', 1000);
  v_salvaEstado();
}

//Criação do objeto XMLHttpRequest
function iniciaAjax() {
  var ajax;
  if(window.XMLHttpRequest){ //Mozilla, Safari, ...
    ajax = new XMLHttpRequest();
  }
  else if (window.ActiveXObject) { //IE
    ajax = new ActiveXObject("Msxml2.XMLHTTP");
	if (!ajax) {
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	}
  }
  return ajax;
}
