/* 	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);

function init() {
	if (document.getElementById) {
		tck = document.getElementById("newsDiv");
		tck.style.display = "";
		if (tck.getElementsByTagName("div").length > 0) {
			actual = 0;
			step = 2;
			speed = 70;
			delay = 0;
			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.top = tck.offsetHeight;
			}

			// start the news rolling ....
			rollNewsHorizontal();
			// 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", rollNewsHorizontal);
		}
	}
//        initAd();
}

function rollNewsHorizontal() {

	// move left edge to left a bit
	news[actual].style.top = parseInt(news[actual].style.top) - step + "px";

	if (parseInt(news[actual].style.height) == tck.offsetHeight % step) {
		// if that movement hasnt taken us off the edge of the div then wait
		// a bit and move it again.
		tick = setTimeout("rollNewsHorizontal()",delay);
	}
	else {
		// if it has taken us over the edge then move to the next item in news array
		if (parseInt(news[actual].style.top) <= 0-news[actual].offsetHeight ) {
			actual++;
			// if at end of array then knock it back to start
			if (actual == news.length) {actual = 0;}
			news[actual].style.top = tck.offsetHeight;
		}
		// wait a bit and try again.
		tick = setTimeout("rollNewsHorizontal()",speed);
	}
}

function initAd() {
	if (document.getElementById) {
		tck2 = document.getElementById("AdRow");
		tck2.style.display = "";
		if (tck2.getElementsByTagName("div").length > 0) {
			actual2 = 0;
			step2 = 2;
			speed2 = 50;
			delay2 = 0;
			news2 = new Array();
			// build an array of news - eg every seperate div provided in the div with
			// id = "all". 
			for (x = 0; x < tck2.getElementsByTagName("div").length; x++) {
				news2[x] = tck2.getElementsByTagName("div")[x];
				news2[x].style.left = -tck2.offsetWidth;
			}

			// start the news rolling ....
			rollNewsVertical();
		}
	}
}

function rollNewsVertical() {
	// move left edge to left a bit
	news2[actual2].style.left = parseInt(news2[actual2].style.left) + step2 + "px";

	if (parseInt(news2[actual2].style.left) == tck2.offsetWidth % step2) {
		// if that movement hasnt taken us off the edge of the div then wait
		// a bit and move it again.
		tick2 = setTimeout("rollNewsVertical()",delay2);
	}
	else {
		// if it has taken us over the edge then move to the next item in news array
//		if (parseInt(news2[actual2].style.left) <= 0-news2[actual2].offsetWidth) {
		if (parseInt(news2[actual2].style.left) >= news2[actual2].offsetWidth) {
			actual2++;
			// if at end of array then knock it back to start
			if (actual2 == news2.length) {actual2 = 0;}
			news2[actual2].style.left = -tck2.offsetWidth;
		}
		// wait a bit and try again.
		tick2 = setTimeout("rollNewsVertical()",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;
  }
}
