var list = new Array(); // global list variable cache
var tickerObj = new Array(); // global tickerObj cache
var hex = new Array();
var counters = new Array();
var timerIDs = new Array();
var ccount = new Array();

function fadeText(divId) {
  if(tickerObj[divId])
  {
    if(hex[divId]>0) {
      hex[divId]-=5; // increase color darkness
      tickerObj[divId].style.color="rgb("+hex[divId]+","+hex[divId]+","+hex[divId]+")";
      setTimeout("fadeText('" + divId + "')", fadeSpeed); 
    } else
      hex[divId]=255; //reset hex value
  }
}

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

function run(divId, count) {
  hex[divId] = 255;
  if(list[divId].length == 1)
  {
  	list[divId][0].style.display = "block";
  	//alert(list[divId][0].innerHTML);
  	return;
  }
  fadeText(divId);
  list[divId][count].style.display = "block";
  ccount[divId] = count;
  if(count > 0)
    list[divId][count-1].style.display = "none";
  else
    list[divId][list[divId].length-1].style.display = "none";
  count++;
  if(count == list[divId].length)
    count = 0;
  counters[divId] = count;
  //if(count == 0) return;
  //alert(list[divId].length);
  timerIDs[divId] = window.setTimeout("run('" + divId + "', " + count+ ")", interval*1000);
}

function jumpNext(divId)
{
	clearTimeout(timerIDs[divId]);
	run(divId,counters[divId]);
}

function jumpPrev(divId)
{
	clearTimeout(timerIDs[divId]);
	var count = counters[divId];
	if(count > 0)
		list[divId][count-1].style.display = "none";
	else
	    list[divId][list[divId].length-1].style.display = "none";
	ccount[divId]--;
	if(ccount[divId] < 0)
		ccount[divId] = list[divId].length-1;
	//alert("Running #" + counters[divId]);
	run(divId,ccount[divId]);
}

function reportError(error) {
  alert("The script could not run because you have errors:\n\n" + error);
  return false;
}

var interval = 10; // interval in seconds
var fadeSpeed = 40; // fade speed, the lower the speed the faster the fade.  40 is normal.


