window.onerror = null;
var message, current_msg, msg_length, msg_substring, font_color, msg_width, body_margin, new_margin, marquee_container, marquee_holder, marquee_txt, container_width, feed_array, do_once;

function run_news() {
	news_stream = new XMLHttpRequest();
	news_stream.open('GET', 'news.txt', true);
	news_stream.setRequestHeader('Content-type', 'text/xml');
	news_stream.onreadystatechange = function() {
		if (news_stream.readyState === 4) { // connection ready
			if (do_once != 'done') {
				do_once = 'done';
				if (news_stream.status === 200) { // file found, proceeding
					all_news = news_stream.responseText;
					feed_array = news_stream.responseText.split('\n');
					build_feed();
				}
			}
		}
	}
	news_stream.send(null);
}

function build_feed() {
	new_marquee = document.createElement('div');
	new_marquee.setAttribute('id', 'marquee_shell');
	new_marquee.setAttribute('style', 'position: absolute; top: 0px; left: 0px; width: 0px; background-color: #333333; border-bottom: solid 1px #000000; overflow: hidden; z-index: 1000;');
	new_marquee.innerHTML = '<div id="marquee_contents" style="position: absolute; top: 0px; left: 0px; color: #FFFFFF; text-align: left; text-overflow: ellipsis; white-space: nowrap;"><span id="marquee_msg">&nbsp;</span></div>';
	document.body.appendChild(new_marquee);

	message = feed_array[0];
	current_msg = 0;
	msg_length = 0;

	marquee_container = document.getElementById('marquee_shell');
	marquee_holder = document.getElementById('marquee_contents');
	marquee_txt = document.getElementById('marquee_msg');

	container_width = window.document.body.clientWidth;
	marquee_container.style.width = container_width+'px';
	marquee_holder.style.width = container_width+'px';
	marquee_container.style.height = (marquee_txt.offsetHeight+2)+'px';
	body_margin = document.body.style.marginTop;
	body_margin = body_margin.replace('px','');
	if (body_margin > 0) { new_margin = parseInt(marquee_txt.offsetHeight) + body_margin + 1; }
	else { new_margin = parseInt(marquee_txt.offsetHeight) + 1; }
	document.body.style.marginTop = new_margin+'px';

	prepare_msg();

	window.setTimeout('run_scroller()', 75);
	window.setInterval('reposition_scroller()', 40);
}

function run_scroller() {
	if (msg_length == 0) { marquee_holder.style.left = '0px'; }
	if (message.substring(msg_length - 1, msg_length) == '<') { msg_length = message.lastIndexOf('>') + 1; }
	msg_substring = message.substring(0, msg_length);
	marquee_holder.innerHTML = '<span id="marquee_msg" style="color: '+font_color+';">'+msg_substring+'</span>';
	msg_width = marquee_txt.offsetWidth;
	if (msg_width >= container_width) { marquee_holder.style.left = 0 - (msg_width - container_width)+'px'; }
	if (++msg_length > message.length) {
		msg_length = 0;
		if (current_msg >= (feed_array.length-1)) {
			message = feed_array[0];
			current_msg = 0;
		} else {
			current_msg += 1;
			message = feed_array[current_msg];
		}
		prepare_msg();
		window.setTimeout('run_scroller()', 5000);
	} else { window.setTimeout('run_scroller()', 75); }
}

function prepare_msg() {
	if (message.indexOf('GREEN:') > -1) {
		font_color = '#55DD55';
		message = message.replace('GREEN:', '');
	} else if (message.indexOf('BLUE:') > -1) {
		font_color = '#96ACFF';
		message = message.replace('BLUE:', '');
	} else if (message.indexOf('RED:') > -1) {
		font_color = '#E55555';
		message = message.replace('RED:', '');
	} else {
		font_color = '#FFFFFF';
	}
	message = message.replace('NEWS:', '<span style="font-style: italic; font-weight: bold;">Newsflash:</span>');
	message = message.replace('SPECIAL:', '<span style="font-style: italic; font-weight: bold;">Special Announcement:</span>');
	message = message.replace('SOON:', '<span style="font-style: italic; font-weight: bold;">Coming Soon!</span>');
}

function reposition_scroller() {
	if (navigator.appName == 'Microsoft Internet Explorer') { marquee_container.style.top = document.documentElement.scrollTop+'px'; }
	else { marquee_container.style.top = window.pageYOffset+'px'; }
}