
var widgets = {
	// Data
	data: {
		weather: {
			wind: null,
			temp: {
				c: null,	// Celsius
				f: null		// Fahrenheit
			}
		}
	},
	
	shown: "",
	
	w: {
		calendar: "widgetCalendar",
		weather: "widgetWeather"
	},
	
	// Functions
	hideAll: function() {
		$("#widgets .widget").hide();
	},
	
	loadWeather: function() {
		$.post("/_xml/gapa_wetter.xml", {}, function(data) {
			try {
				var root = data.getElementsByTagName("dataset")[0];
				
				for (var i = 0; i < root.childNodes.length; i++) {
					switch(root.childNodes[i].nodeName.toLowerCase()) {
						case 'wind':
							widgets.data.weather.wind = root.childNodes[i];
						break;
						
						case 'temp':
						log(root.childNodes[i]);
							var unit = root.childNodes[i].getAttribute("u");
							
							if (unit.toLowerCase() == "c") {
								widgets.data.weather.temp.c = root.childNodes[i];
							} else if (unit.toLowerCase() == "f") {
								widgets.data.weather.temp.f = root.childNodes[i];
							}
						break;
					}
				}

				$("#" + widgets.w.weather).html("Wind: " + widgets.data.weather.wind.getElementsByTagName("FF")[0].firstChild.nodeValue + " km/h<br/><br/>" + 
					"Temp: " + widgets.data.weather.temp.c.getElementsByTagName("TL")[0].firstChild.nodeValue + " °C");
				
			} catch(e) {
				log("Error parsing weather data: " + e);
			}
		}, "xml");
	},
	
	show: function(widget) {
		$("#" + widget).show();
	},
	
	toggleCalendar: function() {
		widgets.hideAll();
		
		if (widgets.shown != widgets.w.calendar) {
			widgets.show(widgets.w.calendar);
			widgets.shown = widgets.w.calendar;
		} else {
			widgets.shown = "";
		}
		
	},
	
	toggleWeather: function() {
		widgets.hideAll();
		
		if (widgets.shown != widgets.w.weather) {
			widgets.show(widgets.w.weather);
			widgets.shown = widgets.w.weather;
		} else {
			widgets.shown = "";
		}
	}
};

var CalendarEvents = {
	events: {},
	
	getEventForDay: function(d, m, y) {
		try {
			var num = CalendarEvents.events[y][m][d];
			return num;
		} catch(e) {
			return 0;
		}
	},
	
	loadAllEvents: function() {
		CalendarEvents.events[2009] = {};
		CalendarEvents.events[2009][6] = {};
		CalendarEvents.events[2009][6][29] = 2;
		CalendarEvents.events[2009][6][27] = 1;
	}
	
};

/*
$(function() {
	CalendarEvents.loadAllEvents();
	widgets.loadWeather();
	
	$("#serviceArea img").click(function() {
		widgets.toggleWeather();
	});
	
	$.datepicker.setDefaults($.extend({showMonthAfterYear: false}, $.datepicker.regional['de']));
	
	$("#widgetCalendar").datepicker({
		onSelect: function(dateText, inst) {
			
			widgets.hideAll();
		},
		
		/**
		 * Taken from jQuery documentation
		 * The function takes a date as a parameter and must return an array with 
		 * [0] equal to true/false indicating whether or not this date is selectable, 
		 * [1] equal to a CSS class name(s) or '' for the default presentation and 
		 * [2] an optional popup tooltip for this date. It is called for each day in the datepicker before is it displayed.
		 * @param {Object} date
		 */
/*
		beforeShowDay: function(date) {
			var options = new Array();
			
			options[0] = false;
			options[1] = '';
			options[2] = '';
			
			var day = date.getDate();
			var month = date.getMonth() + 1;
			var year = date.getFullYear();
			
			var numEvents = CalendarEvents.getEventForDay(day, month, year);
			
			if (numEvents > 0) {
				options[0] = true;
				options[1] = '';
				options[2] = numEvents + (numEvents == 1 ? ' Veranstaltung' : ' Veranstaltungen');
			}
			
			
			return options;
		}
		
	});
});
*/

