$(document).ready(function(){
	$.getJSON("http://www.google.com/calendar/feeds/9il95d3brs5kvm21p4p7fv6ft0%40group.calendar.google.com/public/basic?alt=json-in-script&callback=?",
	
	function(json){
		var count = 0;
		
		var eventListItems = '';
		
		$.each(json.feed.entry,function(i,entry) {
			
			var href = $(entry.link).attr("href");
			
			var content = entry.content.$t;
			
			var day = /[0-9]{1,2}(?=,)/.exec(content);
	
			var month = /Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/.exec(content);
			
			if(month == 'Jan'){month = '00';}
			else if(month == 'Feb'){month = '01';}
			else if(month == 'Mar'){month = '02';}
			else if(month == 'Apr'){month = '03';}
			else if(month == 'May'){month = '04';}
			else if(month == 'Jun'){month = '05';}
			else if(month == 'Jul'){month = '06';}
			else if(month == 'Aug'){month = '07';}
			else if(month == 'Sep'){month = '08';}
			else if(month == 'Oct'){month = '09';}
			else if(month == 'Nov'){month = '10';}
			else if(month == 'Dec'){month = '11';}
			
			var year = /[0-9]{4}(?=\b)/.exec(content);
			
			var hour = /[0-9]{1,2}(?=:)|[0-9]{1,2}(?=a)|[0-9]{1,2}(?=p)/.exec(content);
	
			if(hour == null){
				hour = '';
			}
	
			var minute = /[:][0-9][0-9](?=.+to)/.exec(content);
			
			minute = /[0-9][0-9]/.exec(minute);
			
			if(minute == null){
				minute = '';
			}
			
			var ampm = /am(?=\b)|pm(?=\b)/.exec(content);
			
			if(ampm == null){
				ampm = '';
			} else if(ampm == 'pm'){
				hour = parseInt(hour) + 12;
			}
					
			var thisDate = new Date(year,month,day,hour,minute);
	
			var today = new Date();
	
			var where = /Where:.+?(?=<|\n|$)/.exec(content);
			
			if(where == null){
				where = '';
			}
	
			var description = /Event\sDescription:.+?(?=<|\n|$)/.exec(content);
			
			if(description == null){
				description = '';
			} else {
				description = /[^Event\sDescription:\s].+/.exec(description);
			}
			
			if(thisDate >= today){
				count++;
				eventListItems += '<li><a class="event-title" href="' + href + '" target="_blank">' + entry.title.$t + '</a><div class="event-details-wrapper"><div class="event-details"><p>When: ' + thisDate.format('dddd, mmmm d, yyyy, h:MM tt') + '</p><p>' + where + '</p><p><em>' + description + '</em></p></div></div></li>';
			}
	
			if(count >=5){
				return false;
			}
		})
		
		document.getElementById('eventList').innerHTML = eventListItems;
	});
});