/**
 * Used for wrapping jQuery's .ajax function. It adds some much-needed standardization for our system's millions of AJAX calls.
 * Usage:
		jQuery.go({
			url: "/panel/ajax/thing.php",
			mode: "prompt",
			type: "create",
			get: "var=val;var2=val2",
			post: {
				var: "val",
				var2: "val2"
			},
			handle: {
				200: function(json) {  },
				300: function(json) {  },
				def: function(json) {  }
			}
		});
 *
 *
 * ==> IF the response isn't a JSON object, the result will simply be printed to the body via jQuery("body").append() <==
 * 
 * @author Torgie - 03/04/09
 */

jQuery.extend({
	// Define our function "go" with 
	go: function(s) {
		// We can't do much of anything without a URL!
		if(typeof s.url == "undefined" || s.url == "")
			return false;

		// Build up our GET variables
		var gets = new Array;
		if(typeof s.mode != "undefined" && s.mode != "")
			gets.push("mode=" + s.mode);
		if(typeof s.type != "undefined" && s.type != "")
			gets.push("type=" + s.type);
		if(typeof s.get != "undefined" && s.get != "")
			gets.push(s.get);

		// Define the GET string
		var getString = (gets.length > 0) ? "?" + gets.join("&") : "";

		// Run the AJAX
		jQuery.ajax({
			type: "POST",
			dataType: "json",
			url: s.url + getString,
			data: s.post,
			success: function(json) {
				if(typeof json.code != "number")
					return jQuery.showError(json);

				// default handler for 401 permissions errors
				if(json.code == 401 && typeof s.handle[401] == "undefined")
				{
					if(typeof jQuery("#GB_frame").attr("id") == "string") {
						GB_HEIGHT = json.height;
						GB_WIDTH = json.width;
						GB_position();
						jQuery("#GB_frame").html(json.html);
					}
					else {
						GB_show({
							caption: json.caption,
							html: json.html,
							width: json.width,
							height: json.height
						});
					}
					return true;
				}

				var callbackExecuted = false;
				var showRedBoxOnDefault = false; // if a default handler is defined, and it returns true, the red error box will also appear

				// If there are handlers defined, try to use them
				if (typeof s.handle != "undefined") {
					// Execute the appropriate callback
					for (handledCode in s.handle) {
						if (handledCode == json.code) {
							// Run the callback
							s.handle[handledCode](json);
							callbackExecuted = true;
						}//if
					}//for

					// Execute the default if necessary, and if one exists
					if (!callbackExecuted && typeof s.handle.def != "undefined"){ 
						var defReturn = s.handle.def(json);
						callbackExecuted = true;
						if(defReturn == true)
							showRedBoxOnDefault = true;
					}
				}

				if(!callbackExecuted || showRedBoxOnDefault)
				{
					// If no callbacks were defined, default to redirect or print error message in a popup
					// Quicker development, no change to production behavior
					if(typeof json.url != "undefined"){
						window.location = json.url;
					} else if(json.code == 302) {
						// If no location was provided but a 302 was passed, refresh the page
						window.location = window.location;
					} else if(json.code != 200) {
						jQuery.showError(json.msg);
					}
				}

			}//success
		});//jQuery.ajax

		return true;
	},//end function go

	showError: function(msg){
		jQuery("#jqErrorMsg").remove();
		jQuery("body").append('<div id="jqErrorMsg" style="display:none; position:fixed; top:0; left:40px; background:#8F0000; color:white; font-weight:bold; padding:3px 10px; font-size:10px;">' + msg + ' <a href="javascript:void(0);" id="jqErrorClear" style="color:#ccc">close</a></div>');
		jQuery("#jqErrorMsg").css({ display: "block", opacity: 0, zIndex: 9999 }).animate({opacity: 1}, 200);
		jQuery("#jqErrorClear").click(jQuery.hideError);
	},

	hideError: function(){
		jQuery("#jqErrorMsg").animate({opacity: 0}, 100, function(){
			jQuery("#jqErrorMsg").remove();
		});
	}

});//end extend


