var g_stringScroller = null;

//	--------------------------------------------------------------------

function StringScroller(win, objectId)
{
	this.ok				= false;

	this.win			= win;
	this.doc			= win.document;

	this.efex			= "rightLeft";
	this.speed			= 45;
	this.steps			= 1;
	this.delay			= 100;

	this.pointer		= 0;
	this.string			= "default text to show some scrolling";
	this.message		= null;

	this.efexPool		= ["rightLeft", "leftRight", "topBottom", "bottomTop", "centerFlash", "cinema", "enigma", "matrix", "console"];
	this.signs 			= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	this.efxId			= null;
	this.dlyId 			= null;

	this.scroller		= this.createScroller(objectId);
	this.frame			= this.scroller.parentNode;

	if (this.scroller == null || this.frame == null)
		return;

	this.ok				= true;
	g_stringScroller	= this;
}

//	--------------------------------------------------------------------

StringScroller.prototype.setEfex = StringScroller_setEfex;

function StringScroller_setEfex(efex)
{
	for (var i = 0; i < this.efexPool.length; i++)
	{
		if (efex == this.efexPool[i])
		{
			this.efex = efex;
			return;
		}
	}
}

//	--------------------------------------------------------------------

StringScroller.prototype.setDelay = StringScroller_setDelay;

function StringScroller_setDelay(delay)
{
	this.delay = delay;
}

//	--------------------------------------------------------------------

StringScroller.prototype.setSpeed = StringScroller_setSpeed;

function StringScroller_setSpeed(speed, steps)
{
	this.speed 	= speed;
	this.steps	= steps;
}

//	--------------------------------------------------------------------
StringScroller.prototype.setColor = function(fgColor, bgColor) {
	var object = this.scroller;
	if (object != null) {
		object.style.color = fgColor;
		object.style.backgroundColor = bgColor;
		object.parentNode.style.color = fgColor;
		object.parentNode.style.backgroundColor = bgColor;
	}
}
//	--------------------------------------------------------------------
StringScroller.prototype.setFont = function(font) {
	var object = this.scroller;
	if (object != null) {
		object.style.fontFamily = font;
	}
}

//	--------------------------------------------------------------------

StringScroller.prototype.setString = StringScroller_setString;

function StringScroller_setString(data)
{
	if (typeof(data) == "string" || data == null)
	{
		this.message	= null;
		this.pointer	= 0;
		this.string 	= data;
	}
	else
	{
		this.message	= data;
		this.pointer	= this.message.length - 1;
		this.string 	= this.message[this.pointer];
	}
}

//	--------------------------------------------------------------------

StringScroller.prototype.reset = StringScroller_reset;

function StringScroller_reset(align, string)
{
	this.scroller.style.left	= "0px";
	this.scroller.style.top		= "0px";
	this.scroller.myText.data 	= string;

	this.frame.align			= align;
}

//	--------------------------------------------------------------------

StringScroller.prototype.stop = StringScroller_stop;

function StringScroller_stop()
{
	this.killTimeout();

	clearTimeout(this.dlyId);
	this.dlyId = null;
}

//	--------------------------------------------------------------------

StringScroller.prototype.runStop = StringScroller_runStop;

function StringScroller_runStop()
{
	if (this.efxId)
		this.stop();
	else
		this.start();
}

//	--------------------------------------------------------------------

StringScroller.prototype.start = StringScroller_start;

function StringScroller_start()
{
	this.triggerEfex();

	var exec	= "g_stringScroller.start();";
	this.dlyId 	= setTimeout(exec, this.delay);
}

//	--------------------------------------------------------------------

StringScroller.prototype.triggerEfex = StringScroller_triggerEfex;

function StringScroller_triggerEfex()
{
	if (this.efxId == null)
	{
		this.string = this.getNextString();

		switch(this.efex)
		{
			case "leftRight":
				this.reset("left", this.string);
				this.rightLeft(-this.scroller.offsetWidth, 1);
				break;

			case "rightLeft":
				this.reset("left", this.string);

				var width = -1;
				// undefined in published homepage for Safari browser
				if( "undefined" == typeof( this.frame.offsetWidth )) {
					width = parseInt( this.frame.style.width );
				} else {
					width = this.frame.offsetWidth;
				}

				this.rightLeft( width, -1 );
				break;

			case "topBottom":
				this.reset("center", this.string);
				this.upDown(-this.scroller.offsetHeight, 1);
				break;

			case "bottomTop":
				this.reset("center", this.string);
				this.upDown(this.scroller.offsetHeight, -1);
				break;

			case "centerFlash":
				this.reset("center", this.string);
				this.centerFlash(0, 10);
				break;

			case "cinema":
				this.reset("center", "");
				this.cinema(this.string.length / 2, 0);
				break;

			case "enigma":
				this.reset("left", "");
				this.enigma(0);
				break;

			case "matrix":
				this.memory = new Array(this.string.length);
				var	text	= "";

				for (var i = 0; i < this.string.length; i++)
				{
					this.memory[i] = i;
					text += "_";
				}

				this.reset("center", text);
				this.matrix();
				break;

			case "console":
				this.reset("left", "");
				this.console(0);
				break;
		}
	}
}

//	--------------------------------------------------------------------

StringScroller.prototype.rightLeft = StringScroller_rightLeft;

function StringScroller_rightLeft(position, direction)
{
	position += (this.steps * direction);

	var visible = true;

	if (direction < 0)
	{
		if (position < -this.scroller.offsetWidth)
			visible = false;
	}
	else if (direction > 0)
	{
		if (position > this.frame.offsetWidth)
			visible = false;
	}

	this.scroller.style.left = position + "px";
	var exec = "g_stringScroller.rightLeft(" + position + ", " + direction + ");";

	if (visible)
		this.initTimeout(exec);
	else
		this.killTimeout();

}

