google.load("feeds", "1", {"nocss" : true});
function initialize()
{
	var feed = new google.feeds.Feed("http://www.google.com/calendar/feeds/web-editor%40unumc.org/public/full?orderby=starttime&sortorder=ascending&singleevents=true&futureevents=true&max-results=1");
	feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
	feed.load(function(result)
	{
		var container = document.getElementById("trip_feed");
		if (!result.error)
		{
			var entries = google.feeds.getElementsByTagNameNS(result.xmlDocument, "http://www.w3.org/2005/Atom", "entry");
			var whenElement = google.feeds.getElementsByTagNameNS(entries[0], "http://schemas.google.com/g/2005", "when")[0];
			var titleElement = google.feeds.getElementsByTagNameNS(entries[0], "http://www.w3.org/2005/Atom", "title")[0];
			var title = titleElement.firstChild.nodeValue;
			var whereElement = google.feeds.getElementsByTagNameNS(entries[0], "http://schemas.google.com/g/2005", "where")[0];
			var where = whereElement.getAttribute('valueString');
			var map = 'http://maps.google.co.uk/maps?hl=en&q=' + where;
			var contentElement = google.feeds.getElementsByTagNameNS(entries[0], "http://www.w3.org/2005/Atom", "content")[0];
			var content = contentElement.firstChild.nodeValue;
			var starttime = whenElement.getAttribute('startTime');
			var endtime = whenElement.getAttribute('endTime');
			var startdate = new Date();
			var startsplit = starttime.split("-");
			startdate.setFullYear(startsplit[0], startsplit[1] - 1, startsplit[2]);
			startdate.setHours(0,0,0,0);
			var enddate = new Date();
			var endsplit = endtime.split("-");
			enddate.setFullYear(endsplit[0], endsplit[1] - 1, endsplit[2]);
			enddate.setHours(0,0,0,0);
			
			var months = new makeArray('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
			
			var calcdate = new Date();
			calcdate.setDate(startdate.getDate() + 1);
			
			if(enddate.toDateString() == calcdate.toDateString())
			{
				var whenstring = num_abbrev_str(startdate.getDate()) + ' ' + months[startdate.getMonth() + 1];
			}
			else
			{
				var whenstring = num_abbrev_str(startdate.getDate()) + ' ' + months[startdate.getMonth() + 1] + ' - ' + num_abbrev_str(enddate.getDate()) + ' ' + months[enddate.getMonth() + 1];
			}
			
			var p = document.createElement("p");
			var b = document.createElement("b");
			var br = document.createElement("br");
			var span = document.createElement("span");
			
			b.appendChild(document.createTextNode(whenstring));
			p.appendChild(b);
			p.appendChild(document.createTextNode(' ' + String.fromCharCode(8226) + ' '));
			
			if (where != '')
			{
				var a = document.createElement("a");
				a.setAttribute("href", map);
				a.appendChild(document.createTextNode(title));
				p.appendChild(a);
			}
			else
			{
				p.appendChild(document.createTextNode(title));
			}
			
			p.appendChild(br);
			span.setAttribute("class", "feed");
			span.appendChild(document.createTextNode(String.fromCharCode(160) + String.fromCharCode(160) + '[ ' + content + ' ]'));
			p.appendChild(span);
			container.appendChild(p);
		}
	});
	
	var feed = new google.feeds.Feed("http://www.google.com/calendar/feeds/dqb62hdg303tkbqp5841t3eu2c%40group.calendar.google.com/public/full?orderby=starttime&sortorder=ascending&singleevents=true&futureevents=true&max-results=1");
	feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
	feed.load(function(result) {
		var container = document.getElementById("social_feed");
		if (!result.error) {
			var entries = google.feeds.getElementsByTagNameNS(result.xmlDocument, "http://www.w3.org/2005/Atom", "entry");
			var whenElement = google.feeds.getElementsByTagNameNS(entries[0], "http://schemas.google.com/g/2005", "when")[0];
			var titleElement = google.feeds.getElementsByTagNameNS(entries[0], "http://www.w3.org/2005/Atom", "title")[0];
			var title = titleElement.firstChild.nodeValue;
			var whereElement = google.feeds.getElementsByTagNameNS(entries[0], "http://schemas.google.com/g/2005", "where")[0];
			var where = whereElement.getAttribute('valueString');
			var map = 'http://maps.google.co.uk/maps?hl=en&q=' + where;
			var location = where.split(",");
			var starttime = whenElement.getAttribute('startTime');
			var startdate = new Date();
			startdate.setISO8601(starttime);
			
			var months = new makeArray('Jan','Feb','Mar', 'Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
			
			var whenstring = num_abbrev_str(startdate.getDate()) + ' ' + months[startdate.getMonth() + 1] + ' ~ ' + h24_to_h12(checkTime(startdate.getHours())) + ':' + checkTime(startdate.getMinutes()) + ' ';
			
			(startdate.getHours() < 12) ? whenstring += 'am' : whenstring += 'pm';
			
			var p = document.createElement("p");
			var b = document.createElement("b");
			var br = document.createElement("br");
			var span = document.createElement("span");
			
			b.appendChild(document.createTextNode(whenstring));
			p.appendChild(b);
			p.appendChild(document.createTextNode(' ' + String.fromCharCode(8226) + ' '));
			
			if (where != '')
			{
				var a = document.createElement("a");
				a.setAttribute("href", map);
				a.appendChild(document.createTextNode(location[0]));
				p.appendChild(a);
			}
			else
			{
				p.appendChild(document.createTextNode(location[0]));
			}
			
			p.appendChild(br);
			span.setAttribute("class", "feed");
			span.appendChild(document.createTextNode(String.fromCharCode(160) + String.fromCharCode(160) + '[ ' + title + ' ]'));
			p.appendChild(span);
			container.appendChild(p);
		}
	});
}

function makeArray() {
	for (i = 0; i<makeArray.arguments.length; i++)
		this[i + 1] = makeArray.arguments[i];
}

function num_abbrev_str(n)
{
	return n + ["th","st","nd","rd"][!(n%10>3||Math.floor(n%100/10)==1)*n%10]; 
}

function h24_to_h12(n)
{
	if (n > 12)
		return n - 12;
	else if (n == 0)
		return 12;
	else
		return n;
}

function checkTime(n)
{
	if (n < 10)
		return "0" + n;
	else
		return n;
}

google.setOnLoadCallback(initialize);
