var feed=new Array();
var req=new Array();
var areq=new Array();
var names=new Array();
var times=new Array();
names[1]="Parry Sound North Star";
names[2]="Globe And Mail National News";
names[3]="CBC Top Stories";
var totalFeeds=3;

if (Prototype.Browser.IE) {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
        Prototype.BrowserFeatures['Version'] = new Number(RegExp.$1);
    }
}


function GetFeed(id){
	areq[id]=new Ajax.Request('./ajax/rss2json.php?f='+id, {
		requestTimeout: 2,
		onSuccess: function(transport) {
			feed[id]=transport['responseText'].evalJSON();
			ShowFeed(id);
			clearTimeout(times[id]);
		},
		onException: function(e) { 
			ReportError(id);
		},
		onFailure: function(e) { 
			ReportError(id);
		}
	});
	times[id]=setTimeout('ReportError('+id+')',5000);
}

function PageStart(){
	var i=1;
	var Table=new Element("table",{id:'NewsTable',style:"text-align: left; font-size: 10px; font-family: Verdana,Arial,Helvetica;"});
	var CurRow=new Element("tr",{});
	var m=0;
	while (i<=totalFeeds)
	{
		var txt="<b><font style='font-size: 14px;'>"+names[i]+":</font></b><br><font id='Links"+i+"'>"+
		"<center><BR><BR><BR><img src='ajax/news_load.gif' alt='Loading...' title='Loading...'></center></font>";
		var CurCel=new Element("td",{id:'News'+i,width:"300",height:'125','valign':'top'}).update(txt);
		if ((i % 2)!=0)
		{
			CurRow.appendChild(CurCel);
			m=1;
		}
		else
		{
			CurRow.appendChild(CurCel);
			Table.appendChild(CurRow);
			CurRow=new Element("tr",{});
			m=0;
		}
		req[i]=GetFeed(i);

		i++;
	}
	if (m==1)
	{
		var tmp=new Element('td',{id:'Other'}).update($('links_holder').innerHTML);
		CurRow.appendChild(tmp);
		Table.appendChild(CurRow);
	}
	$('news_holder').appendChild(Table);
	if (Prototype.Browser.IE)
	{
		if(Prototype.BrowserFeatures['Version']<8)
		{
			$('news_holder').innerHTML=$('news_holder').innerHTML;
		}
	}
}

function ShowFeed(id){
	var total=feed[id]['links'].size();
	if (total>10)
	{
		total=10;
	}
	var i=0;
	var txt="";
	var links=feed[id]['links'];
	while (i<total)
	{
		txt+="<a href='"+links[i]['link']+"'>"+links[i]['title']+"</a><BR>";
		i++;
	}
	var tmpTime=new Date();
	if (feed[id]['cached']>0)
	{
		tmpTime.setTime(feed[id]['cached']*1000);
	}
	var tTxt="<!--<font style='font-size:8px;'>Updated at "+tmpTime.getHours()+":"+tmpTime.getMinutes()+"</font>-->";
	$('Links'+id).update(txt+tTxt);
}

function ReportError(id){
	txt="<BR><BR>This feed is currently unavailable.<BR>Please try again later.";
	$('Links'+id).update(txt);
}