function resetCalendar() {
	$('.ajaxable-calendar .previous-month, .ajaxable-calendar .next-month').bind('click',
		function(e) {
			url = '/ajax' + $(this).attr('href');
			loadCalendar(url);
			return false;
		}
	);
}

function loadCalendar(url) {
	$.ajax(
		{
			url: url,
			dataType: 'html',
			success: function(html) {
				$('.ajaxable-calendar').html(html);
				resetCalendar();
			},
			error: function() {
				alert('Sorry, but we were unable to load the calendar');
			}
		}
	);
}

$(document).ready(
	function() {
		$('#country').bind('change',
			function(e) {
				window.location.replace("?country=" + $(this).val() + '#events');
			}
		)
	}
);

$(document).ready(
	function() {
		$('.widget.event_table a.event-link').each(
			function(i) {
				$(this).bind('click',
					function(e) {
						split = $(this).attr('href').split('/')
						id = split[split.length - 2];
						$(this).parent().fadeOut();
						
						$.ajax(
							{
								url: '/ajax' + $(this).attr('href'),
								dataType: 'html',
								success: function(data) {
									details = $('#event-details-' + id)
									details.hide();
									details.html(data);
									if (navigator.userAgent.indexOf("MSIE 7") == -1) {
										details.slideDown(500,
											function() {
												$(this).fadeIn();
											}
										);
									} else {
										/*
										 * Internet Explorer 7 makes text invisible.  Setting the zoom property
										 * manually after the animation makes the text show up again.
										 */
										details.slideDown(500,
											function() {
												$(this).fadeIn(function() {
													$(this).css("zoom", "1");
												});
											}
										);
									}
									
									details.find('.widget.comments').each(
										function(i) {
											bindCommentsForm($(this));
											bindCommentRatings($(this));
										}
									);
									
									details.find('.widget-closed .header a').each(
										function(i) {
											$(this).bind('click',
												function(e) {
													openWidget($(this));
												}
											)
										}
									);
									
									details.find('.widget-open .header a').each(
										function(i) {
											$(this).bind('click',
												function(e) {
													closeWidget($(this));
													
													container = $(this).parent().parent().parent();
													widgets = container.find('.widget-open').size();
													
													if(widgets == 0) {
														container.hide();
														
														split = container.attr('id').split('-');
														id = split[split.length - 1]
														$('#event-' + id + ' .event-detail-links').fadeIn();
													}
												}
											)
										}
									);
								}
							}
						);
						
						return false;
					}
				);
			}
		);
		
		$('.widget.event_table a.attending-link').each(
			function(i) {
				$(this).bind('click',
					function(e) {;
						link = $(this);
						link.hide();
						
						$.ajax(
							{
								url: $(this).attr('href') + '&ajax=1',
								dataType: 'text',
								success: function(data) {
									href = link.attr('href');
									attending = href.substr(href.indexOf('?attending=') + ('?attending=').length);
									
									if (attending == 'true') {
										attending = 'false';
										link.html('Mark as not attending');
									} else {
										attending = 'true';
										link.html('Mark as attending');
									}
									
									href = href.substr(0, href.indexOf('?attending=')) + '?attending=' + attending;
									link.attr('href', href);
									link.show();
								}
							}
						);
						
						return false;
					}
				);
			}
		);
	}
);

$(document).ready(resetCalendar);