// Preloads images except the image in the id tag
function PreloadImages(array, id)
{
	var strImg = document.getElementById(id).src;
	var preloadedImages;
	for(var i = 0; i < array.length; i++)
	{
		if(strImg == array[i])
		{
			array.splice(i, 1);
			break;
		}
	}

	preloadedImages = new Array(array.length);
	for( var i = 0; i < array.length; i++)
	{
		preloadedImages[i] = new Image();
		preloadedImages[i].src = array[i];
	}
}

function ChangeImage(imgID, array, index )
{
	document.getElementById(imgID).src = array[index];
}

function ChangeText(id, array, index)
{
	document.getElementById(id).innerHTML = array[index];
}

var rotationArrays = new Array();
var maintainHeight = new Array();

function MaintainHeight(id, num, nothing)
{	
	if(maintainHeight[num] == null)
		maintainHeight[num] = 0;
	if(	document.getElementById(id).offsetHeight >= maintainHeight[num])
		maintainHeight[num] = document.getElementById(id).offsetHeight;
	else
		document.getElementById(id).style.height = Number(maintainHeight[num]);
}

function RotateObject(time, rotationType, num, f, id, array)
{
	var nRotatingObjects = (arguments.length - 3) / 3;
	rotationArrays[num] = new Array(nRotatingObjects);
	
	var arraylen = array.length;
	for(var i = 0; i < nRotatingObjects; i++)
	{	// undefined allows for maintainHeight to pass last largest height
		if(arguments[i*3 + 5].length && arguments[i*3 + 5].length != arraylen) 
		{
			document.writeln("<h1>All arrays must be the same length when using rotateObject</h1>");
			return;
		}	
	}
	
	var toCall = "RotateObject_R(time, rotationType, num";
	for(var i = 0; i < nRotatingObjects; i++)
		toCall += ", arguments[" + i + "*3 + 3], arguments[" + i + "*3 + 4]" + ", arguments[" + i + "*3 + 5]";
	toCall += ")";
	eval(toCall);
}

function RotateObject_R(time, rotationType, num, f, id, array)
{	
	var index = -1; // in case of sequence type
	var baseFIdArray =  3;
	
	for(var i = baseFIdArray; i < arguments.length; i += 3)
	{
		if(i + 4 == arguments.length)
		{
			index = arguments[i + 3];
			break;
		}
	}
	
	// Changing Index Value
	if(rotationType == "Sequence")
		index = ++index % array.length;
	else if(rotationType == "Random-Dynamic" || rotationType == "Random-Static")
		index = Math.floor(Math.random() * array.length);
	
	// Rotate Objects
	for(var i = baseFIdArray; i < arguments.length - 2; i += 3)
		eval(arguments[i] + "(arguments[i + 1], arguments[i + 2], index)");
	
	// Setting up callback to ChangeObject
	var functToCall = 	"RotateObject_R(" + time + ", '" +
						rotationType + "', " + num;
	for(var i = baseFIdArray, j = 0; i < arguments.length - 2; i += 3, j++)
	{
		functToCall += ", '" + arguments[i]; // add function
		functToCall += "', '" + arguments[i + 1] + "', "; // add id
		rotationArrays[num][j] = arguments[i+2]; // add array to global and add it
		functToCall += "rotationArrays[" + num + "][" + j + "]";
	}
	functToCall += ", " + index + ")";
	
	if(rotationType != "Random-Static")
		setTimeout( functToCall, time);
}
