﻿/*
Variables globales
*/
var idIntervalo = new Array(1);
idIntervalo[0] = new Array(2);
idIntervalo[0][0] = '';

/*
Al principio pensé que todos los navegadores de internet manejarían la misma velocidad al dibujar,
pero después de una cantidad exhaustiva de pruebas, llegué a la conclusión que es muy poco preciso
y predecible esperar que los exploradores lleven una sincronía entre redimensionar el objeto y el
timeout para detener la acción; algunos navegadores redimensionan más rápido de lo que el timeout
llega al límite y otros al contrario.
*/
function animarCambioX (id, miliseg, cambio)
{
	var paso, i;
	for (i=0; i<idIntervalo.length; i++)
		if (idIntervalo[i][0] == '')
			break;
	if (i == idIntervalo.length)
	{
		idIntervalo.push('');
		idIntervalo[i] = new Array(2);
	}
	idIntervalo[i][0] = id;
	var objeto = document.getElementById(id);
	if (objeto.style.width == '')
		objeto.style.width = '0px';
	paso = objeto.style.width;
	if (cambio > parseInt(paso.slice(0,paso.length-2)))
	{
		if ((paso=miliseg/cambio)>=1)
			idIntervalo[i][1] = setInterval ('\
				var str = document.getElementById(\''+id+'\').style.width;\
				str = str.slice (0, str.length-2);\
				if (parseInt (str) < '+cambio+')\
					document.getElementById(\''+id+'\').style.width = (parseInt (str)+1)+\'px\';\
				else\
				{\
					clearInterval ('+idIntervalo[i][1]+');\
					idIntervalo['+i+'][0] = \'\';\
				}\
			', paso);
		else
		{
			paso=cambio/miliseg;
			idIntervalo[i][1] = setInterval ('\
				var str = document.getElementById(\''+id+'\').style.width;\
				str = str.slice (0, str.length-2);\
				if (parseInt (str) < '+cambio+')\
					document.getElementById(\''+id+'\').style.width = (parseInt (str)+'+paso+')+\'px\';\
				else\
				{\
					clearInterval ('+idIntervalo[i][1]+');\
					idIntervalo['+i+'][0] = \'\';\
				}\
			', 1);
		}
	}
	else if (cambio < parseInt(paso.slice(0,paso.length-2)))
	{
		if ((paso=miliseg/cambio)>=1)
			idIntervalo[i][1] = setInterval ('\
				var str = document.getElementById(\''+id+'\').style.width;\
				str = str.slice (0, str.length-2);\
				if (parseInt (str) > '+cambio+')\
					document.getElementById(\''+id+'\').style.width = (parseInt (str)-1)+\'px\';\
				else\
				{\
					clearInterval ('+idIntervalo[i][1]+');\
					idIntervalo['+i+'][0] = \'\';\
				}\
			', paso);
		else
		{
			paso=cambio/miliseg;
			idIntervalo[i][1] = setInterval ('\
				var str = document.getElementById(\''+id+'\').style.width;\
				str = str.slice (0, str.length-2);\
				if (parseInt (str) > '+cambio+')\
					document.getElementById(\''+id+'\').style.width = (parseInt (str)-'+paso+')+\'px\';\
				else\
				{\
					clearInterval ('+idIntervalo[i][1]+');\
					idIntervalo['+i+'][0] = \'\';\
				}\
			', 1);
		}
	}
}

