/* This file defines the connection library
**
*/

//TODO: make thread safe



//CON namespace
SKEL.CON = {
	request: false,
	url: "/query.php",
	callback: { },
	results: { }, //used for squery
	idCnt: 0,

	//the function is the first arg, callback is #2, all other args are args of the function
	query: function(module,funcName,callback){
		//encode all objects as JSON before sending

		var queryId = ++SKEL.CON.idCnt

		/*
		**
			func: funcName,
			queryId: queryId
		**
		*/
		
		var parameters = {
		};
		
		for(var i=3; i<arguments.length; i++){
			parameters['arg'+(i-2)] = arguments[i];
		}

		var header = "&module="+module+"&function="+SKEL.urlEncode(funcName)+"&queryId="+queryId;
		
		//var str = header+"&query="+parameters.toJSONString();
		var str = header+"&payload="+JSON.stringify(parameters);
		
		//var str = header+"&query="+parameters.toJSON();
		//var str = $.toJSON(header+"&query="+parameters);
		//var str = header+"&query=" + $(parameters).toJSON();
		//var str = header+"&query=" +parameters.toJSON();
		//var str = header+"&query=" + $(parameters).toJSON(parameters);

		SKEL.CON.send(SKEL.CON.url,str);

		SKEL.CON.callback[queryId] = callback; //store the callback in the id

		//returns a javascript object as the result
		//window.clearTimeout
		//window.setTimeout(timeoutFunc,500)
	},

	send: function(url,parameters){
		SKEL.CON.request = false;
		if(window.XMLHttpRequest){
			SKEL.CON.request = new XMLHttpRequest();
			if(SKEL.CON.request.overrideMimeType){
				//we can swap between xml or html here
				SKEL.CON.request.overrideMimeType('text/html');
			}
		} else if (window.ActiveXObject) { //IE
			try {
				SKEL.CON.request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					SKEL.CON.request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}

		if(!SKEL.CON.request){
			debug("Failure to set XMLHTTP");
			return false;
		}

		SKEL.CON.request.onreadystatechange = SKEL.CON.onStatusChange;
		SKEL.CON.request.open('POST',url,true);
		SKEL.CON.request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		SKEL.CON.request.setRequestHeader("Content-length", parameters.length);
		SKEL.CON.request.setRequestHeader("Connection","close");
		SKEL.CON.request.send(parameters);

		return true;
	},

	onStatusChange: function(){
		/*
		** 0 == The request is not initialized
		** 1 == The request has been set up
		** 2 == The request has been sent
		** 3 == The request is in process
		** 4 == The request is complete
		*/
		if(SKEL.CON.request.readyState == 2 || SKEL.CON.request.readyState == 1 || SKEL.CON.request.readyState == 3){
			//1 is the one that takes time
			//document.getElementById('loadImg').src = '/images/util/loading.gif';
		} else {
			//if 0 or 4, then clear it
			//document.getElementById('loadImg').src = '/images/nudging/spacer.gif';
		}
		if(SKEL.CON.request.readyState == 4){
			//we got a response from the server (possibly) (status should be "200" for "OK")
			result = SKEL.CON.request.responseText;

			//run this through JSON to decode it
			//obj = result.parseJSON();
			obj = JSON.parse(result);
			//obj = $.parseJSON(result);
			//obj = $(result).parseJSON();
			//obj = $(result).parseJSON(result);

			if(obj.error && obj.errorMsg){
				window.alert(obj.errorMsg);
			}
			
			if(obj.queryId > 0){
				//check to see if we have a callback waiting
				if(SKEL.CON.callback[obj.queryId] && SKEL.isFunction(SKEL.CON.callback[obj.queryId])){
					//run the callback
					SKEL.CON.callback[obj.queryId](obj.data);

					//erase the callback
					SKEL.CON.callback[obj.queryId] = null;
				}
			}
			//debug("object is back",obj);
			//document.getElementById('dayPopTitle').innerHTML = obj.test;
		}
	}
}
