/**
 * @namespace com.thesis.ajax.Command
 * @author 钟军锐 August.R@263.net
 * imports com.thesis.ajax.Connection
 */

/** @id Command */
function Command(sURL){
	this.sURL = sURL;
	/** @type {Array} */
	this.arrParam = new Array();
	this.ParamValueSource = undefined;
	/** @type {Connection} */
	this.Connection = new Connection();
	
	if(typeof Command._initialized == "undefined"){
		Command._initialized = true;
		/** @id initialize */
		Command.prototype.initialize = function(){
			this.Connection.bCompress = false;
			this.Connection.bDecode = false;
			this.Connection.bDecompress = false;
			this.Connection.bEncode = false;
			this.Connection.sURL = this.sURL;
			this.Connection.sMethod = "POST";
		}
		/** @id clearParams */
		Command.prototype.clearParams = function(){
			delete this.arrParam;
			this.arrParam = new Array();
		};
		/** @id removeParam */
		Command.prototype.removeParam = function(sParamName){
			for(var i=this.arrParam.length-1;i>-1;i--){
				if(this.arrParam[i].sParamName == sParamName){
					this.arrParam.splice(i,1);
				}
			}
		};
		/** @id addParam */
		Command.prototype.addParam = function(sParamName,sParamValue,bIndirect,bUnique){
			if(bUnique){
				this.removeParam(sParamName);
			}
			this.arrParam.push({sParamName: sParamName,sParamValue: sParamValue,bIndirect: bIndirect,bUnique: bUnique});
		};
		/** @id getData */
		Command.prototype.getData = function(sPath){
			var oTemp = this.ParamValueSource;
			var arrPath = sPath.split(".");
			for(var i in arrPath){
				oTemp = oTemp[arrPath[i]];
			}
			return oTemp;
		};
		/** @id toConnection */
		Command.prototype.toConnection = function(){
			this.Connection.sURL = this.sURL;
			this.Connection.clearParams();
			for(var i=0;i<this.arrParam.length;i++){
				if(this.arrParam[i].bIndirect){
					this.Connection.addParam(this.arrParam[i].sParamName,this.getData(this.arrParam[i].sParamValue),this.arrParam[i].bUnique);
				}
				else{
					this.Connection.addParam(this.arrParam[i].sParamName,this.arrParam[i].sParamValue,this.arrParam[i].bUnique);
				}
			}
			return this.Connection;
		};
	}
	this.initialize();
}