function animarCambioY (id, miliseg, cambio)
{
	var paso, i;
	for (i=0; i<idIntervalo.length; i++)
		if (idIntervalo[i][0] == '')
			break;
	if (i == idIntervalo.length)
	{
		idIntervalo.push('');
		idIntervalo[i] = new Array(2);
	}
	idIntervalo[i][0] = id;
	var objeto = document.getElementById(id);
	if (objeto.style.height == '')
		objeto.style.height = '0px';
	paso = objeto.style.height;
	if (cambio > parseInt(paso.slice(0,paso.length-2)))
	{
		if ((paso=miliseg/cambio)>=1)
			idIntervalo[i][1] = setInterval ('\
				var str = document.getElementById(\''+id+'\').style.height;\
				str = str.slice (0, str.length-2);\
				if (parseInt (str) < '+cambio+')\
					document.getElementById(\''+id+'\').style.height = (parseInt (str)+1)+\'px\';\
				else\
				{\
					clearInterval ('+idIntervalo[i][1]+');\
					idIntervalo['+i+'][0] = \'\';\
				}\
			', paso);
		else
		{
			paso=cambio/miliseg;
			idIntervalo[i][1] = setInterval ('\
				var str = document.getElementById(\''+id+'\').style.height;\
				str = str.slice (0, str.length-2);\
				if (parseInt (str) < '+cambio+')\
					document.getElementById(\''+id+'\').style.height = (parseInt (str)+'+paso+')+\'px\';\
				else\
				{\
					clearInterval ('+idIntervalo[i][1]+');\
					idIntervalo['+i+'][0] = \'\';\
				}\
			', 1);
		}
	}
	else if (cambio < parseInt(paso.slice(0,paso.length-2)))
	{
		if ((paso=miliseg/cambio)>=1)
			idIntervalo[i][1] = setInterval ('\
				var str = document.getElementById(\''+id+'\').style.height;\
				str = str.slice (0, str.length-2);\
				if (parseInt (str) > '+cambio+')\
					document.getElementById(\''+id+'\').style.height = (parseInt (str)-1)+\'px\';\
				else\
				{\
					clearInterval ('+idIntervalo[i][1]+');\
					idIntervalo['+i+'][0] = \'\';\
				}\
			', paso);
		else
		{
			paso=cambio/miliseg;
			idIntervalo[i][1] = setInterval ('\
				var str = document.getElementById(\''+id+'\').style.height;\
				str = str.slice (0, str.length-2);\
				if (parseInt (str) > '+cambio+')\
					document.getElementById(\''+id+'\').style.height = (parseInt (str)-'+paso+')+\'px\';\
				else\
				{\
					clearInterval ('+idIntervalo[i][1]+');\
					idIntervalo['+i+'][0] = \'\';\
				}\
			', 1);
		}
	}
}

function cambiarColor (id, fondo, frente)
{
	document.getElementById(id).style.backgroundColor = fondo;
	document.getElementById(id).style.color = frente;
	document.getElementById(id).style.borderRightWidth = '0px';
}

function cambiarFondo (id, imagen)
{
	document.getElementById(id).style.backgroundImage = 'url('+imagen+')';
}

function cambiarFuenteColor (id, color)
{
	document.getElementById(id).style.color = color;
}

function cambiarFuenteTamanio (id, tamanio)
{
	document.getElementById(id).style.fontSize = tamanio+'px';
}

function cambiarImagen (id, origen)
{
	document.getElementById(id).src = origen;
}
function detenerAnimacion (id)
{
	var i;
	for (i=0; i<idIntervalo.length; i++)
		if (idIntervalo[i][0] == id) {
			clearInterval (idIntervalo[i][1]);
			idIntervalo[i][0] = '';
		}
}

function detenerAnimaciones ()
{
	var i;
	for (i=0; i<idIntervalo.length; i++) {
		clearInterval(idIntervalo[i][1]);
		idIntervalo[i][0]='';
	}
}

function irA (id, pagina)
{
	document.getElementById(id).src = pagina;
}

function reset (nombre, fondo, frente)
{
	var i;
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		var tableData = document.getElementsByTagName('td');
		var item = [];
		for (i=0; i<tableData.length; i++)
			if(tableData.item(i).getAttribute('name')==nombre)
				item.push(tableData.item(i));
	}
	else
		var item = document.getElementsByName(nombre);
	for (i=0; i<item.length; i++)
	{
		item[i].style.backgroundColor = fondo;
		item[i].style.color = frente;
		item[i].style.borderRightWidth = '1px';
	}
	/*
	Es posible escribir una sola rutina para todos los exploradores, pero se con
	serva la rutina con "document.getElemenstByName()" para recordar que in
	ternet Explorer no la tiene definida (no se puede usar) a pesar de ser el es
	tándar.
	*/
}

function tecla(evento)
{
	if(evento.keyCode)
		return evento.keyCode;
	else if(evento.which)
		return evento.which;
}
