$(document).ready(function () {
	$(".agenda_list a:not(a.internal),#tafel a:not(a.internal)").click(function (event) {
		// 1. Close any open overlays
		$("#overlay_left").css("display", "none");
		$("#overlay_right").css("display", "none");
		$("#wide_overlay").css("display", "none");
		
		var anchorElement = $(this);
		
		// 2. Determine the correct overlay position (left / right / wide)
		if ($(this).hasClass("wide_overlay")) {
			var destination = $("#wide_overlay");
		} else {
			var index = $(".agenda_list li").index($(this).parent().parent());
			if (index % 3 == 1) { var destination = $("#overlay_left");	}
			else { var destination = $("#overlay_right"); }
		}

		// 3. Load the requested agenda item in to the overlay
		$.get(
			anchorElement.attr("href"), 
			function(data) { 
				destination.html(data);
				
				// Open pop-up with URL if overlay contains one
				if (anchorElement.hasClass("external")) {
					window.open($("p", destination).html());
				} else if($(event.target).hasClass("internlink")) {
					window.location = $("base").attr("href")+$("p", destination).html();
				} else {			
					destination.css("top", anchorElement.offset().top);
					destination.toggle();
	
					$(document).ready(function (event) {				
						$(".overlay a.close_overlay, .overlay a.close_overlay_bottom").click(function (event) {
							$(this).parent().toggle();
							return false;
						});					
					});
				}
			}
		);

		// 4. Make sure browser stays put
		return false;
	});
});