$(function() {
	// calendar widget
	var cal = undefined;

	var calendar_init = function() {
		cal = $('div.calendar-wrap');

		// If we have Javascript, make it interactive.  Hence why we add
		// the buttons in this method.  Progressive enhancement!
		var prev_button = $('<a href="#" class="prev">&laquo;</a>')
		    .click(calendar_refresh);
		var next_button = $('<a href="#" class="next">&raquo;</a>')
		    .click(calendar_refresh);
		cal.find('div.month h4').before(prev_button);
		cal.find('div.month h4').before(next_button);
	};

	var calendar_refresh = function(){
		var month = cal.find('.month h4').text();
		var direction = $(this).attr('class');
		$.get('/calendar-ajax', { 
			'month': month,
			'direction': direction
		}, function(response) {
			cal.html(response);
			calendar_init();
		});
		return false;
	};

	// global initialization
	calendar_init();
});
