
var MonthDays = new Array( "-", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", 
				"11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "10th",
				"21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th",
				"31st" );
var Months = new Array( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" );

/**
 * Parse an XML format date (YYYY-MM-DD...) and format for display on home page event list.
 * @param dateString - the XML formatted date string.
 * @return a date string formatted as "Dth Mmm Yyyy".
 */
function dateFormat1( dateString )
{
	if( dateString == null || dateString.length < 10 )
	{
		return "";
	}
	
	var m = dateString.match(/(\d{4})-(\d{2})-(\d{2})/);
	var d = new Date( m[1], (m[2] - 1), m[3] );
	return MonthDays[d.getDate()] + " " + Months[d.getMonth()] + " " + d.getFullYear();
}

/**
 * Handle the ready event when the calendar feed is returned.
 * @param xml - the XML content of the calendar feed.
 */
function handleCalendar(xml) {
	var feed = $("feed", xml);

	var entries = '<table width="100%">';
	var i = 0;
	$(feed).children("entry").each(
			function() {
				if (i++ < 5) {
					var eventTitle = $(this).find("title")
							.text();
					var eventLocation = $(this).find("[nodeName=gd:where]").attr("valueString");
					if( eventLocation == null )
					{
						eventLocation = "";
					}
					var startDate = $(this).find("[nodeName=gd:when]").attr("startTime");
					var text = '<tr>' + 
					'<td width="20%" nowrap valign="top"><span class="style9 navlist">' + dateFormat1( startDate ) + '</span></td>' + 
					'<td valign="top"><span class="style9"><strong>' + eventTitle + '</strong>, <em>' + eventLocation + '</em></span></td>' + 
					'</tr>';
					entries += text;
				}
			});

	entries += '</table>';
	$("#entries").html( entries );
}

$(document).ready(
		function() {
			$.ajax( {
				type : "GET",
				url : "events-feed.php",
				dataType : "xml",
				error : function() {
					alert("Failed");
				},
				success : handleCalendar
			});
		});


