//document.domain = "php5.com";
var Api = Class.create();
Api.prototype = {
	
	initialize:function() {
	},
	
	callMethod:function(APIMethod, params, listener, testingURL, attempts, server) {
		var thisCall = this;
		if (typeof params != "object")
		{//若参数列表不是对象，就将参数列表转换为对象，用来满足后面赋值
			params = {};
		}
		//给参数列表的method属性赋值
		params.method = APIMethod;
		var temp =  APIMethod.split(".");
		//服务器端控制器
		var RESTURLROOT = "/data/api/" + temp[0] + ".php";//testingURL;
		//调试服务器
		if (testingURL){
			RESTURLROOT = testingURL + RESTURLROOT;
		}
		//服务器端再其他主机时使用server参数
		else if (server){
			RESTURLROOT = server + RESTURLROOT;
		}
		//向服务器端传值列表
		var RESTURL = "src=js"
		for (var p in params)
		{//分析参数对象，组合成url串
			var str = new String(params[p]);
			//去掉ascII表中的一些特殊字符，如\x11
			var reg = /(\x0e)|(\x0f)|(\x10)|(\x11)|(\x12)|(\x13)|(\x14)/gi;
			var str = str.replace(reg, " ");
			//url编码
			RESTURL += "&" + p + "=" + encodeURIComponent(str);
		}
		//将向服务器传送的数据列表保存到参数对象，在回调函数需要时可以使用
		params.RESTURL = RESTURL;
		//错误尝试计数器
		var attempts = (attempts == undefined) ? 1 : attempts;
		//建立ajax请求对象
		var req = new AjaxRequest();
		//使用post方法向服务器发起请求
		req.sendPOST(RESTURLROOT, RESTURL, 
						function (response)
						{//成功处理
							thisCall.handleResponse(response.responseXML, APIMethod, params, response.responseText, listener);
						},
						function (response)
						{//失败处理
							if (attempts <= 3)
							{//错误重试
								attempts++;
								thisCall.callMethod(APIMethod, params, listener, testingURL, attempts, server);
							}
							else
							{//三次失败返回错误
								thisCall.handleResponse("", APIMethod, params, "", listener);
							}
						}
					);
	},
	
	handleResponse:function(responseXML, APIMethod, params, responseText, listener) {
		var success = false;
		if (responseXML != null)
		{//ff跨域返回xml对象无权限的解决方案
			responseXML = Try.these (
				function() {return new DOMParser().parseFromString(responseText, "text/xml")},
				function() {return responseXML}
			);
		}
		//确定执行是否成功
		success = Try.these (
			function() {return responseXML.documentElement.getAttribute("stat") == "1"},
			function() {if (responseText.substr(0, 2) == "ok") {responseText = responseText.substr(2, responseText.length); return true}}
		) || false;
		//listener是回调函数所属的类的对象
		listener = (listener) ? listener : this;
		//回调
		listener[this.getCallBackName(APIMethod)](success, responseXML, responseText, params);
	},
	
	getCallBackName:function (dotted) {
		return dotted.split(".").join("_") + "_call";
	}
}
var gdApi = new Api();

var AjaxRequest = Class.create();
AjaxRequest.prototype = {
	
	initialize:function() {
	},
	//私有方法
	__send__:function(url, sendMethod, callback, sendData, callFailure) {
		sendData = sendData + "&timeStamp=" + new Date().getTime();
		var newAjax = new Ajax.Request (
			url, 
			{method: sendMethod, parameters: sendData, onSuccess: callback, onFailure: callFailure}
		);
	},
	//post方式发送请求
	sendPOST:function(url, sendData, callback, callFailure) {
		this.__send__(url, "post", callback, sendData, callFailure);
	},
	//get方式发送请求
	sendGET:function(url, sendData, callback, callFailure) {
		this.__send__(url, "get", callback, sendData, callFailure);
	}
}
//请求过程中给与用户提示
/*var myGlobalHandlers = {
	onCreate: function() {
		$("savingStatus").show();
	},
	onComplete: function() {
		$("savingStatus").hide();
	}
};
Ajax.Responders.register(myGlobalHandlers);*/