var timerDestaque;							// Usada para o TIMER
var STEP = 2;					// Número de pixels a andar para cada passo da animação
var DELAY_STEP = 10;			// Pausa de tempo entre cada iteração da animação
var DELAY = 8000;				// Pausa entre as trocas de destaques

var txtRightOut = 0;			// Posição do destaque que está saindo
var spaceAv = 9999;				// Espaço disponível pelo elementoPai, que reje o espaço a ser utilizado
var txtRightIn = 9999;			// Posição do destaque que está entrando

var indexOut = 0;				// Index do elemento que está saindo 
var indexIn = 1;				// Index do elemento que está entrando
var canPause = true;			// Saber se pode pausar ou se está rolando uma animação
var paused = false;				// inicialmente está o destaque parado, não pausado

var destaques;					// Destaques que estarão no slider
var isDestaque;					// Existem destaques no array acima?

function nextDestaque(){
	if(!isDestaque)
		return;

	clearTimeout(timerDestaque);
	moverDestaques();
}

function previousDestaque(){
	if(!isDestaque)
		return;

	if(canPause){
		clearTimeout(timerDestaque);
		
		indexIn = indexOut - 1;
		if(indexIn < 0)
			indexIn = destaques.length - 1;
			
		destaques[indexIn].style.top = (0 - spaceAv) + "px";
		txtRightOut = 0;
		txtRightIn = 0 - spaceAv;
		
		prevDest();
	}
}


function goTo(index){
	if((indexIn - 1 != index && indexIn != 0) || (indexIn == 0 && index != destaques.length - 1)){
		indexIn = index;
		moverDestaques();
	}
}


function prevDest(){
	clearTimeout(timerDestaque);
	if(!isDestaque)
		return;

	canPause = false;
	txtRightOut += STEP;
	destaques[indexOut].style.top = txtRightOut + "px";
	
	txtRightIn += STEP;
	destaques[indexIn].style.top = txtRightIn + "px";
	
	timerDestaque = setTimeout("prevDest()",DELAY_STEP);

	if(txtRightIn >= 0){
		indexOut = indexIn - 1;
		if(indexOut < 0)
			indexOut = destaques.length - 1;
		resetDestaques();
	}
}

function moverDestaques(){
	clearTimeout(timerDestaque);
	if(!isDestaque)
		return;

	canPause = false;
	txtRightOut -= STEP;
	destaques[indexOut].style.top = txtRightOut + "px";
	
	txtRightIn -= STEP;
	destaques[indexIn].style.top = txtRightIn + "px";
	
	timerDestaque = setTimeout("moverDestaques()",DELAY_STEP);

	if(txtRightIn <= 0){
		resetDestaques();
	}
}

function resetDestaques(){
	if(!isDestaque)
		return;

	clearTimeout(timerDestaque);
	canPause = true;
	txtRightIn = spaceAv;
	txtRightOut = 0;

	document.getElementById("indice" + (indexIn+1)).style.color = "white";
	document.getElementById("indice" + (indexOut+1)).style.color = "#4e5f6e";

	indexIn++;
	if(indexIn >= destaques.length)
		indexIn = 0;
		
	indexOut = indexIn - 1;
	if(indexOut >= destaques.length)
		indexOut = 0;
	else if(indexOut < 0)
		indexOut = destaques.length - 1;
		
	destaques[indexIn].style.top = spaceAv + "px";
	
	if(!paused)	
		timerDestaque = setTimeout("moverDestaques()",DELAY);
}

function playPause(obj){
	if(!isDestaque)
		return;

	if(canPause){
		if(!paused){
			obj.src = "/img/bt_pause_on.jpg";
			clearTimeout(timerDestaque);
		}
		else{
			obj.src = "/img/bt_pause_off.jpg";
			timerDestaque = setTimeout("moverDestaques()", DELAY);
		}
	}
	paused = !paused;
}


function adicionarEventoA(objeto, TipoEvento, funcao){
	if(objeto.addEventListener){ // todos navegadores menos IE
		objeto.addEventListener(TipoEvento, funcao, false);
		return true;
	} else if (objeto.attachEvent){ // IE
		var r = objeto.attachEvent('on'+TipoEvento, funcao);
		return r;
	} else {
		return false;
	}
}

function beginDestaques(){
	destaques = document.getElementsByName("textoGiratorio");
	isDestaque = destaques.length > 0;
	
	if(isDestaque){
		destaques[0].style.display = "block";
		txtRightOut = Math.floor(destaques[0].style.top);
		
		var pai = document.getElementById("elementoPai");
		
		spaceAv = Math.floor( pai.style.height.replace("px","") );
		//pai.style.position = "relative";
		
		txtRightIn = spaceAv;
	
		for (i = 0; i < destaques.length; i++){
			//destaques[i].style.position = "absolute";
			if(i > 0){
				destaques[i].style.top = txtRightIn + "px";
				destaques[i].style.display = "block";
			}
		}
	}

	timerDestaque = setTimeout("moverDestaques()", DELAY);
}

adicionarEventoA(window, 'load', beginDestaques);
