/*
 * FullCalendar v1.4.9 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: Fri Nov 19 22:45:44 2010 -0800
 *
 */

(function($) {

	$.fullCalendar.gcalFeed = function(feedUrl, options) {
		
		feedUrl = feedUrl.replace(/\/basic$/, '/full');
		options = options || {};
		today = new Date();
 
		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;
								}
							}
						});
						if (allDay) {
							$.fullCalendar.addDays(end, -1); // make inclusive
						}
						// we've added a class for past events so that 
						// we can color them differently for display.
						if (end<today) {
                        	klass='fc-event-past';
						} else {
                            klass='';
                        }
						// we can add tags to the citizen calendar through the web
						// but feeds that are automatically consumed won't have tags.
						// so, add a class via options and append the appropriate tag
						// to the description						
						//
						var meta = '';
						var klassy = '';

						switch (options.className){
                        	case 'city': 
								meta = 'government';
                        		break;
                        	case 'salidacafe': 
								meta = 'music';
                        		break;
							case 'highschool': 
								meta = 'highschool';
                        		break;
							case 'dance': 
								meta = 'dance';
                        		break;
							case 'health':
                                meta = 'health';
                                break;
							case 'steamplant':
                                meta = 'steamplant';
                                break;
							default: 
								meta = '';
						}
						if (meta){
							tags = ' Tags: ' + meta;
							klassy = ' ' + meta;
						} else {
							tags = '';
							klassy = '';
						}
						events.push({
							id: entry['gCal$uid']['value'],
							title: entry['title']['$t'],
							url: url,
							start: start,
							end: end,
							allDay: allDay,
							location: entry['gd$where'][0]['valueString'],
							description: entry['content']['$t'],
							className: klass + klassy,
							editable: options.editable || false
						});
					});
				}
				callback(events);
			});
		}
		
	}

})(jQuery);

