/*
 * News ticker
 *
 * Populate "newsticker_news" and "newsticker_urls"
 */
var newsticker_chartimeout = 40;
var newsticker_itemtimeout = 5000;
var newsticker_id = "ticker";
var newsticker_prefix = "";             // Printed before
var newsticker_suffix = "";	            // Printed after

var newsticker_news = new Array();		// News item headlines
var newsticker_urls = new Array();		// News item URLs
var newsticker_current = 0;  			// Current news item index
var newsticker_curcount = 0; 			// Current input buffer position
var newsticker_element = null;

function newsticker_init() {
	// IE
	if(document.all) {
		newsticker_element = document.all[newsticker_id];
	}
	
	// DOM compatible
	if(document.getElementById) {
		newsticker_element = document.getElementById(newsticker_id);
	}
	
	if(newsticker_news.length > 0) {
	    newsticker_tick();
	}
}

function newsticker_tick() {
	var timeout = newsticker_chartimeout;
	
	if(newsticker_current >= newsticker_news.length) {
		newsticker_current = 0;
	}
	
	if(newsticker_curcount < newsticker_news[newsticker_current].length) {
		// Construct out buffer
		newsticker_curcount++;
		var newsticker_out = newsticker_news[newsticker_current].substring(0, newsticker_curcount);
		
		newsticker_out = newsticker_prefix +
						 "<a href=\"" + newsticker_urls[newsticker_current] +
						 "\">" + newsticker_out + "</a>" +
						 newsticker_suffix;
		
		// Write output buffer
		newsticker_write(newsticker_out);			
		
		timeout = newsticker_chartimeout;
	} else {
		// Go to next news item
		newsticker_current++;
		newsticker_curcount = 0;
		
		timeout = newsticker_itemtimeout;
	}
	
	setTimeout("newsticker_tick()", timeout);
}

function newsticker_write(tickertext) {
	if(newsticker_element != null) {
		newsticker_element.innerHTML = tickertext;
	}
}

function newsticker_add(headline, url) {
    if(headline.length > 0 && url.length > 0) {
        newsticker_news[newsticker_news.length] = headline;
        newsticker_urls[newsticker_urls.length] = url;
    }
}

