$(document).ready(
	function() {
		$(".open_tourdates").bind("click",
			function() {
				$(this).openTourDate();
				return false;
			}
		);
		$(".close_tourdates").bind("click",
			function() {
				$(this).closeTourDate();
				return false;
			}
		);
	}
);

jQuery.fn.extend({
	openTourDate: function() {
		var tourDateInfo = $(this).attr("href");
		var tourDateId = $(this).attr("id");
		$("#tourdates" + tourDateId).html("<div id=\"loader" + tourDateId + "\" class=\"loader\">Loading...</div>").slideDown();
		$.ajax({
			type: "GET",
			url: tourDateInfo,
			cache: true,
			success: function(html) {
				html = "<div id=\"tourdates_extra" + tourDateId + "\" class=\"hidden\">" + html + "</div>"
				$("#loader" + tourDateId).slideUp("normal",
					function() {
						$(this).remove()
					}
				);

				$("#tourdates" + tourDateId).prepend(html);
				$("#tourdates_extra" + tourDateId).slideDown();
			},
			error: function() {
				$("#tourdates" + tourDateId).html("<div class=\"error\">Error loading additional information.</div>");
			}
		});
		$("#more_tourdates" + tourDateId).hide();
		$("#less_tourdates" + tourDateId).show();
	  },
	  closeTourDate: function() {
		var tourDateId = $(this).attr("id");
		$("#tourdates" + tourDateId).slideUp("normal",
			function() {
				$("#tourdates" + tourDateId).empty();
			}
		);
		$("#less_tourdates" + tourDateId).hide();
		$("#more_tourdates" + tourDateId).show();
	}
});
