//configure the below five variables to change the style of the scroller
var scrollerdelay='4000'; //delay between msg scrolls. 3000=3 seconds.
var scrollerwidth='550px';
var scrollerheight='29px';
var scrollerbgcolor='#005091';
//set below to '' if you don't wish to use a background image
var scrollerbackground='';

var ie=document.all;
var dom=document.getElementById;
var index;

var stopped = false;
var initialized = false;

var main_obj;
var first_obj;
var second_obj;

var news;
var timers = new Array(new Array(), new Array());

function move(thisdiv, otherdiv){
	if(stopped) return;
	tdiv = eval(thisdiv);
	odiv = eval(otherdiv);
	if (parseInt(tdiv.style.top)>0 && parseInt(tdiv.style.top)<=5 || !initialized){
		initialized = true;
		tdiv.style.top=0+"px";
		timers[(thisdiv=='first_obj')?0:1][0] = setTimeout("move('"+thisdiv+"', '"+otherdiv+"')", scrollerdelay);
		timers[(thisdiv=='first_obj')?0:1][1] = '"move(\''+thisdiv+'\', \''+otherdiv+'\')", scrollerdelay';
		timers[(otherdiv=='first_obj')?0:1][0] = setTimeout("move('"+otherdiv+"', '"+thisdiv+"')", scrollerdelay);
		timers[(otherdiv=='first_obj')?0:1][1] = '"move(\''+otherdiv+'\', \''+thisdiv+'\')", scrollerdelay';
		return;
	}
	if (parseInt(tdiv.style.top)>=tdiv.offsetHeight*-1){
		tdiv.style.top=parseInt(tdiv.style.top)-5+"px";
		timers[(thisdiv=='first_obj')?0:1][0] = setTimeout("move('"+thisdiv+"', '"+otherdiv+"')", 50);
		timers[(thisdiv=='first_obj')?0:1][1] = '"move(\''+thisdiv+'\', \''+otherdiv+'\')", 500';
	} else {
		tdiv.style.top=parseInt(scrollerheight)+"px";
		tdiv.innerHTML=news[index];
		if (index==news.length-1){
			index=0;
		} else {
			index++;
		}
	}
}

function startscroll(){
	main_obj=ie? main2 : document.getElementById("main2");
	first_obj=ie? first2 : document.getElementById("first2");
	second_obj=ie? second2 : document.getElementById("second2");

	news = splitText(messages, div_html_end);

	first_obj.innerHTML = news[0];
	second_obj.innerHTML = news[(news.length==1)? 0 : 1];

	if(news.length>2){
		index=2;
	} else {
		index=0;
	}

	main_obj.onmouseover = stopscroll;
	move('first_obj', 'second_obj');
	second_obj.style.top=scrollerheight;
	second_obj.style.visibility='visible';
}

function stopscroll(){
	if(stopped){ return; }
	stopped = true;
	for(i=0; i<timers.length; i++){
		clearTimeout(timers[i][0]);
	}
	main_obj.onmouseover = '';
	main_obj.onmouseout = restartscroll;
}

function restartscroll(){
	if(!stopped){ return; }
	stopped = false;
	for(i=0; i<timers.length; i++){
		timers[i][0] = eval('setTimeout('+timers[i][1]+');');
	}
	main_obj.onmouseover = stopscroll;
	main_obj.onmouseout = '';
}

if (ie||dom){
	document.writeln('<div id="main2" style="position:relative;width:'+scrollerwidth+';height:'+scrollerheight+';overflow:hidden;background-color:'+scrollerbgcolor+' ;background-image:url('+scrollerbackground+')">');
	document.writeln('<div style="position:absolute;width:'+scrollerwidth+';height:'+scrollerheight+';clip:rect(0 '+scrollerwidth+' '+scrollerheight+' 0);left:0px;top:0px">');
	document.writeln('<div id="first2" style="position:absolute;width:'+scrollerwidth+';left:0px;top:0px;">');
	document.write(messages[0]);
	document.writeln('</div>');
	document.writeln('<div id="second2" style="position:absolute;width:'+scrollerwidth+';left:0px;top:0px;visibility:hidden">');
	document.write(messages[dyndetermine=(messages.length==1)? 0 : 1]);
	document.writeln('</div>');
	document.writeln('</div>');
	document.writeln('</div>');
}

if (window.addEventListener){
	window.addEventListener("load", startscroll, false);
} else if (window.attachEvent){
	window.attachEvent("onload", startscroll);
} else if (ie||dom) {
	window.onload=startscroll;
}

function splitText(txtArray, endText){
	var div_obj = second_obj;
	rows = new Array();
	for(i=0; i<txtArray.length; i++){
		endPos = txtArray[i].length-endText.length;
		app = txtArray[i].substr(0, endPos);
		pos = app.lastIndexOf('>');
		startText = txtArray[i].substr(0, pos+1);
		div_obj.innerHTML = txtArray[i];
		if(div_obj.offsetHeight >= parseInt(scrollerheight)){
			str = txtArray[i].substr(startText.length, endPos-startText.length);
			words = str.split(' ');
			succ = startText;
			numWords=0;
			for(j=0; j<words.length; j++){
				if(words[j] == '') continue;
				prev = succ;
				succ += ((numWords==0)?'':' ') + words[j];
				numWords++;
				div_obj.innerHTML = succ + endText;
				if(div_obj.offsetHeight > parseInt(scrollerheight)){
					if(numWords > 1){
						rows[rows.length] = prev + endText;
						j--;
					} else {
						rows[rows.length] = succ + endText;
					}
					numWords = 0;
					succ = startText;
				}
			}
			if(numWords > 0){
				rows[rows.length] = succ + endText;
			}
		} else {
			rows[rows.length] = txtArray[i];
		}
	}
	return rows;
}
