/* 	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", initxx);

function initxx() {
	if (document.getElementById) {
		tck = document.getElementById("ticker1");
		if (tck.getElementsByTagName("div").length > 0) {
			step = 1;
			speedxx = 22;
			delay = 100;
			news = new Array();
			// build an array of news - eg every seperate div provided in the div with
			// id = "all". 
			j=tck.getElementsByTagName("div").length;
                       
                        for (i = 0; i < j; i++)
                        {

                                news[i] = tck.getElementsByTagName("div")[i];
                                news[i].style.left = 0;
                                //tck.offsetWidth;
                        }
                        
			// start the news rolling ....
			 actual = 0;
                        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) 
        {
		// 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) {refresh();actual=0;
                         news[actual].style.left = news[actual].offsetWidth;}
                        else
                        news[actual].style.left = tck.offsetWidth;
		}
		// wait a bit and try again.
		tick = setTimeout("rollNews()",speedxx);
    	}
}

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;
  }
}
