/**
 * Import EffectHandler.js first
 * (c) 2005 Mat Gessel
 */

/**
 * ExplodeTextEffect
 */
ExplodeTextEffect.prototype = new AbstractTextEffect();
ExplodeTextEffect.prototype.constructor = ExplodeTextEffect;

ExplodeTextEffect.DEFAULT_INTERVAL = 10;
ExplodeTextEffect.DEFAULT_RADIUS = 125;
ExplodeTextEffect.DEFAULT_STEPS = 10;

/**
 * Constructor
 * @param interval the animation delay in ms
 */
function ExplodeTextEffect(interval, steps, radius)
{
	this.initExplodeTextEffect(interval, steps, radius);
}

ExplodeTextEffect.prototype.initExplodeTextEffect = function(interval, steps, radius)
{
	if (interval == null)
	{
		interval = ExplodeTextEffect.DEFAULT_INTERVAL;
	}
	if (steps == null)
	{
		steps = ExplodeTextEffect.DEFAULT_STEPS;
	}
	if (radius == null)
	{
		radius = ExplodeTextEffect.DEFAULT_RADIUS;
	}
	this.initAbstractTextEffect(interval);
	this.m_steps = steps;
	this.m_radius = radius;
	this.m_step = -1;
	this.m_trajectories = null;
};

ExplodeTextEffect.prototype.initState = function()
{
	this.m_step = 0;
	if (this.m_trajectories == null)
	{
		this.m_trajectories = new Array(this.m_parts.length);
	}
	var arcLength = 1.0 / this.m_parts.length * 2;
	var sign = 1;
	var startAngle;
	for (var i = 0; i < this.m_parts.length; i++)
	{
		sign = (Math.random() >= 0.5) ? 1 : -1;
		startAngle = 1.0 - i / (this.m_parts.length - 1)
		this.m_trajectories[i] = sign * (startAngle + Math.random() * arcLength);
	}
};

ExplodeTextEffect.prototype.doStart = function(target)
{
	if (! this.isRunning())
	{
		this.initState();
		this.startLoop();
	}
};

ExplodeTextEffect.prototype.doStep = function()
{
	if (this.m_step == this.m_steps)
	{
		this.stopLoop();
		for (var i = 0; i < this.m_parts.length; i++)
		{
			this.m_parts[i].style.position = "";
			this.m_parts[i].style.top = "";
			this.m_parts[i].style.left = "";
			this.m_parts[i].style.opacity = "";
		}
		return;
	}
	
	var radius = Math.round(this.m_step / this.m_steps * this.m_radius);
	var opacity = 1.0 - this.m_step / (this.m_steps - 1);
	for (var i = 0; i < this.m_parts.length; i++)
	{
		this.m_parts[i].style.position = "relative";
		this.m_parts[i].style.top = Math.sin(this.m_trajectories[i] * Math.PI) * radius + "px";
		this.m_parts[i].style.left = Math.cos(this.m_trajectories[i] * Math.PI) * radius + "px";
//		this.m_parts[i].style.opacity = opacity;
//		this.m_parts[i].style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + Math.round(opacity * 100) + ")"; // stupid IE hack
	}
	this.m_step++;
};

ExplodeTextEffect.prototype.doFinish = function()
{
	// NOOP
};

