
(function($) {
	
	var EventCalendar = {
		container:     '#datepicker',
		cells:         'a.ui-state-default',
		cellContainer: 'td',
		title:         '.ui-datepicker-title',
		updatingId:    'event-calendar-updating',
		cachedEvents:  {},
		
		init: function () {
			var self = this, date = new Date();
			
			self.makeTransparent();

			if (currentMonth != undefined && currentYear != undefined) {
				date.setDate(1);
				date.setMonth(currentMonth-1);
				date.setFullYear(currentYear);
			}
			
			self.month = date.getMonth()+1;
			self.year  = date.getFullYear();		
			
			$(self.container).datepicker({defaultDate: date});
			
			self.unhookCells();
			
			if ($.datepicker._notifyChangeOld == undefined) {
				$.datepicker._notifyChangeOld = $.datepicker._notifyChange;
			}
			
			$.datepicker._notifyChange = function (inst) {
				$.datepicker._notifyChangeOld(inst);
				self.month = inst.selectedMonth + 1;
				self.year  = inst.selectedYear;
				self.update();
			}
			
			self.update();
		},
		
		makeTransparent: function () {
			$('#TB_ajaxContent').css({
				position:   'relative',
				top:        '80px'				
			});
			$('#TB_window').css({
				border:     '0',
				background: 'transparent'
			});
			$('#TB_title').hide().after($('<div />').height(27));
		},
		
		unhookCells: function () {
			var self = this;
			$(self.cells).each(function () {
				var $this = $(this);
				$this.parent(self.cellContainer)[0].onclick = '';
			});					
		},
		
		update: function () {
			var self = this;
			
			self.unhookCells();
			
			$(self.title).append(self.updatingStatus());
			$.ajax({
				url:      '/events/get_events',
				type:     'post',
				data:     {
					month: self.month,
					year:  self.year
				},
				dataType: 'json',
				success:  function (response) {
					if (response.month == self.month && response.year == self.year) {
						if (response.events != undefined) {
							self.addEvents(response.events);
						}
						$('#' + self.updatingId).remove();
					}
				}	
			});	
		},
		
		addEvents: function (monthEvents) {
			var self = this;
			
			var openEvent = function (e) {
				e.preventDefault();
				
				var $this = $(this), id = $this.attr('id'), name = $this.attr('title');
				
				$this.remove();
				$("#TB_window").remove();
				$("body").append('<div id="TB_window" />');
				tb_show(name, '/event/' + id + '/' + $this.attr('rel') + '?width=600&height=395'); 
			};			
			
			$(self.cells).each(function () {
				var $this = $(this), cellDay = $this.text();
				
				$.each(monthEvents, function (day, event) {
					if (day == cellDay) {
						var link = $('<div />')
									.attr('id', event.id)
									.attr('href', '#event' + event.id)
									.attr('title', event.name)
									.attr('rel', event.month + '-' + event.year)
									.text(event.name)
									.css('cursor', 'pointer')
									.click(openEvent);
									
						$this.append(link).append('<br />');
					}
				});
			});
		},
		
		updatingStatus: function () {
			return $('<span />')
						.attr('id', this.updatingId)
						.text('Loading content...')
						.css('marginLeft', '10px');
		}
	};
	
	$(function () {
		EventCalendar.init();
	});	
})(jQuery);

