/**
* Our AJAX debugging jQuery plugin.
*
* This plugin holds/eventually will hold a number of functions for 
* debugging AJAX calls from jQuery.
*
* @author Douglas Muth <dmuth@guesswho.com>
*/


/**
* Placeholder function for now.
*/
jQuery.ajaxDebug = function() {
}


/**
* This function wraps a call to $.getJSON() so that we get some debugging
* output, includign the URL that was called.
*/
jQuery.getJSONDebug = function(url, data, callback) {

	//
	// Loop through our array of data and create the full URL we're calling.
	//
	var debug_url = "";
	for (key in data) {

		if (debug_url != "") {
			debug_url += "&";
		}

		debug_url += escape(key) + "=" + escape(data[key]);

	}

	debug_url = location.protocol + "//" + location.hostname + url 
		+ "?" + debug_url;

	//alert(debug_url); // Debugging
	var debug_output = "";
	debug_output += "AJAX GET JSON request to this URL:\n\n" 
		+ debug_url + "\n\n";

	//
	// Wrap our callback function in a function that will display the 
	// data returned first.
	//
	var callback_debug = function(data) {

		debug_output += "Data returned:\n\n";
		debug_output += jQuery.ajaxDebug.arrayDump(data);
		alert(debug_output);

		callback(data);
	};

	jQuery.getJSON(url, data, callback_debug);

} // End of getJSONDebug()


/**
* This function walks through an array and dumps the values in plaintext
*	format for debugging purposes.
*
* @param mixed data The value we want to dump.  Can be a multi-dimensional 
*	array.
*
* @param string prefix The prefix to print before each item.  
*	Used when calling itself recursively.
*
* @return string A gigantic string with values separated by newlines.
*/
jQuery.ajaxDebug.arrayDump = function(data, prefix) {

	var retval = "";

	if (!prefix) {
		prefix = "";
	}

	if (typeof(data) == "object") {
		//
		// Array?  Loop through it.  If another array if found, call 
		// ourselves again, otherwise add the value to our retval.
		//
		for (var key in data) {

			if (typeof(data[key]) == "object") {
				prefix_param = prefix + key + ": ";
				retval += jQuery.ajaxDebug.arrayDump(data[key], prefix_param);

			} else {
				retval += prefix + key + ": " + data[key] + "\n";

			}

		}

	} else {
		//
		// Not an array.  Add the value onto the return value.
		//
		retval += data + "\n";

	}

	return(retval);

} // End of arrayDump()


