
/**
* Our loadAjax plugin.  It is used have a central place for loading 
*	and displaying content via AJAX.
*
* Running this plugin on an element causes a click() handler to be 
*	set on that element that, when clicked, will cause content from
*	a URL that we specificy to be loaded via AJAX, and displayed where 
*	we specify.
*
* The URL comes from the href value on the link.  It can also be
*	specified as an option if we're applying this plugin on a button
*	or some other element that doesn't have an href attribute.
*
* This plugin is "chained", which means that "this" amounts to 1 or more
*	DOM objects.
*
* @param array settings An optional array of settings for this function.
*
*	- string target A jQuery identifier.  The parent of the clicked on 
*		element is searched for this identifier.  Any matches will 
*		cause the content to be displayed there.  If not specified, 
*		content is displayed in the clicked on element's parent.
*
*	- string loading A jQuery identifier. If specified, the parent of the 
*		clicked on element will be searched for this identifier, it will be
*		unhidden with show() before the AJAX request is started, and hidden
*		with hide() when the query is successful.
*
*	- string callback A function to be run at the end of the click event.
*		An argument is passed into this function, which represents the 
*		item clicked on.
*
*	- boolean hide Set to false if we DON'T want to hide the link when it
*		is clicked on.  Default: true.
*
*	- string url URL to load via AJAX.  This overrides whatever is specific 
*	on original HTML object.
*
* Example usage:
*	$("#button").loadAjax({target: $(".target"), loading: $(".loading"});
*
* Example HTML this might be applied to:
*	<div>
*	<a href="http://someurl/path">click me</a>
*	<span style="display: none; " class="loading">Loading...</span>
*	<span style="target"></span>
*	</div>
*
*/
jQuery.fn.ajaxLoad = function(settings) {

	settings = jQuery.extend({
		target: "",
		loading: "",
		url: "",
		callback: "",
		hide: true
		}, settings);

	return(this.each(function() {

		var element = jQuery(this);

		element.click(function() {
			//alert(element.text()); // Debugging

			//
			// If we are displaying the loading dialogue, do so.
			//
			var loading;
			if (settings["loading"]) {
				loading = element.parent().find(settings["loading"]);
				loading.html("loading");
				loading.show();
			}

			//
			// If we have a target, assign it.  Otherwise, create a new
			// element and stick it on the end of what we clicked on.
			//
			var target;
			if (settings["target"] != "") {
				//
				// First we're going to search in the parent.
				//
				target = element.parent().find(settings["target"]);

				//
				// If no luck there, then search the entire document.
				//
				if (!target.length) {

					target = jQuery(settings["target"]);

					//
					// If *still* no luck, this is bad.  Throw an error.
					//
					if (!target.length) {
						var error = "Could not find target '" + settings["target"] + "'";
						throw(error);
					}

				}


			} else {
				target = jQuery('<span class="ajaxload-target">Loading...</span>');
				element.after(target);

			}

			//
			// If we don't have a URL, just use the href.
			//
			var url = $(this).attr("href");
			if (settings["url"]) {
				url = settings["url"];
			}

			//
			// Callback for when data is loaded.
			// We'll hide the loading dialogue, put the data into our 
			// target, and hide the original element that was clicked on.
			//
			var process_data = function(data) {

				//
				// If we had loading dialogue, hide it.
				//
				if (settings["loading"]) {
					loading.hide();
				}

				target.html(data);
				target.show();

				//
				// Hide the element we clicked on?
				//
				if (settings["hide"]) {
					element.hide();
				}

				//
				// If we have a callback, fire it.
				//
				if (settings["callback"]) {
					settings["callback"](element);
				}

				};

			jQuery.get(url, {}, process_data);

			//
			// Return false, so that the click does NOT send the
			// user's browser to a new page.
			//
			return(false);

		}); // End of click()

	})); // End of this.each()

}; // End of ajaxLoad() jQuery plugin.


