// JavaScript Document

function ShowNews() {
        if (!FeatureStories || !FeatureStories.length) return;
        on++;
        on %= FeatureStories.length;
	$cutline = FeatureStories[on].getElementsByTagName("cutline")[0].firstChild;
	if ($cutline==null || $cutline.nodeValue.length==0) $cutline = FeatureStories[on].getElementsByTagName("description")[0].firstChild.nodeValue;
	else $cutline = $cutline.nodeValue;

        MM_setTextOfLayer('newsText','', $cutline);
        n_link = MM_findObj('newsLink');
        val = FeatureStories[on].getElementsByTagName("link")[0].firstChild;
        if (val==null) val='';
        else val = val.nodeValue;
        if (val.length==0) {
                n_link.className="hidden";
                n_link.firstChild.nextSibling.href = '#';
        }
        else {
                n_link.className="";
                n_link.firstChild.nextSibling.href = FeatureStories[on].getElementsByTagName("link")[0].firstChild.nodeValue;
        }
        news_div = MM_findObj('news');
        news_div.className = 'headlines_'+on;
        DisplayTextFeatures();
}

LoadFeatures();

    google.load("feeds", "1");

var keys = Array();
var vals = Array();

function set(name,val) {
	keys[keys.length] = name;
	vals[vals.length] = val;
}
function get(name) {
	for (i=0;i<keys.length;i++) {
		if (keys[i]==name) return vals[i];
	}
	return 0;
}

function initialize() {
var feed = new google.feeds.Feed(feed_source);
feed.setNumEntries(100);  // Can't control the feed, must have it pruned before I work with it.
feed.load(function(result) {
if (!result.error) {
  set('Jan', 0);
  set('Feb', 1);
  set('Mar', 2);
  set('Apr', 3);
  set('May', 4);
  set('Jun', 5);
  set('Jul', 6);
  set('Aug', 7);
  var today = Date();
  info = today.split(' ');
  var t_year = parseInt(info[3]);
  var t_month = get( info[1] );
  var t_day = parseInt(info[2]);
  var container = document.getElementById("feed");
  var d = document.createElement('div');
  d.setAttribute('class', 'eventTitle');
  d.appendChild(document.createTextNode( "Events" ) );
  container.appendChild(d);
  var shown=0;
  for (var i = 0; i < result.feed.entries.length; i++) {
    var entry = result.feed.entries[i];
    info = entry.publishedDate.split(' ');
    year = parseInt(info[3]);
    month = get(info[2]);
    day = parseInt(info[1]);
    if ( ( (year>t_year)||(year==t_year&&month>t_month)||(year==t_year&&month==t_month&&day>=t_day) ) && shown<4) {
        shown++;
        var a = document.createElement('a');
        a.setAttribute('class', 'event');
        a.setAttribute('href', entry.link);
        a.setAttribute('target', '_blank');
        a.setAttribute('onclick', "return eventOpen('"+entry.link+"');");
        a.appendChild(document.createTextNode(entry.title));
        container.appendChild(a);
        var d = document.createElement('div');
        d.appendChild(document.createTextNode( info[0]+' '+info[2]+' '+info[1]+' '+info[3] ) );
        container.appendChild(d);
        }
  }
  var d = document.createElement('div');
  d.setAttribute('id', 'eventPageLink');
  var a = document.createElement('a');
  a.setAttribute('href', "https://events.sfu.ca/");
  a.appendChild(document.createTextNode("More SFU Events"));
  d.appendChild(a);
  container.appendChild(d);
}
});
}
google.setOnLoadCallback(initialize);

function eventOpen(url) {
	window.open(url,'eventwindow','width=680,height=575,scrollbars=yes')
	return false;
}

window.setInterval("ShowNews()",30000);

