//  Event function
(function(){
	var _Event = function(args){
			this.elements = [];
			if(args.length === 0){
				return null;
			}else{
				for(var i = 0, len = args.length; i < len; i++){
					if(typeof args[i] === 'string'){
						var el = document.getElementById(args[i]);
						this.elements.push(el);
					}else{
						this.elements.push(args[i]);
					}
				}
			}
	};

	_Event.prototype = {
		each : function(fn){
			if(typeof fn !== 'function') return;
			for(var i = 0, len = this.elements.length; i < len; i++){
				fn.call(this,this.elements[i]);
			}
			return this;
		},

		addEvent : function(type, fn){
			var _add = function(el){
				if(typeof fn !== 'function') return;
				function wrapper(evt){
					if(el){
						fn.call(el, evt);
					}
				}
				if(el){
					document.addEventListener ? el.addEventListener(type, wrapper, false) : el.attachEvent('on'+type, wrapper);
				}
			};

			this.each(function(el){
				_add(el);
			});

			return this;
		},

		removeEvent : function(type, fn){
			var _remove = function(el){
				if(typeof fn !== 'function') return;
				function unwrapper(evt){
					fn.call(el, evt);
				}
				document.removeEventListener ? el.removeEventListener(type, unwrapper, false) : el.detachEvent('on'+type, unwrapper);
			};
			this.each(function(el){
				_remove(el);
			});

			return this;
		},

		stopEvent : function(evt){
			evt = evt || window.event;
			if(evt){
				evt.preventDefault ? evt.preventDefault() : evt.returnValue = false;
				evt.stopPropagation ? evt.stopPropagation() : evt.cancelBubble = true;
			}
		},

		preventDefault : function(evt){
			evt = evt || window.event;
			if(evt){
				evt.preventDefault ? evt.preventDefault() : evt.returnValue = false;
			}
		},

		stopPropagation : function(evt){
			evt = evt || window.event;
			if(evt){
				evt.stopPropagation ? evt.stopPropagation() : evt.cancelBubble = true;
			}
		}
	};

	window.$IdEvent = function(){
		return new _Event(arguments);
	};
})();

//	AJAX function
var AJAX = function(){
	this.xhr = null;
};
AJAX.prototype = {
	request : function(type, url, callback, param){
		var _this = this;

		this.xhr = this.createXHR();
		param = param || null;
		this.xhr.onreadystatechange = function(){
			if (_this.xhr.readyState == 4) {
				if (_this.xhr.status == 200) {
					var responseInfo;
					
					if(_this.xhr.responseXML){
						responseInfo = _this.xhr.responseXML.documentElement ? _this.xhr.responseXML : _this.xhr.responseText;
					}else{
						responseInfo = _this.xhr.responseText;
					}
					callback.success(responseInfo);
				}else {
					callback.failure('服务器忙，请重新操作！');
				}
			}
		};
		
		this.xhr.open(type, url, true);
		this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
		if (type != 'POST') {
			param = null;
		}
		this.xhr.send(param);
	},

	createXHR : function(){
		var method = [
					function() {return new XMLHttpRequest(); },
					function() {return new ActiveXObject("Microsoft.XMLHTTP"); },
					function() {return new ActiveXObject("Msxml2.XMLHTTP"); }
		];
		for(var i = 0, len = method.length; i < len; i++){
			try{
				method[i]();
			}catch(e){
				continue;
			}
			this.createXHR = method[i];
			return method[i]();
		}
	},
	
	abortXHR : function(time){
		var _this = this;
		
		setTimeout(function(){
			_this.xhr.abort();
		}, time);
	}
};