//	--------------------------------------------------------------------

StringScroller.prototype.upDown = StringScroller_upDown;

function StringScroller_upDown(position, direction)
{
	position += (this.steps * direction);

	var visible = true;

	if (direction < 0)
	{
		if (position < -this.frame.offsetHeight)
			visible = false;
	}
	else if (direction > 0)
	{
		if (position > this.frame.offsetHeight)
			visible = false;
	}

	this.scroller.style.top = position + "px";
	var exec = "g_stringScroller.upDown(" + position + ", " + direction + ");";

	if (visible)
		this.initTimeout(exec);
	else
		this.killTimeout();
}

//	--------------------------------------------------------------------

StringScroller.prototype.centerFlash = StringScroller_centerFlash;

function StringScroller_centerFlash(flag, count)
{
	flag = (flag == 1 ? 0 : 1);

	if (flag == 1)
	{
		count--;
		this.scroller.myText.data = this.string;
	}
	else
		this.scroller.myText.data = " ";

	var exec = "g_stringScroller.centerFlash(" + flag + "," + count +");";

	if (count > 0)
		this.initTimeout(exec);
	else
		this.killTimeout();
}

//	--------------------------------------------------------------------

StringScroller.prototype.cinema = StringScroller_cinema;

function StringScroller_cinema(center, position)
{
	if (position == 0)
		this.scroller.myText.data = this.string.substr(center, 1);
	else
	{
		var posLeft		= center - position;
		var posRight	= center + position;
		var	txtLeft		= "";
		var	txtRight	= "";

		if (posLeft >= 0)
			txtLeft = this.string.substr(posLeft, 1);

		if (posRight < this.string.length - 1)
			txtRight = this.string.substr(posRight, 1);

		this.scroller.myText.data = txtLeft + this.scroller.myText.data + txtRight;
	}

	position++;
	var exec = "g_stringScroller.cinema(" + center + ", " + position + ");";

	if (position <= center)
		this.initTimeout(exec);
	else
		this.killTimeout();
}

//	--------------------------------------------------------------------

StringScroller.prototype.enigma = StringScroller_enigma;

function StringScroller_enigma(position)
{
	var	text = this.string.substring(0, position);

	for (var i = position; i < this.string.length; i++)
		text += this.signs.charAt(Math.round(Math.random()* this.signs.length));

	this.scroller.myText.data = text;

	position++;
	var exec = "g_stringScroller.enigma(" + position + ");";

	if (position <= this.string.length)
		this.initTimeout(exec);
	else
		this.killTimeout();
}

//	--------------------------------------------------------------------

StringScroller.prototype.matrix = StringScroller_matrix;

function StringScroller_matrix()
{
	var	random		= Math.round(Math.random() * (this.memory.length - 1));
	var randomPos 	= this.memory[random];

	this.memory.splice(random, 1);

	var	firstSlice	= this.scroller.myText.data.substring(0, randomPos);
	var	lastSlice	= this.scroller.myText.data.substring(randomPos + 1, this.string.length);

	this.scroller.myText.data = firstSlice + this.string.charAt(randomPos) + lastSlice;

	var exec = "g_stringScroller.matrix();";

	if (this.memory.length > 0)
		this.initTimeout(exec);
	else
		this.killTimeout();
}

//	--------------------------------------------------------------------

StringScroller.prototype.console = StringScroller_console;

function StringScroller_console(position)
{
	var	text = this.string.substring(this.string.lenght - position, position + 1);

	this.scroller.myText.data = text + "_";

	position++;
	var exec = "g_stringScroller.console(" + position + ");";

	if (position < this.string.length)
		this.initTimeout(exec);
	else
		this.killTimeout();
}

//	--------------------------------------------------------------------

StringScroller.prototype.initTimeout = StringScroller_initTimeout;

function StringScroller_initTimeout(exec)
{
	this.killTimeout();
	this.efxId = setTimeout(exec, this.speed);
}

//	--------------------------------------------------------------------

StringScroller.prototype.killTimeout = StringScroller_killTimeout;

function StringScroller_killTimeout()
{
  	if (this.efxId)
		clearTimeout(this.efxId);

  	this.efxId	= null;
}

//	--------------------------------------------------------------------

StringScroller.prototype.getObject = StringScroller_getObject;

function StringScroller_getObject(id, object)
{
	var doc = null;

	if (object.document)
		doc = object.document;
	else
		doc = object;

	return (doc.getElementById) ? doc.getElementById(id) : doc.all(id);
}

//	--------------------------------------------------------------------

StringScroller.prototype.getNextString = StringScroller_getNextString;

function StringScroller_getNextString()
{
  	if (this.message != null)
	{
		if (this.pointer == 0)
  	  		this.pointer = this.message.length - 1;

		return this.message[this.pointer--];
	}
	else
		return this.string;
}

//	--------------------------------------------------------------------

StringScroller.prototype.createScroller = StringScroller_createScroller;

function StringScroller_createScroller(objectId)
{
	var	object = this.getObject(objectId, this.win);

	if (object != null)
	{
		// ensure non-empty text node for Safari browser
		var	text = this.doc.createTextNode(" ");

		object.appendChild(text);
		object.myText = text;

		object.style.position	= "relative";
		object.style.whiteSpace	= "nowrap";
	}

	return object;
}

//	--------------------------------------------------------------------
