/*  Script to create a scrolling text news box. Takes teh news you give it and scrolls
    those items across the screen for your users to see

    Based on newsticker by www.fczbkk.sl/js/newsticker/

*/


// run init when the window loads.....
addEvent(window, "load", init);

var news;
var tick;

function init() {
    if (document.getElementById) {
        tck = document.getElementById("all");
        if (tck.getElementsByTagName("div").length > 0) {
            actual = 0;
            step = 2;
            speed = 15;
            delay = 1000;
            news = new Array();
            // build an array of news - eg every seperate div provided in the div with
            // id = "all".
            for (i = 0; i < tck.getElementsByTagName("div").length; i++) {
                news[i] = tck.getElementsByTagName("div")[i];
                news[i].style.left = "600px";//tck.offsetWidth;
            }
            // start the news rolling ....
            rollNews();
            // add listeners for when mouse goes over tck to stop and when it leaves
            // tck to start again
            addEvent(tck, "mouseover", stopNews);
            addEvent(tck, "mouseout", rollNews);
        }
    }
}

function rollNews() {
    // move left edge to left a bit
    news[actual].style.left = parseInt(news[actual].style.left) - step + "px";

    if (parseInt(news[actual].style.left) == tck.offsetWidth % step) {
        // if that movement hasnt taken us off the edge of the div then wait
        // a bit and move it again.
        tick = setTimeout("rollNews()",delay);
    }
    else {
        // if it has taken us over the edge then move to the next item in news array
        if (parseInt(news[actual].style.left) <= 0-news[actual].offsetWidth) {
            actual++;
            // if at end of array then knock it back to start
            if (actual == news.length) {actual = 0;}
            news[actual].style.left = "600px";//tck.offsetWidth;
        }
        // wait a bit and try again.
        tick = setTimeout("rollNews()",speed);
    }
}

function stopNews() {
    clearTimeout(tick);
}

function addEvent(obj, evType, fn){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    return false;
  }
}

