/**
 * @namespace com.thesis.ajax.DataLink
 * @author 钟军锐 August.R@263.net
 * imports com.thesis.ajax.Connection
 * imports com.thesis.base.Detect
 * imports com.thesis.util.Event
 */

/** @id DataLink */
function DataLink(oConnection){
	this.Connection=oConnection;
	this.sURL = "";
	this.sParams = "";
	this.XmlHttpRequest = undefined;
	this.sCompress = undefined;
	this.sDecompress = undefined;
	
	if(typeof DataLink._initialized == "undefined"){
		DataLink._initialized = true;
		/** @id compressData */
		DataLink.prototype.compressData = function(sMsg){
			if(this.Connection.bCompress){
				this.sCompress = sMsg;
				//do something about compress here
				return this.sCompress;
			}
			else{
				return sMsg;
			}
		};
		/** @id decompressData */
		DataLink.prototype.decompressData = function(sData){
			if(this.Connection.bDecompress){
				this.sDecompress = sData;
				//do something about decompress here
				return this.sDecompress;
			}
			else{
				return sData;
			}
		};
		/** @id createXmlHttpRequest */
		DataLink.prototype.createXmlHttpRequest = function(){
			if(typeof XMLHttpRequest=="undefined" && window.ActiveXObject){
				this.__arrSignatures__=["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP.2.0",
										"MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
				for(var i=0;i<this.__arrSignatures__.length;i++){
					try{
						return new ActiveXObject(this.__arrSignatures__[i]);
					}
					catch(oError){
						//ignore
					}
				}
				throw new Error("MSXML is not installed on your system.");
			}
			else{
				return new XMLHttpRequest();
			}
			throw new Error("You browser doesn't support HTTP requests.");
		};
		/** @id callbackFunction */
		DataLink.prototype.callbackFunction = function(sData){
			uEvent.Listener.notify(this,"Arrived",this.decompressData(sData));
		};
		/** @id initialize */
		DataLink.prototype.initialize = function(oRoot){
			delete this.XmlHttpRequest;
			this.XmlHttpRequest = this.createXmlHttpRequest();
			this.XmlHttpRequest.onreadystatechange = function(){
				if(oRoot.XmlHttpRequest.readyState == 4){
					oRoot.callbackFunction(oRoot.XmlHttpRequest.responseText);
				}
			};
		};
		/** @id baseGet */
		DataLink.prototype.baseGet = function(){
			this.sURL=this.Connection.sURL;
			for(var i=0;i<this.Connection.arrParam.length;i++){
				this.sURL += this.sURL.indexOf("?")==-1 ? "?":"&";
				this.sURL += encodeURIComponent(this.Connection.arrParam[i].sParamName) + "=" + 
								encodeURIComponent(this.compressData(this.Connection.arrParam[i].sParamValue));
			}
			if(typeof this.XmlHttpRequest != "undefined" && this.XmlHttpRequest !=null){
				uEvent.Listener.notify(this,"Requesting");
				this.XmlHttpRequest.open("get",this.sURL,true);
				this.XmlHttpRequest.send(null);
			}
		};
		/** @id basePOST */
		DataLink.prototype.basePOST = function(){
			this.sURL=this.Connection.sURL;
			this.sParams="";
			for(var i=0;i<this.Connection.arrParam.length;i++){
				this.sParams += i>0 ? "&":"";
				this.sParams += encodeURIComponent(this.Connection.arrParam[i].sParamName) + "=" + 
								encodeURIComponent(this.compressData(this.Connection.arrParam[i].sParamValue));
			}
			if(typeof this.XmlHttpRequest != "undefined" && this.XmlHttpRequest !=null){
				uEvent.Listener.notify(this,"Requesting");
				this.XmlHttpRequest.open("post",this.sURL,true);
				this.XmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				this.XmlHttpRequest.send(this.sParams);
			}
		};
		/** @id request */
		DataLink.prototype.request = function(){
			this.initialize(this);
			if(this.Connection.sMethod.toUpperCase()=="GET"){
				this.baseGet();
			}
			else if(this.Connection.sMethod.toUpperCase()=="POST"){
				this.basePOST();
			}
		};
	}
}

