// JavaScript Document

/****
* Get a Google Calendar feed and display the event title and date
*
* Requires:
* - Google AJAX API Key
* - Google Calendar feed URL (XML)
* 
* A few quick notes:
* The feed URL must be in XML format, and it must have the additional parameters after the '?'. Also, normally the XML feed will be the 'basic' version
* but you need to change 'basic' in the XML URL to 'full'. If both of these things are not done, then the feed fails to load.
* There is an ISO8601 date function that takes the XML date and processes it into a readable date by Javascript. Parts of the code here were returning
* incorrect date values, so you'll see several commented-out lines.
*
* Most of this code by:
* - http://hansdebano.net/google/feedapi/calendarfeeds.html
*
* Customized by:
* - MECSB
****/

google.load("feeds", "1");
function initialize() {

//http://www.google.com/calendar/feeds/phoenixprideorg%40gmail.com/public/basic
//http://www.google.com/calendar/feeds/asu.edu_q065bkoducouhbr42c83548dgk%40group.calendar.google.com/public/full?orderby=starttime&sortorder=ascending&futureevents=true&max-results=500

	var feed = new       google.feeds.Feed("http://www.google.com/calendar/feeds/phoenixprideorg%40gmail.com/public/full?orderby=starttime&sortorder=ascending&futureevents=true&max-results=10");
	feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
	
	// Default value displays only four feed entries
	feed.setNumEntries(10);
	feed.load(function(result) {
	var container = document.getElementById("feed");
	if (!result.error) {
		var whenstring = '';
		var entries = google.feeds.getElementsByTagNameNS(result.xmlDocument, "http://www.w3.org/2005/Atom", "entry");
		var table = document.createElement("table");
		var tbody = document.createElement("tbody");
		for (var i = 0; i < entries.length; i++) {
			var titleElement = google.feeds.getElementsByTagNameNS(entries[i], "http://www.w3.org/2005/Atom", "title")[0];
			var title = titleElement.firstChild.nodeValue;
			var whenElement = google.feeds.getElementsByTagNameNS(entries[i], "http://schemas.google.com/g/2005", "when")[0];
			var starttime = whenElement.getAttribute('startTime');
			var startdate = new Date();
			startdate.setISO8601(starttime);
			
			//Parse the date
			var startstring = starttime.substr(5, 5);
			startstring = startstring.replace('-', '/');
			whenstring = startstring;
			
			//Count loops to do even/odd rows
			if ((i+1)%2 == 0) {
				var odd = false;
			} else {
				var odd = true;
			}
			
			//Set up HTML
			var tr = document.createElement("tr");
			var td = document.createElement("td");
			var td2 = document.createElement("td");
			var div = document.createElement("div");
			var spanwhen = document.createElement("span");
			
			spanwhen.setAttribute("class", "when");
			tr.setAttribute("height", "26");
			td2.setAttribute("class", "datewhen");
			div.setAttribute("class", "title");
			
			if(odd) {
				td.setAttribute("class", "odd");
				td2.setAttribute("class", "datewhen odd");
			}
			
			spanwhen.appendChild(document.createTextNode(whenstring));
			div.appendChild(document.createTextNode(title));
			td.appendChild(div);
			td2.appendChild(spanwhen);
			tr.appendChild(td);
			tr.appendChild(td2);
			tbody.appendChild(tr);
			
			
			//alert(tr);
		}
		table.setAttribute("cellspacing", "0");
		table.setAttribute("cellpadding", "0");
		table.appendChild(tbody);
		container.appendChild(table);
	}
});
}

google.setOnLoadCallback(initialize);
