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

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

GlintTextEffect.DEFAULT_EFFECT_TIME = 333;
GlintTextEffect.DEFAULT_AMPLITUDE = 3;
GlintTextEffect.DEFAULT_BACKGROUND = "#0064C0";
GlintTextEffect.DEFAULT_HIGHLIGHT = "#FFFFBF";

/**
 * Constructor
 * @param interval the animation delay in ms
 */
function GlintTextEffect(effectTime, amplitude, background, highlight)
{
	this.initGlintTextEffect(effectTime, amplitude, background, highlight);
}

GlintTextEffect.prototype.initGlintTextEffect = function(effectTime, amplitude, background, highlight)
{
	if (effectTime == null)
	{
		effectTime = GlintTextEffect.DEFAULT_EFFECT_TIME;
	}
	if (amplitude == null)
	{
		amplitude = GlintTextEffect.DEFAULT_AMPLITUDE;
	}
	if (background == null)
	{
		background = GlintTextEffect.DEFAULT_BACKGROUND;
	}
	if (highlight == null)
	{
		highlight = GlintTextEffect.DEFAULT_HIGHLIGHT;
	}
	this.initAbstractTextEffect(-1);
	this.m_effectTime = effectTime;
	this.m_amplitude = amplitude;
	this.m_currentPart = -1;
	this.m_backgroundString = background;
	this.m_background = ColorTools.parseColor(background);
	this.m_highlight = ColorTools.parseColor(highlight);
};

GlintTextEffect.prototype.initState = function()
{
	this.m_currentPart = 0;
	this.setInterval(Math.round(this.m_effectTime / this.m_parts.length));
};

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

GlintTextEffect.prototype.doStep = function()
{
	if (this.m_currentPart == this.m_parts.length * 2)
	{
		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.color = this.m_backgroundString;
		}
		return;
	}
	
	// units are in PI radians
	var PHASE_OFFSET = 0;
	for (var i = 0; i < this.m_parts.length; i++)
	{
		var animationPhase = (this.m_parts.length - this.m_currentPart) / this.m_parts.length * 1;
		var phase = animationPhase + PHASE_OFFSET;
		var xIn = i / this.m_parts.length * 1 + phase;

		if (xIn < 0)
		{
			xIn = 0;
		}
		if (xIn > 1)
		{
			xIn = 1;
		}
		
		var y = Math.sin(xIn * Math.PI);
		var yOut = Math.round(this.m_amplitude * Math.pow(y, 1));
		
//		Debug.write(yOut + " ");
		
		// wave effect
		this.m_parts[i].style.position = "relative";
		this.m_parts[i].style.top = -yOut + "px"; // transform for screen coordinates
		
		// really nauseating fisheye effect
//		this.m_parts[i].style.fontSize = (100 + yOut) + "%"; // transform for screen coordinates

		var color = this.m_backgroundString;
		var ambientColorFactor = Math.sqrt(y) / 2;
		var glintColorFactor = Math.pow(y - 0.05, 10);
		var colorFactor = Math.min(1.0, ambientColorFactor + glintColorFactor);
//		Debug.write(colorFactor + " ");
		if (colorFactor > 0)
		{
			var colorArray = ColorTools.blendColorsAt(colorFactor, this.m_background, this.m_highlight);
			color = ColorTools.toHex(colorArray);
		}
		this.m_parts[i].style.color = color;
	}
//	Debug.writeln();
	
	this.m_currentPart = (this.m_currentPart + 1);
};

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