jQuery.noConflict();
(function($) { 
  $(function() {
    	function convertDate(start,end){
			function pad(number) {
				return (number < 10 ? '0' : '') + number;
			}
			
			var s = new Date(start),
				smonth = s.getMonth()+1,
				sdate = s.getDate(), 
				shour = pad(s.getHours()),
				smin = pad(s.getMinutes());
			var e = new Date(end),
				emonth = e.getMonth()+1,
				edate = e.getDate(),
				ehour = pad(e.getHours()),
				emin = pad(e.getMinutes());
				
			if(shour == 00 && smin == 00 & ehour == 00 && emin == 00) {
				return smonth + "/" + sdate + " - " + emonth + "/" + (edate-1);
			}
			else if (smonth == emonth & sdate == edate & shour == ehour && smin == emin) {
				return smonth + "/" + sdate + " " + shour + smin;
			}
			else if (smonth == emonth & sdate == edate) {
				return smonth + "/" + sdate + " " + shour + smin + " - " + ehour + emin;
			}
			else {
				return smonth + "/" + sdate + " " + shour + smin + " - " + emonth + "/" + (edate)  + " " + ehour + emin;
			}
		}
	
	var numevents = 12;
	var url = "http://www.google.com/calendar/feeds/martinsburgcap.com_dj683eh6gaopp9bmofd6ggb9ts@group.calendar.google.com/public/full?alt=json&orderby=starttime&max-results=" + numevents + "&singleevents=true&sortorder=ascending&futureevents=true";

	$.getJSON(url, function(data) {
		$("#agenda").html(" ");
		$.each(data.feed.entry, function() {
			var title = this.title.$t,
				link = this.link[0].href,
				start = new Date(this.gd$when[0].startTime),
				end = new Date(this.gd$when[0].endTime);
			$("#agenda").append("<a href='" + link + "'>" + title + "</a> " + convertDate(start,end) + "<br /><br />");
		});
	});
  });
})(jQuery);
