/*--------------------------------------------------------------------------*/
/* JavaScript Document                                                     */
/*------------------------------------------------------------------------*/

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

function initialize() {
	var feedlimit=5
	var feed = new google.feeds.Feed(cp_feedurl); //URL = GLOBAL SET IN PAGE HEADER.
	var feedcontainer = document.getElementById("gCalFeed");
	feed.setNumEntries(feedlimit) //Google Feed API method
	feed.load(function(result) {
		if (!result.error) { 
			var rssoutput='<ul id="feed_event_listing">';
			for (var i = 0; i < result.feed.entries.length; i++) {
				var entry = result.feed.entries[i];
				//PARSE DATA:
				var entryTitleStr = entry.title;
				var tempStrArray=entry.content.split("<br>") //0=when, 2=where, 3=event status, 4=event description
				var whenStr = new String(tempStrArray[0]);
				whenStr = whenStr.replace("When: ", " ");
				whenStr = whenStr.replaceAll("EDT", " ");
				whenStr = whenStr.replaceAll("EST", " ");
				var whereStr = tempStrArray[2];
				whereStr = whereStr.replace("Where: ", " ");
				if (whereStr.startsWith('Event Status:')) { whereStr = " "; } else { whereStr = " — at " + whereStr; }							
				var descriptionStr = new String(tempStrArray[4]);
				if (descriptionStr!="undefined") { 
					descriptionStr = descriptionStr.replace("Event Description: ", " ");
					descriptionStr = descriptionStr.replaceAll("\n", "<br />");
				} else { descriptionStr = "No description available."; }
				//FORMAT OUTPUT:
				rssoutput+= '<li id="event' + i + '"><a href="#event_description' + i + '" onclick=' + "'Effect.toggle(";
				rssoutput+= '"event' + i + '_description", "slide", { duration: 0.85 });' + "'>";
				rssoutput+= '<span class="feed_event_title">' + entryTitleStr + '</span></a><br />';
				rssoutput+= '<span class="feed_when">' + whenStr + '</span>';
				rssoutput+= '<span class="feed_where">' + whereStr + '</span>';
				rssoutput+= '<ul id="event' + i + '_description" class="feed_description" style="display:none">';
				rssoutput+= '<li>' + descriptionStr + '</li></ul></li>';
			}
			rssoutput+= "</ul>";
			feedcontainer.innerHTML=rssoutput;
		} else {
			feedcontainer.innerHTML=("Event Feed Unavailable");
		}
	});
}

google.setOnLoadCallback(initialize);


/*--------------------------------------------------------------------------*/