// Constructor
function TextReel
	(ContainerId, TextLines, Speed, TickTime, SlowDownDistance, MinSlowdownSpeed, ChangeTextDelay)
{
	// Setup member variables.
	this.ContainerId = ContainerId;
	this.Speed = Speed;
	this.TickTime = TickTime;
	this.SlowDownDistance = SlowDownDistance;
	this.ChangeTextDelay = ChangeTextDelay;
	this.MinSlowdownSpeed = MinSlowdownSpeed;

	// Initialize Text reel.
	this.CurTextLineIdx = -1;
	this.CurTextPhraseIdx = -1;
	this.PosLimit = 0;
	this.TextReelObj = document.getElementById(this.ContainerId);
	if (this.TextReelObj == null)
		return; // Image reel not found - nothing to do.
	this.TextReelObj.style.overflow = "hidden";
	this.TextReelObj.style.clip = "rect(0px 0px 0px 0px)";

	if (TextLines == null || TextLines.length == 0)
		return; // TextLines in reel not found - nothing to do.
		
	// Prepare all objects at once.
	this.LinesObjs = new Array();
	for (i = 0; i < TextLines.length; i++)
	{
		var textLine = new Array();
		textLine = TextLines[i].split('|');
		var phrasesObjs = new Array();
		for (j = 0; j < textLine.length; j++)
		{
			var phrase = document.createElement('span');
			phrase.innerHTML = textLine[j] + '&nbsp;';
			phrase.style.position = "absolute";
			phrase.style.left = this.TextReelObj.offsetWidth.toString() + "px";
			phrase.style.top = "0px";
			phrase.style.display = "none";
			phrasesObjs[j] = phrase;
			this.TextReelObj.appendChild(phrase);
		}
		this.LinesObjs[i] = phrasesObjs;
	}
		
	// Start updates.
	this.NextLine();
}

TextReel.prototype.Tick = function ()
{
	var newPos = this.PosLimit;
	var curLine = this.LinesObjs[this.CurTextLineIdx];
	var curPhrase = null;
	if (this.CurTextPhraseIdx >= 0)
	{
		curPhrase = curLine[this.CurTextPhraseIdx];
		var Speed = this.Speed;
		var prevPos = parseInt(curPhrase.style.left);
		if (prevPos - this.PosLimit < this.SlowDownDistance)
		{	// Slow speed at the very end.
			Speed = Speed / (this.SlowDownDistance - (prevPos - this.PosLimit));
			Speed = Math.floor(Speed);
			if (Speed < this.MinSlowdownSpeed)
				Speed = this.MinSlowdownSpeed;
		}
				
		var newPos =  prevPos - Speed;
		if (newPos < this.PosLimit)
			newPos = this.PosLimit;

		curPhrase.style.left = newPos.toString() + "px";
	}
	
	if (newPos == this.PosLimit)
	{	// Move to the next phrase, if any left.
		if (curPhrase != null)
			this.PosLimit += curPhrase.offsetWidth;
	
		this.CurTextPhraseIdx += 1;
		if (this.CurTextPhraseIdx >= curLine.length)
		{	// Move to next line.
			window.clearInterval (this.interval);
			var thisObj = this;
			window.setTimeout(function() { thisObj.NextLine(); }, this.ChangeTextDelay);
			return;
		}
			
		var nextPhrase = curLine[this.CurTextPhraseIdx];
		if (nextPhrase == null)
		{
			window.clearInterval (this.interval);
			return;
		}
		
//		nextPhrase.style.left = this.TextReelObj.offsetWidth.toString() + "px";
		nextPhrase.style.display = "";
	}
}

TextReel.prototype.NextLine = function ()
{
	if (this.CurTextLineIdx >= 0)
	{
		var curLine = this.LinesObjs[this.CurTextLineIdx];
		for (i = 0; i < curLine.length; i++)
		{
			var curPhrase = curLine[i];
			curPhrase.style.display = "none";
			curPhrase.style.top = "0px";
			curPhrase.style.left = this.TextReelObj.offsetWidth.toString() + "px";
		}
	}

	this.CurTextLineIdx += 1;
	if (this.CurTextLineIdx >= this.LinesObjs.length)
		this.CurTextLineIdx = 0;

	this.CurTextPhraseIdx = -1;
	this.PosLimit = 0;

	var thisObj = this;
	this.interval = window.setInterval(function() { thisObj.Tick(); }, this.TickTime);
}

