

var Ajax = new Class();
Ajax.prototype = {
	Ajaxs: new Array(),
	targetUrl: '',
	sendString: '',
	resultHandle: function(){},
	initialize: function (config){
		this.config = common.extend({
			lang_loading : '正在通信，请稍候...',
			lang_load_failed : '通信失败，请重新尝试！',
			recvType: 'XML',								// 返回方式，HTML: 、XML
			loadingId: 'ajaxLoading',						// loading 显示对像
			postIframe: 'ajaxPostFrame',					// 框架POST方式的框架ID及NAME
			loadingAlign: 3									//0:不改变位置、1：上、2：右、4：下、8：左、16：中，可以两个相加，比如3为右上角
		},
		config||{});
		
		this.XMLHttp = this.createXMLHttp();
	},
	
	createXMLHttp: function() {
		var	request	= false;
		if(window.XMLHttpRequest) {
			request	= new XMLHttpRequest();
			if(request.overrideMimeType) {
				request.overrideMimeType('text/xml');
			}
		} else if(window.ActiveXObject)	{
			var	versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
			for(var	i=0; i<versions.length;	i++) {
				try	{
					request	= new ActiveXObject(versions[i]);
					if(request)	{
						return request;
					}
				} catch(e) {}
			}
		}
		return request;
	},
	
	processHandle: function() {
		if(this.XMLHttp.readyState == 4) {
			if(this.XMLHttp.status == 200)	{
				for(i = 0; i < this.Ajaxs.length; i++) {
					if(this.Ajaxs[i] == this.targetUrl) {
						this.Ajaxs[i] = null;
					}
				}

				this.hideLoading();

				if(this.config.recvType == 'HTML') {
					this.resultHandle(this.XMLHttp.responseText);
				} else if(this.config.recvType == 'XML')	{
					this.resultHandle(this.XMLHttp.responseXML.lastChild ? this.XMLHttp.responseXML.lastChild.firstChild.nodeValue : '');
				}
			} else {
				popup.show({content:this.config.lang_load_failed, contentType: 3});
			}
			return;
		} else {
			this.loading();
		}
	},

	get: function(targetUrl, resultHandle) {
		targetUrl += '/inajax' + URL_SEP + '1';
		if(in_array(targetUrl, this.Ajaxs)) {
			return false;
		} else {
			 this.Ajaxs.push(targetUrl);
		}
		this.targetUrl = targetUrl;
		this.XMLHttp.onreadystatechange = this.processHandle.bind(this);
		this.resultHandle = resultHandle;
		if(window.XMLHttpRequest) {
			this.XMLHttp.open('GET', this.targetUrl);
			this.XMLHttp.send(null);
		} else {
			this.XMLHttp.open("GET", targetUrl, true);
			this.XMLHttp.send();
		}
	},

	post: function(formid, resultHandle)	{
		var targetUrl = $(formid).action = $(formid).action + '/inajax' + URL_SEP + '1';
		if(in_array(targetUrl, this.Ajaxs)) {
			return false;
		} else {
			this.Ajaxs.push(targetUrl);
		}
		this.targetUrl = targetUrl;

		var iframe = $(this.config.postIframe);
		
		/*
		由于不能取消带参数的绑定事件，而又要避免重复绑定，因此每次POST都将以前的框架移除后重新生成
		没找到好方法之前暂时这样
		*/
		if (iframe) {
			iframe.remove();
		}
		if (is_ie) {
			iframe = $('#body').create('<iframe name="' + this.config.postIframe + '" id="' + this.config.postIframe + '"></iframe>')
			if (!IS_DEBUG) {
				iframe.style.display = 'none';
			}
		} else {
			iframe = $('#body').create('iframe', {display: 'none'}, {id: this.config.postIframe}, {'name': this.config.postIframe});
		}

		this.loading();
		
		$(formid).target = this.config.postIframe;
		this.resultHandle = resultHandle;
		if (!iframe.attach) {
			// 避免重复绑定
			iframe.on({'load' : this.postLoad.bind(this)});
			iframe.attach = 1;
		}

		$(formid).submit();
		return false;
	},
	
	postLoad: function () {
		for(i = 0; i < this.Ajaxs.length; i++) {
			if(this.Ajaxs[i] == this.targetUrl) {
				this.Ajaxs[i] = null;
			}
		}
//		alert(this.config);
		var postIframe = this.config.postIframe;
		if(is_ie) {
			var s = $(postIframe).contentWindow.document.XMLDocument.text;
		} else {
			var s = $(postIframe).contentWindow.document.documentElement.firstChild.nodeValue;
		}
		this.hideLoading();
		this.resultHandle(s);
	},
	
	loading: function (){
		var obj = $(this.config.loadingId);
		if (!obj) {
			obj = $('#body').create('div', {display: 'none'}, {id: this.config.loadingId}, {className: 'loading'});
		}

		var align = parseInt(this.config.loadingAlign);
		if (align) {
			obj.innerHTML = this.config.lang_loading;
			obj.style.display = '';
			var sLeft = 0, sTop = 0;
			if (align & 16) {
				sLeft = (body.clientWidth - obj.offsetWidth) / 2;
				sTop = (body.clientHeight - obj.offsetHeight) / 2 + body.scrollTop;
			} else {
				if (align & 8) {
					sLeft = 0;
				}
				if (align & 4) {
					sTop = body.clientHeight - obj.offsetHeight + body.scrollTop;
				}
				if (align & 2) {
					sLeft = body.clientWidth - obj.offsetWidth;
				}
				if (align & 1) {
					sTop = body.scrollTop;
				}
			}
			obj.style.left = sLeft + 'px';
			obj.style.top = sTop + 'px';
		} else {
			obj.innerHTML = '<img src="' + IMG_URL + 'loading.gif" style="vertical-align: middle; margin: 5px;"> ' + this.config.lang_loading;
		}
	},
	
	hideLoading: function () {
		if (parseInt(this.config.loadingAlign)) {
			$(this.config.loadingId).style.display = 'none';
		} else {
			$(this.config.loadingId).innerHTML = '';
		}
	}
};

//var ajax = new Ajax();
