// JavaScript Document

var NewsStories, xmlHttpNews;
var FeatureStories, xmlHttp;

// if (on==null) on=0;

function ShowNews() {
	if (!FeatureStories || !FeatureStories.length) return;
	on++;
	on %= FeatureStories.length;
	MM_setTextOfLayer('newsText','',FeatureStories[on].getElementsByTagName("description")[0].firstChild.nodeValue );
	n_link = MM_findObj('newsLink');
	n_link.firstChild.nextSibling.href = FeatureStories[on].getElementsByTagName("link")[0].firstChild.nodeValue;
	news_div = MM_findObj('news');
	news_div.className = 'headlines_'+on;
	DisplayTextFeatures();
}

news_name = 'News around Campus';
feature_name = 'Special Features';
function DisplayTextFeatures() {
	if (FeatureStories==null || FeatureStories.length==0) {
		html = '		<h3>'+feature_name+'</h3>';
		MM_setTextOfLayer('features','', html );
		return;
	}
	show = on;
	html = '		<h3>'+feature_name+'</h3>';
	count=0;
	countMax = FeatureStories.length-1;
	if (countMax>3) countMax = 3;
	
	while (count<countMax) {
		count++;
		show--;
		show += FeatureStories.length;
		show %= FeatureStories.length;
		featureLink = FeatureStories[show].getElementsByTagName("link")[0].firstChild.nodeValue;
		html += '<a class="title" href="'+featureLink+'">'+FeatureStories[show].getElementsByTagName("title")[0].firstChild.nodeValue+'</a>';
		html += FeatureStories[show].getElementsByTagName("description")[0].firstChild.nodeValue;
	}
	MM_setTextOfLayer('features','', html );
}

function DisplayTextNews() {
	if (NewsStories==null || NewsStories.length==0) {
		html = '		<h3>'+news_name+'</h3>';
		MM_setTextOfLayer('textNews','', html );
		return;
	}
	show = 0;
	html = '		<h3>'+news_name+'</h3>';
	count=0;
	countMax = NewsStories.length;
	if (countMax>6) countMax = 6;
	Complete = new Object();
	
	while (count<countMax) {
		count++;
		
		do {
			show = Math.floor(Math.random()*NewsStories.length);
		} while ( Complete[show]==true );
		Complete[show] = true;
		
		
		desc = NewsStories[show].getElementsByTagName("description")[0].firstChild.nodeValue;
		nl = NewsStories[show].getElementsByTagName("link")[0].firstChild.nodeValue;
		openNew = NewsStories[show].getElementsByTagName("new")[0].firstChild.nodeValue;
		if (openNew==1) target = ' target="_blank"';
		else target = '';
		html += '<a href="'+nl+'"'+target+'>'+desc+'</a>';
		show++;
		show %= NewsStories.length;
		/*  // Will need stuff like this for keywords:
		<div class="newsKeywords">
			<a href="#">SFU</a>
			<div class="divider">|</div>
			<a href="#">Surey</a>

			<div class="divider">|</div>
			<a href="#">Renovations</a>		</div>
		*/
	}
	MM_setTextOfLayer('textNews','', html );
}

function GetXmlHttpObject()
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
  {
  objXMLHttp=new XMLHttpRequest()
  }
else if (window.ActiveXObject)
  {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
return objXMLHttp
} 

var feature_url="xml/headlines.xml?"+Math.random();
function LoadFeatures() {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		// alert ("Browser does not support HTTP Request")
		return;
	} 
	xmlHttp.onreadystatechange=FeaturesLoader;
	xmlHttp.open("GET",feature_url,true);
	xmlHttp.send(null);
}

var featuresLoaded = false;
function FeaturesLoader() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		FeatureStories = xmlHttp.responseXML.getElementsByTagName("headline");
		if (MM_findObj('features')!=null) {
			DisplayTextFeatures();
			featuresLoaded = true;
		}
	}
}
function ShowFirstFeatures() {
	if (featuresLoaded) return;
	// on--;  // Show the one associated with the current picture
	on = pickRandom( FeatureStories.length );
	ShowNews();
}

var news_url="xml/news.xml?"+Math.random();
function LoadNews() {
	xmlHttpNews=GetXmlHttpObject()
	if (xmlHttpNews==null) {
		// alert ("Browser does not support HTTP Request")
		return;
	} 
	xmlHttpNews.onreadystatechange=NewsLoader;
	xmlHttpNews.open("GET",news_url,true);
	xmlHttpNews.send(null);
}

var newsLoaded = false;
function NewsLoader() {
	if (xmlHttpNews.readyState==4 || xmlHttpNews.readyState=="complete") { 
		NewsStories = xmlHttpNews.responseXML.getElementsByTagName("story");
		if (MM_findObj('news')!=null) {
			DisplayTextNews();
			newsLoaded = true;
		}
	}
}
function ShowFirstNews() {
	if (newsLoaded) return;
	DisplayTextNews();
}

LoadFeatures();
LoadNews();
startFunctions[startFunctions.length] = ShowFirstFeatures;
startFunctions[startFunctions.length] = ShowFirstNews;
