// Set variables
var tickerArea;
var tickerText;
var hexColor = 0;
var maxColor = 170;
var fadeSpeed = 50;
var interval = 8000;

// Initial call to set up ticker contents
function runTicker(id) {
  // get element that will display the ticker texts
  tickerArea = document.getElementById(id);
  if(!tickerArea) {
    traceErr("No element with id \""+id+"\"");
  }
  // get all elements to display in the ticker area
  tickerText = tickerArea.childNodes;
  if(tickerText.length<=0) {
    traceErr("Ticker element \""+id+"\" is empty");
  }
  // cycle through elements
  for (var i=0; i<tickerText.length; i++) {
    var node = tickerText[i];
    if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) {
	  tickerArea.removeChild(node);
    }
  }
  // start the ticker
  run(id, 1);
}

// starts the actual fade/display process of each ticker text
function run(id, count) {
  // fade the text
  fadeIn(id);
  tickerText[count].style.display = "block";
  if(count>0) {
    tickerText[count-1].style.display = "none";
  } else {
    tickerText[tickerText.length-1].style.display = "none";
  }
  count++;
  if(count == tickerText.length) {
    count = 0;
  }
  window.setTimeout("run('"+id+"',"+count+")",interval);
}

// fade the text into view
function fadeIn(id) {
  if(tickerArea) {
    if(hexColor<maxColor) {
      hexColor+=5;
      tickerArea.style.color="rgb("+hexColor+","+hexColor+","+hexColor+")";
      setTimeout("fadeIn('"+id+"')", fadeSpeed); 
    } else {
      hexColor=0;
    }
  }
}


// fade the text out of view - not working yet
function fadeOut(id) {
  if(tickerArea) {
    if(hexColor<maxColor) {
      hexColor-=5;
      tickerArea.style.color="rgb("+hexColor+","+hexColor+","+hexColor+")";
      setTimeout("fadeOut('"+id+"')", fadeSpeed); 
    } else {
      hexColor=170;
    }
  }
}



// show any errors
function traceErr(err) {
  alert("error:\n" + err);
  return false;
}





