
	var working = false;

	var Calendar = {
		init : function() {
			if ($('ul#dates li').length == 0) {
				$('.selected_dates').css('padding-bottom', 250);
				$('.dates_list, .visit_form').hide();
			}

			$('.calendar table a').click(function() {
				if (!working) Calendar.date_go($(this));
				return false;
			})

			Calendar.prep();
		},


		date_go : function(the_link) {
			working = true;

			the_href = the_link.attr('href');
			the_date = the_href.substr(the_href.length - 10);
			the_cell = $('.calendar td.date_'+the_date);

			if (the_cell.hasClass('available')) {
				the_cell
					.removeClass('available')
					.addClass('selected');

				Calendar.date_add(the_date);
			}
			else {
				the_cell
					.removeClass('selected')
					.addClass('available');

				Calendar.date_delete(the_date);
			}
		},


		date_add : function(the_date) {
			$.get('/incs/actions/date.add.php', { 'date' : the_date, 'ajax' : true }, function(response_html) {
				$('.selected_dates').css('padding-bottom', 0);
				$('.dates_list:hidden, .visit_form:hidden').fadeIn();

				$('#dates').remove();

				$(response_html).appendTo('.dates_list');

				$('.new').effect('highlight', {}, 1000);

				$('<input type="hidden" name="req_dates[]" value="' + the_date + '" class="date_' + the_date + ' hide_this" />' + "\n").appendTo('.visit_form form fieldset');

				Calendar.prep();
			}, 'html');
		},


		date_delete : function(the_date) {
			$('#dates li.date_' + the_date)
				.css('background-color', '#ff9')
				.fadeOut('normal', function() {
					$.get('/incs/actions/date.delete.php', { 'date' : the_date, 'ajax' : true });

					$(this).remove();

					$('input[type=hidden].date_' + the_date).remove();

					if ($('.dates_list ul li').length == 0) {
						$('.dates_list:visible').fadeOut('normal', function() {
							$('.selected_dates').css('padding-bottom', 250);

							$('.visit_form:visible').fadeOut('normal', function() {
								$('label.error').removeClass('error');
								$('.alert.error').remove();
							})
						})
					}

					Calendar.prep();
				});
		},


		prep : function() {
			$('#dates li a').click(function() {
				if (!working) Calendar.date_go($(this));
				return false;
			})

			working = false;
		}
	};

