MG.namespace("MG.util.Async");
MG.util.Async = (function() {
	var defaults = {
		cache	: false,
		url		: 'rest.php',
		error	: function(o, httpStatus, ex) {
			MG.util.Modal.hideModal();
			if(httpStatus != "abort") {
				var s = [ httpStatus, ' ', o.status, '<br/>', ex, '<br/>', o.responseText ].join('');
				
				$.jGrowl(s, {sticky:true});
			}
		}
	};
	
	/**
	 * Returns a baseline xhr config
	 * @param	{Object}	custom 	JS object of kvp
	 * @return {Object}
	 */
	function getConfig(custom) {
		return $.extend({}, defaults, custom);
	}
	

	/**
	 * Displays async feedback to the user
	 * @param	{String}	json  JSON encoded return
	 * @return	{Boolean|Object}
	 */
	function handleMessaging(json) {
		try {
			// validate metadata
			var o = $.parseJSON(json);
			
			if(o.status && o.message) {
				if(o.status == 'failure') {
					$.jGrowl(o.message, { sticky : true });
				} else {
					$.jGrowl(o.message, {
						theme : 'standard'
					});
				}
			} else {
				return null;
			}
				
			return o;
		} catch(ex) {
			$.jGrowl(ex.toString(), {sticky:true});
		}
		
		return null;
	}
	
	return {
		getConfig			: getConfig,
		handleMessaging		: handleMessaging
	};
})();
