var _ultimas_tmp = window.onload;

var lista_u, itens_u, tamanho_real_u, topo_maximo_u, ativo_u;

var COR_CLARA = 200, COR_ESCURA = 0, NUMDE_ITERACOES = 5,
	PASSO = 20 / NUMDE_ITERACOES,
	PASSODE_COR = parseInt((COR_CLARA - COR_ESCURA) / NUMDE_ITERACOES),
	PAUSA = 4000;

var temporizador_u;

var cor_ativando_u = COR_CLARA, cor_desativando_u = COR_ESCURA;

window.onload = function() {
	_ultimas_tmp();
	
	lista_u = document.getElementById("ultimas").getElementsByTagName("ul")[0];
	itens_u = lista_u.getElementsByTagName("a");
	tamanho_real_u = itens_u.length - 3;
	topo_maximo_u = -10 - tamanho_real_u * 20;
	topo_minimo_u = -10;
	lista_u.style.top = "-10px";
	ativo_u = 1;
	ativar(ativo_u);

	document.getElementById("ultimasproxima").onclick = function() {
		clearTimeout(temporizador_u);
		mostrar_proxima();
	}

	document.getElementById("ultimasanterior").onclick = function() {
		clearTimeout(temporizador_u);
		mostrar_anterior();
	}

	temporizador_u = setTimeout(mostrar_proxima, PAUSA);
}

function subir_u() {
	var topo = parseInt(lista_u.style.top) - PASSO;
	if(topo == topo_maximo_u) topo = -10;

	lista_u.style.top = topo + "px";
	cor_ativando_u -= PASSODE_COR;
	cor_desativando_u += PASSODE_COR;
	
	itens_u[ativo_u].style.color = "#" + cor_desativando_u.toString(16) +
		cor_desativando_u.toString(16) + cor_desativando_u.toString(16);
	itens_u[ativo_u + 1].style.color = "#" + cor_ativando_u.toString(16) +
		cor_ativando_u.toString(16) + cor_ativando_u.toString(16);

	if((topo - 10) % 20 == 0) {
		cor_ativando_u = COR_CLARA;
		cor_desativando_u = COR_ESCURA;

		ativo_u = ativo_u + 1;
		if(ativo_u == tamanho_real_u + 1) {
			itens_u[ativo_u].style.color = "#" + cor_ativando_u.toString(16) +
				cor_ativando_u.toString(16) + cor_ativando_u.toString(16);
			ativo_u = 1;
			itens_u[ativo_u].style.color = "#" + cor_desativando_u.toString(16) +
				cor_desativando_u.toString(16) + cor_desativando_u.toString(16);
		}
		temporizador_u = setTimeout(mostrar_proxima, PAUSA);
	} else {
		temporizador_u = setTimeout(subir_u, 100);
	}
}

function descer_u() {
	var topo_atual = parseInt(lista_u.style.top);
	if(topo_atual == topo_minimo_u) {
		itens_u[1].style.color = "#" + COR_CLARA.toString(16) +
			COR_CLARA.toString(16) + COR_CLARA.toString(16);
		lista_u.style.top = topo_maximo_u + "px";
		ativo_u = tamanho_real_u + 1;
	}

	var topo = parseInt(lista_u.style.top) + PASSO;

	lista_u.style.top = topo + "px";
	cor_ativando_u -= PASSODE_COR;
	cor_desativando_u += PASSODE_COR;
	
	itens_u[ativo_u].style.color = "#" + cor_desativando_u.toString(16) +
		cor_desativando_u.toString(16) + cor_desativando_u.toString(16);
	itens_u[ativo_u - 1].style.color = "#" + cor_ativando_u.toString(16) +
		cor_ativando_u.toString(16) + cor_ativando_u.toString(16);

	if((topo - 10) % 20 == 0) {
		cor_ativando_u = COR_CLARA;
		cor_desativando_u = COR_ESCURA;

		ativo_u = ativo_u - 1;
		temporizador_u = setTimeout(mostrar_proxima, PAUSA);
	} else {
		temporizador_u = setTimeout(descer_u, 100);
	}
}

function ativar(indice_u) {
	itens_u[indice_u].className = "ativo";
}

function desativar(indice_u) {
	itens_u[indice_u].className = "";
}

function mostrar_proxima() {
	subir_u();
}

function mostrar_anterior() {
	descer_u();
}


