// $Id$

// Function to replace the center pane on click
function edmCenterReplace(e) {
	edmReplace(e, "#center-pane");
}

// Function to replace the news pane on click
function edmNewsReplace(e) {
	edmReplace(e, "#news-pane");
}

// Common function to replace a page function on click
function edmReplace(e, name) {
		e.preventDefault();
		
		// Event target is not always the <a>, but can be a child, like <img>. Loop back to the <a> and use the HREF from there.
		target = e.target;
		while (target != null && target.tagName != 'A') {
			target = target.parentNode;
		}
		href = target.href;
		$(name).load(href + " " + name, function() {
			// Reinitialize the click handlers
			edmInit();
		});
}

// Function to initialize the click handlers.
function edmInit() {
	$(".center-a").click(edmCenterReplace);
	$(".news-a").click(edmNewsReplace);
}

// Invoked when the document is ready.
$(document).ready(function(){

	// Initialize permanent handlers.
	// Some <a> have a no-redirect class, and should keep their default behavior	
	$("#menu td a").each(function(index) {
		cl = $(this).attr("class");
		if (cl == null || cl.toLowerCase().indexOf("no-redirect") == -1) {
			$(this).click(edmCenterReplace);
		}
	});

	$(".menu-a").click(edmCenterReplace);

	// Initialize the click handlers
	edmInit();

	// Ajax debug, just in case.
	$("#log").ajaxError(function() {
		alert("Ajax error!");
		$(this).text('Triggered ajaxError handler.');
	});
});

