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

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

WaveTextEffect.DEFAULT_AMPLITUDE = 10;

/**
 * Constructor
 * @param interval the animation delay in ms
 */
function WaveTextEffect(interval, amplitude)
{
	this.initWaveTextEffect(interval, amplitude);
}

WaveTextEffect.prototype.initWaveTextEffect = function(interval, amplitude)
{
	this.initAbstractTextEffect(interval);
	this.m_amplitude = (amplitude != null) ? amplitude : WaveTextEffect.DEFAULT_AMPLITUDE;
	this.m_currentPart = -1;
}

WaveTextEffect.prototype.initState = function()
{
	this.m_currentPart = 0;
}

WaveTextEffect.prototype.doStart = function(target)
{
	this.initState();
	this.startLoop();
}

WaveTextEffect.prototype.doStep = function()
{
	// units are in PI radians
	var PHASE_OFFSET = -1;
	for (var i = 0; i < this.m_parts.length; i++)
	{
		var animationPhase = (this.m_parts.length - this.m_currentPart) / this.m_parts.length * 2;
		var phase = animationPhase + PHASE_OFFSET;
		var xIn = i / this.m_parts.length * 2 + phase;

		if (xIn < -1)
		{
			xIn = -1;
		}
		if (xIn > 1)
		{
			xIn = 1;
		}
		
		var yOut = Math.round(this.m_amplitude * Math.sin(xIn * Math.PI));
		
// zipper effect
//		if (i % 2 > 0)
//		{
//			yOut = -yOut;
//		}
		
		// transform for screen coordinates
		yOut = -yOut;
		
		this.m_parts[i].style.position = "relative";
		this.m_parts[i].style.top = yOut + "px";
//		Debug.write(-yOut + " ");
	}
//	Debug.writeln();
	
	this.m_currentPart = (this.m_currentPart + 1) % (this.m_parts.length * 2);
};

WaveTextEffect.prototype.doFinish = function()
{
	this.stopLoop();
	for (var i = 0; i < this.m_parts.length; i++)
	{
		this.m_parts[i].style.position = "";
		this.m_parts[i].style.top = "";
	}
};

