var newsIndex = 0;						// index of current news item
var newsInterval = 8000;			// interval between news updates, in ms

var lengthInterval = 33;			// interval between text updates, in ms

// internal use
var data;

var newsTimer;
var newsElement;

var lengthTimer;
var lengthCurrent;


/* parse the news data from data element into internal variable
   data should be in JSON
   write an error message to the ticker if the data cannot pe parsed
*/
function loadNewsData() {
	if (data == null) {
		try {
			parsed = JSON.parse(document.getElementById('NewsTickerData').innerHTML);
			data = parsed.news;
			if (parsed.interval != null) {
				newsInterval = parsed.interval;
			}
			return true;
		} catch (e) {
			newsElement.innerHTML = "Error: " + e.name + ", " + e.message;
			return false;
		}
	}
	return true;
}


/* start the news ticker
*/
function startNewsTicker() {
	// stop ticker in case it is already running
	stopNewsTicker();
	
	// get link element to write news to
	newsElement = document.getElementById('NewsTickerItem');
	
	// parse the news data
	var result = loadNewsData();
	
	// set the refresh timer
	if (result) {
		showNews();
		if (data.length > 1) {
			newsTimer = setInterval("nextNews()", newsInterval);
		}
	}
}


/* stop the news ticker from updating (note: does not stop the length timer)
*/
function stopNewsTicker() {
	if (newsTimer != null) {
		clearInterval(newsTimer);
		newsTimer = null;
	}
}


/* start or stop the ticker as appropriate
*/
function toggleNewsTicker() {
	if(newsTimer != null) {
		stopNewsTicker();
	} else {
		newsIndex++;
		startNewsTicker();
	}
}

/* display the current news item
*/
function showNews() {
	// get the current news object
	var newsItem = data[newsIndex];
	
	// set the link and body of the ticker element
	if (newsItem != null && newsItem.id != null && newsItem.title != null) {
		newsElement.href = "news.php?id=" + newsItem.id.toString();
		startLengthIncrease();		// reset the width and start the width timer
	} else {
		newsElement.innerHTML = "There is no news to display at present";
		newsElement.href = "#";
	}
}


/* switch to a different news item
   wrap-around is handled here
*/
function changeNewsIndex(delta) {
	newsIndex += delta;
	if (newsIndex >= data.length) {
		newsIndex = 0;
	} else if (newsIndex < 0) {
		newsIndex = data.length - 1;
	}
	showNews();
}


/* switch to the next news item
*/
function nextNews() {
	changeNewsIndex(1);
}


/* switch to the pewvious news item
*/
function previousNews() {
	changeNewsIndex(-1);
}


/* start the ticker effect
*/
function startLengthIncrease() {
	if (lengthTimer != null) {
		clearInterval(lengthTimer);
		lengthTimer = null;
	}
	lengthCurrent = 0;
	lengthTimer = setInterval("lengthIncrease()", lengthInterval);
}


/* increase the length of text displayed in the ticker
*/
function lengthIncrease() {
	if (lengthCurrent >= data[newsIndex].title.length) {
		clearInterval(lengthTimer);
		lengthTimer = null;
	} else {
		lengthCurrent++;
		newsElement.innerHTML = data[newsIndex].title.substring(0, lengthCurrent);
	}
}



