(function($){

	$.extend({
		spark: {
			version_info: {
				title: "Spark JSON Engine",
				version: "1.0",
				author: "Ian Whitcomb",
				website: "http://www.ooeygui.net"
			}
		}
	});

	$.extend($.spark, {
		replaceAll: function(needle, value, subject){
			if(subject){
				if(typeof needle != "object"){
					if(typeof value != Array){
						while(subject.indexOf(needle) != -1){
							subject = subject.replace(needle, value);
						}
					}
				} else {
					if(typeof value != "object"){
						for(i = 0; i < needle.length; i++){
							while(subject.indexOf(needle[i]) != -1){
								subject = subject.replace(needle[i], value);
							}
						}
					} else {
						for(i = 0; i < needle.length; i++){
							if(value[i]){
								while(subject.indexOf(needle[i]) != -1){
									subject = subject.replace(needle[i], value[i]);
								}
							}
						}
					}
				}
			}
			return subject;
		},
		request: function(o){
			
			o = {
				success: o.success || function(obj){},
				loading: o.loading || function(obj){},
				url: $.spark.replaceAll("\r","",$.spark.replaceAll("\n","",o.url)),
				engine: o.engine || "http://allatu.pair.com/itero/clients/spark/",
				callback: o.callback || "$.spark.success",
				type: o.type || "json",
				data: o.data,
				action: o.action || "%engine%?url=%url%&callback=%callback%&type=%type%&data=%data%"
			}
		
			var data = new Array();
			var n = 0;
			for(i in o.data){
				data[n] = i+"="+$.spark.replaceAll("\r","",$.spark.replaceAll("\n","",o.data[i]));	
				n++;
			}
			
			data = data.join(",");
		
			$.spark.loading = o.loading;
			$.spark.success = o.success;
			
			$.spark.loading();
			$(document).append("<script type=\"text/javascript\" src=\""+$.spark.replaceAll(["%engine%","%url%","%callback%","%type%","%data%"],[o.engine,o.url,o.callback,o.type,data],o.action)+"\"></script>");
			
		}
	});

})(jQuery);
