﻿// 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.$EVENT = function(){
		return new _EVENT(arguments);
	};
})();

//	AJAX function
function Ajax(url, option) {
	var cl = arguments.callee;
	if (!(this instanceof cl)) return new cl(url, option);

	function _getXHR() {
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else if (ActiveXObject) {
			try { return new ActiveXObject('MSXML2.XMLHTTP'); }
			catch(e) { return new ActiveXObject('Microsoft.XMLHTTP'); }
			return null;
		}
	}

	var loc    = location.toString();
	//var domain = '';
	//try { domain = loc.match(/^https?:\/\/([a-z0-9_\-\.]+)/i)[1]; } catch(e) {}

	this._url = url;
	this._options  = new Object;
	this._headers  = new Object;
	this._options = {
		type   :"xhr",
		method :"post",
		proxy  :"",
		timeout:0,
		onload :function(req){},
		ontimeout:function(req){},
		jsonp_charset : "utf-8"
	};

	this.option(option);

	var _opt = this._options;

	_opt.type   = _opt.type.toLowerCase();
	_opt.method = _opt.method.toLowerCase();

	if (typeof window.__jindo2_callback == "undefined") {
		window.__jindo2_callback = new Array();
	}

	switch (_opt.type) {
		case "get":
		case "post":
			_opt.method = _opt.type;
			_opt.type   = "xhr";
		case "xhr":
			this._request = _getXHR();
			break;

	}
};

Ajax.prototype._onload = function() {
	if (this._request.readyState == 4) {
		this._options.onload(Ajax.Response(this._request));
	}
};

Ajax.prototype.request = function(oData) {
	var t   = this;
	var req = this._request;
	var opt = this._options;
	var data, v,a = [], data = "";
	var _timer = null;

	if (typeof oData == "undefined" || !oData) {
		data = null;
	} else {
		for(var k in oData) {
			v = oData[k];
			if (typeof v == "function") v = v();
			a[a.length] = k+"="+encodeURIComponent(v);
		}
		data = a.join("&");
	}

	req.open(opt.method.toUpperCase(), this._url, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
	req.setRequestHeader("charset", "utf-8");
	for(var x in this._headers) {
		if (typeof this._headers[x] == "function") continue;
		req.setRequestHeader(x, String(this._headers[x]));
	}

	if (typeof req.onload != "undefined") {
		req.onload = function(rq){ clearTimeout(_timer); t._onload(rq) };
	} else {
		req.onreadystatechange = function(rq){ clearTimeout(_timer); t._onload(rq) };
	}

	if (opt.timeout > 0) {
		_timer = setTimeout(function(){ try{ req.abort(); }catch(e){}; opt.ontimeout(req); }, opt.timeout * 1000);
	}

	req.send(data);

	return this;
};

Ajax.prototype.abort = function() {
	this._request.abort();

	return this;
};

Ajax.prototype.option = function(name, value) {
	if (typeof name == "undefined") return "";
	if (typeof name == "string") {
		if (typeof value == "undefined") return this._options[name];
		this._options[name] = value;
		return this;
	}

	try { for(var x in name) this._options[x] = name[x] } catch(e) {};

	return this;
};

Ajax.prototype.header = function(name, value) {
	if (typeof name == "undefined") return "";
	if (typeof name == "string") {
		if (typeof value == "undefined") return this._headers[name];
		this._headers[name] = value;
		return this;
	}

	try { for(var x in name) this._headers[x] = name[x] } catch(e) {};

	return this;
};

Ajax.Response  = function(req) {
	if (this === Ajax) return new Ajax.Response(req);
	this._response = req;
};

Ajax.Response.prototype.xml = function() {
	return this._response.responseXML;
};

Ajax.Response.prototype.text = function() {
	return this._response.responseText;
};

Ajax.Response.prototype.json = function() {
	if (this._response.responseJSON) {
		return this._response.responseJSON;
	} else if (this._response.responseText) {
		try {
			return eval("("+this._response.responseText+")");
		} catch(e) {
			return {};
		}
	}

	return {};
};

Ajax.Response.prototype.header = function(name) {
	if (typeof name == "string") return this._response.getResponseHeader(name);
	return this._response.getAllResponseHeaders();
};

