var text;

function text_effect(name,id,max_chars)// 'name' must be the name of created object, 'id' is the id of marquee element,
{											// 'mas_chars' is the max number of characters that should show in elemen(if not set,
											// it will estimate this number according to element width,
											// the content of all element's childs will show one by one	
	this.elem = document.getElementById(id);
	this.texts = new Array();
	for( i = 0; i<this.elem.childNodes.length;i++)
	{
		if(this.elem.childNodes[i].nodeType == 1 && this.elem.childNodes[i].innerHTML)
		{
			this.texts.push(this.elem.childNodes[i].innerHTML);
		}
		else if(this.elem.childNodes[i].nodeType == 3 && this.elem.childNodes[i].nodeValue)
			this.texts.push(this.elem.childNodes[i].nodeValue);
	}
	this.elem.innerHTML = "";
	this.max_chars = max_chars;
	if(this.max_chars == null)
		this.max_chars = this.elem.offsetWidth/7;
	this.elem.style.overflow = "hidden";
	this.finish_index = 0;
	this.start_index = 0;
	this.current_index = 0;//represent current text index in the array of texts
	this.timer;
	this.name = name;
	this.delay_between_texts = 1000;
	this.delay_between_chars = 120;
	this.start = function(effect)// the options for effect is 1 or 2
	{
		if(this.texts.length == 0)
			return;
		this.stop();
		switch (effect)
		{
			case 1:
				this.effect1_recursive();
				break;
			case 2:
				this.effect2_recursive();
				break;
			default:
				this.effect1_recursive();	
		}	
		return;
	}
	this.effect1_recursive = function()// this function is not originaly a public function and should not call from outside
	{
		
		this.elem.innerHTML = this.texts[this.current_index].substring(this.start_index,this.finish_index)
		this.finish_index++;
		if(this.finish_index - this.start_index >this.max_chars)
			this.start_index++;
		if (this.finish_index >= this.texts[this.current_index].length) 
		{
			this.finish_index = 0;
			this.start_index = 0;
			this.current_index++;
			if(this.current_index >= this.texts.length)
				this.current_index = 0;
			this.timer = setTimeout(this.name + '.effect1_recursive()', this.delay_between_texts);
			return;
		}
		this.timer = setTimeout(this.name +'.effect1_recursive()',this.delay_between_chars);
	}
	this.stop = function()
	{
		this.elem.innerHTML = this.texts[this.current_index].substring(this.start_index,this.max_chars);
		if(this.timer)
			clearTimeout(this.timer);
	}
	this.effect2_recursive = function()
	{
		this.elem.innerHTML = this.texts[this.current_index].substring(this.start_index,this.finish_index);
		var tmp = document.createElement("span");
		tmp.style.textDecoration = "underline";
		tmp.style.color = "#aaaaaa";
		//tmp.style.fontSize = "22px";
		//tmp.style.backgroundColor = "#ffeeee";
		tmp.innerHTML = this.texts[this.current_index].substring(this.finish_index,this.finish_index+1);
		this.elem.appendChild(tmp);
		this.finish_index++;
		if(this.finish_index - this.start_index > this.max_chars)
			this.start_index++;
		if (this.finish_index + 1 >= this.texts[this.current_index].length) 
		{
			this.finish_index = 0;
			this.elem.innerHTML = this.texts[this.current_index].substring(this.start_index,this.texts[this.current_index].length);	
			this.start_index = 0;
			this.timer = setTimeout(this.name + '.effect2_recursive()', this.delay_between_texts);
			this.current_index++;
			if(this.current_index >= this.texts.length)
				this.current_index = 0;
			return;
		}
		this.timer = setTimeout(this.name +'.effect2_recursive()',this.delay_between_chars);
	}

}
function init_marquee()
{
	text = new text_effect("text","marquee",38 );
	text.start(2);// start with effect1
}