// initialises global collection oNewsHeadlines, then calls LoopNewsHeadlines to display news headlines
function InitNewsTicker() {
	if(!document.getElementById("newsTicker")) return;
	Ptr("newsHeadlines");
	Ptr("newsTicker");
	Ptr("newsBlinker");
	Ptr("divNews");
	
	// store array of headlines in oNewsHeadlines
	window.oNewsHeadlines = [];
	var oDivs = newsHeadlines.getElementsByTagName("div");
	for(var i=0; i<oDivs.length; i++) if(oDivs[i] && oDivs[i].getAttribute("url")) oNewsHeadlines.push(oDivs[i]);

	// if at least one headline, begin looping
	if(oNewsHeadlines.length > 0) {
		setTimeout("LoopNewsHeadlines()", 4000);
		InitNewsBlinker();
	}
}

// loops through headlines in global collection oNewsHeadlines, displaying each via PushTickerChars (typewriter effect)
function LoopNewsHeadlines() {
	if(typeof oNewsHeadlines.iCurrIndex == "undefined")
		oNewsHeadlines.iCurrIndex = 0;
	else
		oNewsHeadlines.iCurrIndex++;			// move to next headline

	// loop back to first headline if reached last one
	if(oNewsHeadlines.iCurrIndex >= oNewsHeadlines.length) oNewsHeadlines.iCurrIndex = 0;
	
	// clear current headline and show newsBlinker
	newsTicker.innerHTML = "";
	newsTicker.href = oNewsHeadlines[oNewsHeadlines.iCurrIndex].getAttribute("url");
	divNews.style.visibility = "visible";
	newsBlinker.Start();

	// after 1 sec, display current headline via PushTickerChars
	setTimeout("newsBlinker.Stop(true); PushTickerChars(\""+ oNewsHeadlines[oNewsHeadlines.iCurrIndex].innerHTML + " [more]\")", 1000);
}

// adds the specified characters to the news ticker, typewriter style
function PushTickerChars(sText) {
	if(typeof sText == "string") {
		newsTicker.innerHTML += sText.substr(0,1);
		if(sText.length > 1)
			setTimeout("PushTickerChars(\""+ sText.substr(1) +"\")", 20);
		else {
			newsBlinker.Start();														// start blinker once all chars written
			setTimeout("LoopNewsHeadlines()", 6000);				// next headline after 7 secs
		}
	}
}

// turns newsBlinker SPAN into controllable DHTML object with methods
function InitNewsBlinker() {
	newsBlinker.BLINK_TIME = 200;
	newsBlinker.MAX_BLINKS = 8;

	newsBlinker.iBlinkCount = 0;
	newsBlinker.timer = null;

	newsBlinker.Start = function() {
		this.iBlinkCount = 0;
		if(!this.timer) {
			this.Blink();
			this.timer = window.setInterval("newsBlinker.Blink()", this.BLINK_TIME);
		}
	}
	newsBlinker.Blink = function() {
		if(this.bVisible) this.Hide(); else this.Show();
		if(++this.iBlinkCount == this.MAX_BLINKS) this.Stop();
	}
	newsBlinker.Stop = function(bLeaveVisible) {
		window.clearInterval(this.timer);
		this.timer = null;
		this.iBlinkCount = 0;
		if(bLeaveVisible) this.Show(); else this.Hide();
	}
	newsBlinker.Show = function() { this.style.visibility = "visible"; this.bVisible = true; }
	newsBlinker.Hide = function() { this.style.visibility = "hidden"; this.bVisible = false; }

	newsBlinker.Hide();
}
