/* 
 * SimpleTuition JSON XMLHttpRequest
 */
;(function(){

	var f = function(){};
	var p = f.prototype;
	 
	/**
	 * Common defaults shared by all XMLHttpRequests
	 */
	p.defaults = { 
		type: "GET",
		dataType: "json",
		cache: false,
		timeout: 30000,
		async: false,
		/**
		 * If our XMLHttpRequest fails then we 
		 * handle it using our error logger as well as
		 * notify the server by drawing a new image pixel
		 * @param xhr - XMLHttpResponse the response that comes back
		 * @param e - error message
		 * @param st - status code
		 */
		error: function(xhr,e,st){
			log.error("JSON FAILURE. STATUS:"+xhr.status+" ("+ xhr.statusText +"), ERROR:"+e+","+st,st);
		}
	};
	
	/**
	 * Sets the defaults on the parameters package
	 * @param opts - options for the json call
	 * @return obj array
	 */
	p.init = function(opts) { 
		return jQuery.extend([],this.defaults,opts);
	};
	
	/*
	 * Send a XMLHttpRequest using ajax/json types
	 * @param opts - options for the json call
	 * @return false 
	 */
	 p.send = function(opts) {
	 	var reqOpts = this.init(opts);
	 	this.log(reqOpts);
	 	/** TODO: instead of substring(0,1) try startsWith type of fn **/
	 	//if ((reqOpts.url).substring(0,1)!=$T.global.rootPath) {
	 	//	reqOpts.url = $T.global.rootPath + reqOpts.url;
	 	//}
	 	$.ajax(reqOpts);
	 	return false;
	 };	 
	
	
	/**
	 * Log the request as it goes out during debug mode
	 * @param o - the options defined in the json call
	 */
	 p.log = function(o) { 
	 	log.debug("NEW JSON REQUEST: TYPE="+o.type+" URL="+o.url,o);
	 }
	
	/*
	 * Create the new JSON Object within the $T domain
	 * and a global scope json exists which should be
	 * refactored
	 */
	json = $T.json = new f();
	
})();

