var timer = null;

if(navigator.appName == 'Microsoft Internet Explorer')
{
	incremento         		= 20;
	deslocamento_atual   	= 0;
	tamanho_compensado 		= 0;
    milisegundos		  	= 100;
	deslocamento_referencia = 180;
	tamanhoExibir        	= 540;
   
}else if(navigator.appName == 'Netscape'){

	incremento         		= 30;
	deslocamento_atual   	= 0;
    tamanho_compensado 		= 0;
    milisegundos		  	= 110;
	deslocamento_referencia = 180;
	tamanhoExibir        	= 540;
	
}

var deslocamento      	 	  = deslocamento_referencia;
var tabelareferenciaID        = 'tabelafim';
var divrolanteID			  = 'rolante';			
var idBotaoEsquerda			  = 'botaoEsquerda';
var botaoEsquerda			  = www_url + 'img/navegador.esq.azul.jpg';
var botaoEsquerdaDesabilitado = www_url + 'img/navegador.esq.cinza.jpg';
var idBotaoDireita			  = 'botaoDireita';
var botaoDireita			  = www_url + 'img/navegador.dir.azul.jpg';
var botaoDireitaDesabilitado  = www_url + 'img/navegador.dir.cinza.jpg';


function cartelaCentral()
{
	divRolante 	   = document.getElementById('rolante');
	todas_cartelas = divRolante.childNodes;

	x = 0;
	
	cartelas = new Array();
	
	for(i=0;i<todas_cartelas.length;i++)
	{
		if( (todas_cartelas[i].id != undefined) && (todas_cartelas[i].id != null) )
		{
			if( (todas_cartelas[i].style.display != 'none') && (todas_cartelas[i].id !='tab0')&& (todas_cartelas[i].id !='tab1000') && (todas_cartelas[i].id !='tabelafim') )
			{
				cartelas[x] = todas_cartelas[i];
				x++;
			}
		}
	}

	indice = ((divRolante.offsetLeft / deslocamento_referencia) * -1);

	retorno = null;
	
	if((cartelas[indice] != undefined) && (cartelas[indice] != null))
	{
		retorno = cartelas[indice].id;
	}
	
	return retorno;
}

function contarCartelas()
{
	numero_cartelas = 0;
	
	elementos = document.getElementById('rolante').childNodes;
	
	for(i=0;i<elementos.length;i++)
	{
		if( (elementos[i].id != undefined) && (elementos[i].id != 'tabelafim') )
		{
			if(elementos[i].style.display != 'none')
			{
				numero_cartelas++;
			}
		}
	}
	
	return numero_cartelas;
}

function validarBotoes()
{
	validarBotaoEsquerda();
	validarBotaoDireita();
}

function validarBotaoEsquerda()
{
	leftDiv = document.getElementById(divrolanteID).offsetLeft;
	
	if(leftDiv < 0)
	{
			habilitaImagemBotao(idBotaoEsquerda, botaoEsquerda, deslocarRolandoEsquerda);
	}else{
			desabilitaImagemBotao(idBotaoEsquerda, botaoEsquerdaDesabilitado);
	}
}

function validarBotaoDireita()
{
	objetoDiv = document.getElementById(divrolanteID);
	leftTabela = contarCartelas() * deslocamento;//(contarPalavra('<tbody>',objetoDiv.innerHTML)-1 - tabelasRetiradas) * deslocamento;//document.getElementById(tabelareferenciaID).offsetLeft;
	leftDiv    = document.getElementById(divrolanteID).offsetLeft;
	
	if((leftTabela + leftDiv) > tamanhoExibir)
	{
			habilitaImagemBotao(idBotaoDireita, botaoDireita, deslocarRolandoDireita);
	}else{
			desabilitaImagemBotao(idBotaoDireita, botaoDireitaDesabilitado);
	}
}

function desabilitaImagemBotao(idImagemBotao,caminhoImagem)
{
	imagemBotao = document.getElementById(idImagemBotao);
	imagemBotao.src = caminhoImagem;
	imagemBotao.onclick = '';
}

function habilitaImagemBotao(idImagemBotao,caminhoImagem,nomeFuncao)
{
	imagemBotao = document.getElementById(idImagemBotao);
	imagemBotao.src = caminhoImagem;
	imagemBotao.onclick = nomeFuncao;
}

