/*
 * REQUIRES JQUERY
 *
 *
 * Looks for links on the page of the form:
 *	<a href="----" ajaxtarget="id">
 *	<form method="get" action="----" ajaxtarget="id">
 *
 * and uses the ajaxtarget attribute to make an ajax request to the href
 * in the tag, appending ajaxtarget=id to the URL's query string.  The returned
 * HTML is placed into the DOM element with the given id.
 * 
 * See also: ajaxify.cfm
 * 
 * TODO:
 * 	- Manipulate the current page's URL's hash to make bookmarking possible.
 *  - Forms: take the form's ACTION attribute into account on the URL.
 *
 * Version 1.3 - 20071115
 * 
 * 	- Added HACKy version of url#hash tracking.  Makes assumtions based on the
 * 		needs of the RT/Dygert catalog and requires the queryString object.
 * 
 * Version 1.2 - 20071115
 * 
 * 	- Added support for forms; GET method only.
 * 
 * 
 * Version 1.1 - 20071115
 * 
 *  - Original version.  Basic <A> links.
 *
*/

//--- Applies handling function to relevant links
function ajaxify() {
	$("a[ajaxtarget]").bind("click", ajaxifyOnclick);
	$("form[ajaxtarget]").bind("submit", ajaxifyOnsubmit);
}

//--- Called as an onClick on any ajaxified links
function ajaxifyOnclick(e){

	var target = $(this).attr("ajaxtarget");
	var url = $(this).attr("href");
	return ajaxload(url, target);
}

function ajaxifyOnsubmit(e){
	//--- Currently only handles GET submissions
	if( $(this).attr("method").toUpperCase() != "GET" ) return;
	
	var target = $(this).attr("ajaxtarget");
	
	var qs = "?" + $(this).serialize();
	
	//--- TODO: make this incorporate the form's ACTION attribute, too...
	var url = qs;
	
	return ajaxload(url, target);
}

//--- Called when AJAX request finishes... makes sure the new code's links are ajaxified.
function ajaxifyDone(url, target) {
	
	$( ajaxifyDomtargetFromTarget(target) ).removeClass("loading");
	
	//--- TODO: make this target only the new code?
	ajaxify();
}

function ajaxload(url, target) {

	//--- CSS selector to determine what code is replaced on the page
	//	There should probably be more robust detection of what info is passed
	var domtarget = "#" + target;


	//--- Alter the page's "bookmark" (url hash section) to reflect this change. 
	ajaxifyBookmark(url);


	//-- Build the request URL
	//	Use the original URL and append the mode variables.
	//	Also add the "?" if necessary
	url += (url.indexOf("?") < 0?"?":"");
	url += "&ajaxtarget=" + target;

	$(domtarget).addClass("loading").load(url, {}, function(){ ajaxifyDone(url, target) } );
	
	return false;
}

function ajaxifyDomtargetFromTarget(t) { return "#" + t; }

function ajaxifyBookmark(qs){
	//--- HACK: Throw up the requested query on the hash of the page.
	//	This assumes that all requests are to the same page and that the passed
	//	URL is JUST a query string.  Also requires the queryString object defined
	//	elsewhere
	var hashqs = new queryString( window.location.hash.substring(1,window.location.hash.length) ); //Remove # and create new qs object
	hashqs.append( qs );
	window.location.hash = hashqs.get();
}


//--- Ajaxify the links/forms once they're loaded.
$(ajaxify);
