/**
 * @author Diego
 */

var txtActual = "#txt01";
var imgActual = "#img01";
var animando = false;
var numAnimados = 0;
var actual = 0;
var opciones;
var timeoutID;
var tiempoPasar = 15000;

$(document).ready(function()
{
	controles();
});

function controles()
{
	opciones = $("#linksAplicaciones a").get();
	$("#linksAplicaciones a").click(mostrar);
	
	timeoutID = setTimeout(automatico,tiempoPasar);
}


function automatico()
{
	if (animando) { return; }
	if (actual == opciones.length-1) { $(opciones[0]).click(); }
	else { $(opciones[actual+1]).click(); }
	timeoutID = setTimeout(automatico,tiempoPasar);
}

function mostrar(e)
{
	e.preventDefault();
	var texto = $(this).attr("href");
	var imagen = "#img" + texto.substr(texto.length-2,2);
	
	if (((txtActual == texto)&&(imagen == imgActual))||(animando)) { return; }
	
	clearTimeout(timeoutID);
	delete(timeoutID);
	timeoutID = setTimeout(automatico,tiempoPasar); 
	
	for (var i=0, il=opciones.length; i<il; i++)
	{
		var elemento = opciones[i];
		if (elemento == $(this).get(0))
		{
			if (actual == i) { return; }
			else { actual = i; }  
		}	
	}
	
	$("#linksAplicaciones a").removeClass("actual");
	$(this).addClass("actual");
	
	muestraTexto(texto);
	muestraImagen(imagen);
}

function muestraTexto(texto)
{
	animando = true;
	$(txtActual).animate({"opacity":"0"}, 250,
		function()
		{
			$(texto).animate({"opacity":"1"}, 250, 
				function()
				{
					txtActual = texto;
					numAnimados++;
					if (numAnimados >= 2) { numAnimados = 0; animando = false; }					
				}
			);
		}
	);
}

function muestraImagen(imagen)
{
	animando = true;
	$(imagen).css("z-index","2");
	$(imagen).css("opacity","1");
	$(imgActual).animate({"opacity":"0"}, 500,
		function()
		{
			$(imagen).css("z-index","3");
			$(imgActual).css("z-index","2");
			imgActual = imagen;
			numAnimados++;
			if (numAnimados >= 2) { numAnimados = 0; animando = false; }	
		}
	)
}