function deslocarEsquerda()
{
	objetoDiv = document.getElementById(divrolanteID);
	leftDiv   = objetoDiv.offsetLeft;

	if(leftDiv == -1){ leftDiv = 0;}

	if(deslocamento_atual == 0)
	{
			if(leftDiv < 0)
			{
				leftDiv = leftDiv + (deslocamento + tamanho_compensado);
			}
	}else{
			if(leftDiv < 0)
			{
				leftDiv = leftDiv + (incremento + tamanho_compensado);
			}
	}
	
	objetoDiv.style.left = leftDiv + 'px';
}

function deslocarDireita()
{
	objetoTabela = document.getElementById(tabelareferenciaID);
	objetoDiv 	 = document.getElementById(divrolanteID);
	leftTabela   = contarCartelas() * deslocamento;//(contarPalavra('<tbody>',objetoDiv.innerHTML)- 1 - tabelasRetiradas) * deslocamento;//objetoTabela.offsetLeft;
	leftDiv	     = objetoDiv.offsetLeft;
	
	if(deslocamento_atual == 0)
	{
			if((leftTabela + leftDiv) > tamanhoExibir)
			{
				leftDiv = leftDiv - (deslocamento - tamanho_compensado);
			}
	}else{
			if((leftTabela + leftDiv) > tamanhoExibir)
			{
				leftDiv = leftDiv - (incremento - tamanho_compensado);
			}
	}
	objetoDiv.style.left = leftDiv + 'px';
}

function deslocarRolandoEsquerda()
{
	desabilitaImagemBotao(idBotaoEsquerda, botaoEsquerdaDesabilitado);
	desabilitaImagemBotao(idBotaoDireita, botaoDireitaDesabilitado);
	
	objetoTabela = document.getElementById(tabelareferenciaID);
	objetoDiv 	 = document.getElementById(divrolanteID);
	leftTabela   = contarCartelas() * deslocamento;//(contarPalavra('<tbody>',objetoDiv.innerHTML)-1  - tabelasRetiradas) * deslocamento;//objetoTabela.offsetLeft;
	leftDiv	     = objetoDiv.offsetLeft;
	
	if((deslocamento_atual < deslocamento_referencia) && (leftDiv < 0))
	{
			deslocamento_atual = deslocamento_atual + incremento;
			deslocarEsquerda();
			timer = setTimeout('deslocarRolandoEsquerda()',milisegundos);
	}else{
			clearTimeout(timer);
			deslocamento_atual = 0;
			if(leftDiv < 0){
				habilitaImagemBotao(idBotaoEsquerda, botaoEsquerda, deslocarRolandoEsquerda);
			}
			if((leftTabela + leftDiv) > tamanhoExibir){
				habilitaImagemBotao(idBotaoDireita, botaoDireita, deslocarRolandoDireita);
			}
	}
}

function deslocarRolandoDireita()
{
	desabilitaImagemBotao(idBotaoDireita, botaoDireitaDesabilitado);
	desabilitaImagemBotao(idBotaoEsquerda, botaoEsquerdaDesabilitado);
	
	objetoTabela = document.getElementById(tabelareferenciaID);
	objetoDiv 	 = document.getElementById(divrolanteID);
	leftTabela   = contarCartelas() * deslocamento;//(contarPalavra('<tbody>',objetoDiv.innerHTML)-1  - tabelasRetiradas) * deslocamento;//objetoTabela.offsetLeft;
	leftDiv	     = objetoDiv.offsetLeft;
	
	if((deslocamento_atual < deslocamento_referencia) && ((leftTabela + leftDiv) > tamanhoExibir))
	{
			deslocamento_atual = deslocamento_atual + incremento;
			deslocarDireita();
			timer = setTimeout('deslocarRolandoDireita()',milisegundos);
	}else{
			clearTimeout(timer);
			deslocamento_atual = 0;
			if((leftTabela + leftDiv) > tamanhoExibir){
				habilitaImagemBotao(idBotaoDireita, botaoDireita, deslocarRolandoDireita);
			}
			if(leftDiv < 0){
				habilitaImagemBotao(idBotaoEsquerda, botaoEsquerda, deslocarRolandoEsquerda);
			}
	}
}

/*
function contarPalavra(palavra,string)
{
	string = string.toLowerCase();
	array = string.split(palavra);
	return (array.length - 1);
}
*/