var timerID = 0;
var timerIDExibicao = 0;
var tStart  = null;

tempoAtualizacaoLista = 15*1000; // segundos*1000
tempoProximaTarefa = 3*1000; // segundos*1000

function UpdateTimer() {
   	timerID = setTimeout("UpdateTimer()", tempoProximaTarefa);
   	try{
   		avancar();
   	}catch(e){
	}
}

function UpdateTimerExibicao() {
   timerIDExibicao = setTimeout("UpdateTimerExibicao()", tempoAtualizacaoLista);
   atualizarLista();
}

function Start() {
	if(timerID == 0){
   		timerID  = setTimeout("UpdateTimer()", 300);
   		var div2 = document.getElementById('imagemLoadingStoped');
		div2.style.display = 'none';
		var div = document.getElementById('imagemLoading');
		div.style.display = '';
   	}
}

function StartExibicao() {
   timerIDExibicao  = setTimeout("UpdateTimerExibicao()", tempoAtualizacaoLista);
}

function Stop() {
   if(timerID) {
      clearTimeout(timerID);
      timerID  = 0;
      var div = document.getElementById('imagemLoading');
	  div.style.display = 'none';
	  var div2 = document.getElementById('imagemLoadingStoped');
	  div2.style.display = '';
   }
   tStart = null;
}

function avancarParar() {
	Stop();
	avancar();
}

function avancar() {
	new Effect.BlindDown('exibicao');
	form = document.forms[0];
	//alert(ind + ' ' + total);
	if(ind + 1 >= total){
		ind = -1;
	}
	ind = ind + 1;
	document.getElementById("mytext").innerHTML = tarefas[ind];
	
	status = 'avanca';
}

function retornar() {
	Stop();
	new Effect.BlindDown('exibicao');
	form = document.forms[0];
	if(status = 'avanca'){
		if(ind <= 0){
			//alert(ind);
			ind = total - 1;
		}else{
			//alert("2222");
			ind = ind - 1;
		}
	}else{
		if(ind == 0){
			ind = total - 1;
		}else{
			ind = ind - 1;
		}
	}
	document.getElementById("mytext").innerHTML = tarefas[ind];
	status = 'retorna';
}

var getAssunto = function(tarefa) { return tarefa.assunto };

function preencherTabela(lista) {
	form = document.forms[0];
	DWRUtil.removeAllRows("tabela");
	var indiceInc = 0;
	//alert("valores: " + total + " - " + lista.length);
	if( (total == 0) && (lista.length > 0) ){
		//alert("foi");
		total = lista.length;		
		var div = document.getElementById('principalTarefas');
		div.style.display = '';
		Start();
	}else{
		if(total != lista.length){
			var div = document.getElementById('principalTarefas');
			div.style.display = '';
			if(timerID == 0){
				Start();
			}			
		}
		if(lista.length == 0){
			var div = document.getElementById('principalTarefas');
			div.style.display = 'none';
			Stop();
		}
		total = lista.length;
	}

	tarefas = new Array(total);
		DWRUtil.addRows( "tabela", lista , [ getAssunto ], {
		rowCreator:function(options) {
			var row = document.createElement("tr");
			return row;
		},
		cellCreator:function(options) {
			var td = document.createElement("td");
		   	//alimenta ARRAY
		   	tarefas[indiceInc] = options.data;
			indiceInc = indiceInc + 1;
			return td;
		}
	});
	
}

function inicializarPagina(){
	exibir();
}

var ind = 0;
var total = 0;
var tarefas =  new Array(100); 
var status = 'avanca'; // indica se o esta avancando(avanca) ou retornando(retorna) nas tarefas 

var winW = 630, winH = 460;

function verifica(){
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth - 220;
			winH = 0;
			//alert("IE winW: "+winW+" -- winH: "+winH);
		}else{
			winW = window.innerWidth - 220;
			winH = 0;
			//alert("FOX winW: "+winW+" -- winH: "+winH);
		}
		moveDiv('principalTarefas', winW , winH);
}

function moveDiv(name, left, top){
  	var the_style = getStyleObject(name);
  	var the_left = left;
  	var the_top = top;
 	
 	if (document.layers) {
	  	the_style.left = the_left;
	  	the_style.top = the_top;  
  	}else{
	  	the_style.left = the_left + "px";
	    the_style.top = the_top + "px"; 
  	}
}

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
}

function atualizarLista() {
	try{
		AjaxTarefasUsuario.getListaAtualizada(preencherTabela);
	}catch(e){
	}
}

function fechar() {
	var div = document.getElementById('principalTarefas');
	div.style.display = 'none';
	Stop();
}

function abrir(url) {
	//alert(url);
	var frame = document.getElementById('iContent');
	frame.src = url;
}


function exibir(){
	try{
		AjaxTarefasUsuario.getUsuarioCallCenter(processaExibicao);
	}catch(e){
	}
}

function processaExibicao(inteiro){
	if (inteiro == 1){
		verifica();
		StartExibicao();
		try{
				AjaxTarefasUsuario.getListaAtualizada(preencherTabela);
			}catch(e){
		}
		Start();
	}else{
		var div = document.getElementById('principalTarefas');
		div.style.display = 'none';
	}
}

function startStop(){
	if(timerID == 0){
		Start();
	}else{
		Stop();
	}
}
