/**
* @author: Michael Smith
* fades an object from 'start' to 'end' alpha levels at a certain 'rate' of change and a certain 'delay'
*/
function fade(id, start, end, rate, delay)
{
	var obj = getLyr(id);
	var current = start;
	var recursive = false;
	if (start > end)
	{
		current -= rate;
		if (current <= 0)
		{
			current = 0;
		}
		else 
		{
			if (current < end)
				current = end;
			else
				recursive = true;	
		}
	}
	else if (start < end)
	{	
		current += rate;
		if (current >= 100)
		{
			current = 100;
		}
		else 
		{
			if (current > end)
				current = end;
			else
				recursive = true;	
		}
	}
	else
		return;
	obj.alpha(current);
	if (recursive)
	{
		var newcall = "fade('" + id +"'," + current + "," + end + "," + rate + "," + delay + ")";
		setTimeout(newcall, delay);
	}
}

/**
* @author Michael Smith
* fades an object from 100 to 0 alpha and then sets it invisible
*/
function fadeOut(id)
{
	var rate = 20;
	var delay = 3;
	fade(id, 100, 0, rate, delay);
	setTimeout("getLyr('" + id + "').vis('hidden')",100 + delay);
	setTimeout("getLyr('" + id + "').sty.display='none'",100 + delay); // hack for Mac FF 2
}
function fadeIn(id)
{
	var rate = 20;
	var delay = 3;
	getLyr(id).sty.display = ''; // hack for Mac FF 2
	getLyr(id).vis('visible');
	fade(id, 0, 200, rate, delay);
}

/**
 * performs action smoothly
 * - user-defined action
 * - accelerates and decellerates based on decay
 * - based on expanded polynomials (using binomial coefficients 1,3,3,1)
 * @param {Object} fun function to execute with a factor of the result
 * @param {Float} n current progress (also used for starting value)
 * @param {Float} scale multiplier to apply to progress
 * @param {Float} rate how quickly the action progresses to completion
 * @param {Float} decay proportion of total time spent accelerating or decellerating
 * @param {Integer} delay in milliseconds before proceeding
 * @param {boolean} execute function with the change from prior value rather than actual value
 * @author Michael Smith
 */
function smoothAction(fun, n, scale, rate, decay, delay, nPrior)
{
	var m = (1 - n);
	var x = (0) + 3*decay*m*m*n + 3*(1-decay)*m*n*n + n*n*n;
	var next = false;
	// increasing or decreasing rate?
	if (rate > 0)
	{
		// check for start or end out of range 
		if (n >= 1) 
			x = 1;
		else 
			next = true;
	}
	else if (rate <= 0) // decreasing or no acceleration or deceleration
	{
		// check for start or end out of range 
		if (n < 0 || rate == 0) 
			x = 0;
		else
			next = true;
	}
	var nP = false;
	var nAdjust = 0;
	if (nPrior == undefined || nPrior === false) 
	{
	// do nothing
	}
	else if (nPrior === true || nPrior >= 0) 
	{
		nP = x;
		if (nPrior === true) 
		{
			// do nothing
		}
		else 
		{
			nAdjust = nPrior;
		}
	}
//	alert(nP);
//	alert(x-nAdjust);
	
	eval(fun + '(' + Math.round(scale*(x-nAdjust)) + ')');
	if (next)
	{
		n += rate;
		setTimeout(function() { smoothAction(fun, n, scale, rate, decay, delay, nP); }, delay);
	}
	else
	{
		clearTimeout();
	}
}
// testing
//smoothAction('alert', 0, 0.1, 0.2, 50);

/**
* @author Angus Turnbull
*/
// Example four-point animation
// Loop the loop, don't panic about the formulae, we're just calling mL.x and .y.
function bezierAnim(id, n)
{
	var obj = getLyr(id);
	n += 0.02;
	var m = (1 - n);
	var x1 = 500, x2 = 350, x3 = 0, x4 = 100;
	var y1 = 300; y2 = 400; y3 = 200; y4 = 100;

	// Bezier curve formulae. These are basically expanded polynomials... use binomial
	// coefficients like 1,2,1 or 1,4,6,4,1 etc. if you want to change the number of points.

	nX = x1*m*m*m + 3*x2*m*m*n + 3*x3*m*n*n + x4*n*n*n;
	nY = y1*m*m*m + 3*y2*m*m*n + 3*y3*m*n*n + y4*n*n*n;
	obj.x(nX); obj.y(nY);
	document.getElementById('statLyr').innerHTML = ("X = " + nX);

	if (n <= 1) setTimeout("bezierAnim('" + id + "'," + n + ")", 5);
}
