/*
 * FullCalendar v1.4.8 Google Calendar Extension
 *
 * Copyright (c) 2010 Adam Shaw
 * Dual licensed under the MIT and GPL licenses, located in
 * MIT-LICENSE.txt and GPL-LICENSE.txt respectively.
 *
 * Date: Sat Oct 16 17:10:03 2010 -0700
 *
 */

(function($) {

	$.fullCalendar.gcalFeed = function(feedUrl, options) {
		
		feedUrl = feedUrl.replace(/\/basic$/, '/full');
		options = options || {};
		
		return function(start, end, callback) {
			var params = {
				'start-min': $.fullCalendar.formatDate(start, 'u'),
				'start-max': $.fullCalendar.formatDate(end, 'u'),
				'singleevents': true,
				'max-results': 9999
			};
			var ctz = options.currentTimezone;
			if (ctz) {
				params.ctz = ctz = ctz.replace(' ', '_');
			}
			$.getJSON(feedUrl + "?alt=json-in-script&callback=?", params, function(data) {
				var events = [];
				if (data.feed.entry) {
					$.each(data.feed.entry, function(i, entry) {
						var startStr = entry['gd$when'][0]['startTime'],
							start = $.fullCalendar.parseISO8601(startStr, true),
							end = $.fullCalendar.parseISO8601(entry['gd$when'][0]['endTime'], true),
							allDay = startStr.indexOf('T') == -1,
							url;
						$.each(entry.link, function() {
							if (this.type == 'text/html') {
								url = this.href;
								if (ctz) {
									url += (url.indexOf('?') == -1 ? '?' : '&') + 'ctz=' + ctz;
								}
							}
						});
						var startDate = new Date(start);
						var endDate = new Date(end);
						
						var d_names = new Array("Sun", "Mon", "Tue",
						"Wed", "Thu", "Fri", "Sat");

						var m_names = new Array("January", "February", "March", 
						"April", "May", "June", "July", "August", "September", 
						"October", "November", "December");
						
						var fullTime = "";
						
						if (allDay) {
							$.fullCalendar.addDays(end, -1); // make inclusive
							fullTime = d_names[startDate.getDay()] + ", " + m_names[startDate.getMonth()] + " " + startDate.getDate() + ", " + startDate.getFullYear();
							if(startDate.getTime() != endDate.getTime()) {
								endDate.setDate(endDate.getDate()-1);
								fullTime += " - " + d_names[endDate.getDay()] + ", " + m_names[endDate.getMonth()] + " " + endDate.getDate() + ", " + endDate.getFullYear();	
							}
						} else {
							fullTime = d_names[startDate.getDay()] + ", " + m_names[startDate.getMonth()] + " " + startDate.getDate() + ", " + startDate.getFullYear();
							if(end != null && startDate.getTime() != endDate.getTime()) {
								var startHour = startDate.getHours();
								var startMin = startDate.getMinutes();
								var startA_P = "am";
								if(startHour > 11) {
									startA_P = "pm";
									startHour = startHour-12;
								}
								if(startHour == 0) startHour = 12;
								fullTime += " from " + startHour;
								if(startMin < 10) {
									if(startMin == 0) fullTime += startA_P;
									else fullTime += ":0" + startMin + startA_P;
								} else fullTime += ":" + startMin + startA_P;
								fullTime += " - ";	
								
								var endHour = endDate.getHours();
								var endMin = endDate.getMinutes();
								var endA_P = "am";
								if(endHour > 11) {
									endA_P = "pm";
									endHour = endHour-12;
								}
								if(endHour == 0) endHour = 12;
								fullTime += " " + endHour;
								if(endMin < 10) {
									if(endMin == 0) fullTime += endA_P;
									else fullTime += ":0" + endMin + endA_P;
								} else fullTime += ":" + endMin + endA_P;
							} else {
								var startHour = startDate.getHours();
								var startMin = startDate.getMinutes();
								var startA_P = "am";
								if(startHour > 11) {
									startA_P = "pm";
									startHour = startHour-12;
								}
								if(startHour == 0) startHour = 12;
								fullTime += " at " + startHour;
								if(startMin < 10) {
									if(startMin == 0) fullTime += startA_P;
									else fullTime += ":0" + startMin + startA_P;
								} else fullTime += ":" + startMin + startA_P;
							}
						}
						events.push({
							id: entry['gCal$uid']['value'].replace("@google.com", ""),
							title: entry['title']['$t'],
							url: '#',
							start: start,
							end: end,
							allDay: allDay,
							fullTime: fullTime,
							location: entry['gd$where'][0]['valueString'],
							description: entry['content']['$t'],
							className: options.className,
							editable: options.editable || false
						});
					});
				}
				callback(events);
			});
		}
		
	}

})(jQuery);

