// JavaScript Document

/* Configuration */

var stepsPerSecond = 30;

/* Fields */

var fadingLayers = new Array();
var movingLayers = new Array();
var scalingLayers = new Array();

/* Functions */

function toLayer(id)
{
	if(document.getElementById)
	{
		// Firefox and Internet Explorer 5+
		return document.getElementById(id);
	}
	else if(document.all)
	{
		// Internet Explorer 4
		return document.all[id];
	}
	else if(document.layers)
	{
		// Navigator 4
		return document.layers[id];
	}
}

function toStyle(id)
{
	return toLayer(id).style;
}

function toggleLayer(id)
{
	var style = toStyle(id);
	if(style.display == 'none' || style.display == '')
	{
		style.display = 'block';
	}
	else
	{
		style.display = 'none';
	}
}

function showLayerOnly(id, ids)
{
	var style = toStyle(id);
	style.display = 'block';
	for(var i = 0; i < ids.length; i++)
	{
		if(String(ids[i]) !== String(id))
		{
			style = toStyle(ids[i]);
			style.display = 'none';
		}
	}
}

function showLayer(id)
{
	var style = toStyle(id);
	style.display = 'block';
}

function showLayers(ids)
{
	for(var i = 0; i < ids.length; i++)
	{
		style = toStyle(ids[i]);
		style.display = 'block';
	}
}
function hideLayer(id)
{
	var style = toStyle(id);
	style.display = 'none';
}

function hideLayers(ids)
{
	for(var i = 0; i < ids.length; i++)
	{
		style = toStyle(ids[i]);
		style.display = 'none';
	}
}

function setLayerOpacity(id, opactiy)
{
	var style = toStyle(id);
	style.opacity = opactiy;
	style.MozOpacity = opactiy;
	style.filter = 'alpha(opacity=' + (opactiy * 100) + ')';
	
	if(opactiy == 0)
	{
		style.display = 'none';
	}
	else
	{
		style.display = 'block';
	}
}

function setLayerPosition(id, x, y)
{
	var style = toStyle(id);
	style.marginLeft = x + 'px';
	style.marginTop = y + 'px';
}

function setLayerSize(id, w, h)
{
	var style = toStyle(id);
	if(w == 0 || h == 0)
	{
		style.display = 'none';
	}
	else
	{
		style.display = 'block';
		style.width = w + 'px';
		style.height = h + 'px';
	}
}

function lerp(a, b, t)
{
	return a + t * (b - a)
}

function fadeLayer(id, from, to, ms)
{
	var now = new Date();
	if(!fadingLayers[id])
	{
		// Begin animation, storing the interval id and a new step value
		fadingLayers[id] = new Array(
			setInterval(
				"fadeLayer('" + id + "', " + from + ", " + to + ", " + ms + ")",
				1000 / stepsPerSecond),
			now.getTime());
		setLayerOpacity(id, from);
	}
	else
	{
		if(now.getTime() > fadingLayers[id][1] + ms)
		{
			// Animation is complete
			clearInterval(fadingLayers[id][0]);
			fadingLayers[id] = null;
			setLayerOpacity(id, to);
		}
		else
		{
			// Continue animation
			t = (Math.cos(Math.PI - (((1.0 / ms) * (now.getTime() - fadingLayers[id][1])) * Math.PI)) + 1) * 0.5;
			setLayerOpacity(id, lerp(from, to, t));
		}
	}
}

function moveLayer(id, fromX, fromY, toX, toY, ms)
{
	var now = new Date();
	if(!movingLayers[id])
	{
		// Begin animation, storing the interval id and a new step value
		movingLayers[id] = new Array(
			setInterval(
				"moveLayer('" + id + "', " + fromX + ", " + fromY + ", " + toX + ", " + toY + ", " + ms + ")",
				1000 / stepsPerSecond),
			now.getTime());
		setLayerPosition(id, fromX, fromY);
	}
	else
	{
		if(now.getTime() > movingLayers[id][1] + ms)
		{
			// Animation is complete
			clearInterval(movingLayers[id][0]);
			movingLayers[id] = null;
			setLayerPosition(id, toX, toY);
		}
		else
		{
			// Continue animation
			t = (Math.cos(Math.PI - (((1.0 / ms) * (now.getTime() - movingLayers[id][1])) * Math.PI)) + 1) * 0.5;
			setLayerPosition(id, lerp(fromX, toX, t), lerp(fromY, toY, t));
		}
	}
}

function scaleLayer(id, fromW, fromH, toW, toH, ms)
{
	var now = new Date();
	if(!scalingLayers[id])
	{
		// Begin animation, storing the interval id and a new step value
		scalingLayers[id] = new Array(
			setInterval(
				"scaleLayer('" + id + "', " + fromW + ", " + fromH + ", " + toW + ", " + toH + ", " + ms + ")",
				1000 / stepsPerSecond),
			now.getTime());
		setLayerSize(id, fromW, fromH);
	}
	else
	{
		if(now.getTime() > scalingLayers[id][1] + ms)
		{
			// Animation is complete
			clearInterval(scalingLayers[id][0]);
			scalingLayers[id] = null;
			setLayerSize(id, toW, toH);
		}
		else
		{
			// Continue animation
			t = (Math.cos(Math.PI - (((1.0 / ms) * (now.getTime() - scalingLayers[id][1])) * Math.PI)) + 1) * 0.5;
			setLayerSize(id, lerp(fromW, toW, t), lerp(fromH, toH, t));
		}
	}
}
