var timerID = null;
var timerRunning = false;
var delay = 50;
var scrollpos = 0;
var boxsize = 0;
var refsize = 0;
//var bannerText = "";

function stopTimer()
{
    if(timerRunning && timerID != null) {
        clearTimeout(timerID);
    }
    timerRunning = false;
}

function startTimer()
{
    timerRunning = true;
    timerID = self.setTimeout("timerTick()", delay);
}

function timerTick()
{
    stopTimer(); //make sure it's off while we work...
    //work goes here
    var ref = document.getElementById("scrollref");
    var box = document.getElementById("scrollcanvas");
    var canvas = document.getElementById("scrollcanvas");

    if(refsize == 0) { //initialize global values
        boxsize = box.clientWidth;
        refsize = ref.clientWidth;
        //alert("box="+boxsize+" ref="+refsize);
        canvas.style.width = refsize+"px";
        scrollpos = boxsize;
        //canvas.innerHTML = bannerText;
        canvas.style.left = scrollpos+"px";
        canvas.style.visibility = "visible";
    }
    if(scrollpos <= -refsize) {
        scrollpos = boxsize;
        //alert("reset to "+boxsize);
    }
    scrollpos -= 1;
    canvas.style.left = scrollpos+"px";
    startTimer(); //then restart the timer for another iteration
}

function initializeBanner() {
    //if(bannerText.length > 147) {
    //    bannerText = bannerText.substring(0,147);
    //}
    //bannerText = bannerText + "...";
    //var ref = document.getElementById("scrollref");
    //ref.innerHTML = bannerText;
    //alert("INIT boxsize="+boxsize+" refsize="+refsize);
    //document.write("<br>test(initializeBanner)<br>");
    startTimer();
}

function terminateBanner() {
    stopTimer();
}
