var list; // global list variable cache
var tickerObj; // global tickerObj cache
var opacity = 0;
var interval = 10; // interval in seconds
var fadeSpeed = 15; // fade speed, the lower the speed the faster the fade.  40 is normal.
var alerted;

function fadeIn(divId) {
	if(tickerObj) {
		for (i = 0; i <= 100; i++) {
			setTimeout("changeOpac(" + i + ",'" + divId + "')", fadeSpeed * i);
		}
	}
}

function fadeOut(divId) {
	var timer = 0;
	if(tickerObj) {
		for (i = 100; i >= 0; i--) {
			timer++;
			setTimeout("changeOpac(" + i + ",'" + divId + "')", (timer) * (fadeSpeed/2));
		}
	}
}

function changeOpac(opacity, id) {
	if (opacity > 100) {opacity = 100;}
	var object = document.getElementById(id).style;

	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity="+opacity+")";
}

function initialiseList(divId) {
  tickerObj = document.getElementById(divId);
  if (!tickerObj) {
  // reportError("Could not find a div element with id \"" + divId + "\"");
 	 return;
  }
  list = tickerObj.childNodes;
  if(list.length <= 0)
    reportError("The div element \"" + divId + "\" does not have any children");
  for (var i=0; i<list.length; i++) {
    var node = list[i];;
    if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) 
        tickerObj.removeChild(node);
  }
  run(divId, 0);
}


function run(divId, count){
	changeOpac(0, divId);
	list[count].style.display = "block";
	fadeIn(divId);
	setTimeout("fadeOut('" + divId + "')", ((interval-1)*1000));
	if (count > 0) {
		list[count - 1].style.display = "none";
	}
	else {
		list[list.length - 1].style.display = "none";
	}
	count++;
	if (count == list.length) {
		count = 0;
	}

	window.setTimeout("run('" + divId + "', " + count+ ")", interval*1000);
}
function reportError(error) {
  alert("The script could not run because you have errors:\n\n" + error);
  return false;
}

String.prototype.repeat = function(l){
	return new Array(l+1).join(this);
};

window.onload = function(){
	initialiseList('testitext');
};
