Array.prototype.inArray = function(element) {
	var inArray = false;
	for (var i=0;i<this.length;i++) {
		if (this[i]==element) {
			inArray = true;
		}
	}
	return inArray;
}
Function.prototype.bind = function(object) {
    var method = this
    return function() {
        return method.apply(object, arguments) 
    }
}
var as = {
	$: function(id) {
		return document.getElementById(id);
	},
	getElementPosition: function(element) {		
		var l = 0;
		var t = 0;
		while (element) {
			l += element.offsetLeft;
			t += element.offsetTop;
			element = element.offsetParent;
		}	
		return {"left":l, "top":t};
	},
	getBTN: function(tname,cont) {
		if (!cont) {var cont = document}
		return cont.getElementsByTagName(tname);
	},
	style: function(element,obj) {
		for (var i in obj) {
			element.style[i] = obj[i];	
		}
	},
	getBCN: function(cname,tname,cont) {
		var cnameRE = new RegExp("(^| )"+cname+"($| )","");
		var els = [], preEls;
		if (!cont) {var cont = document;}
		if (!tname) {var tname = "*"}
		preEls = as.getBTN(tname,cont);
		for (var i=0,l=preEls.length;i<l;i++) {
			if (preEls[i].className.match(cnameRE)) {
				els.push(preEls[i]);
			}
		}
		return els;
	},
	map: function(array,fn) {
		for (var i=0,l=array.length;i<l;i++) {
			fn(array[i],i);
		}
	},
	addEvent: function(element,evt,fn) {
		if (element.addEventListener) {
			element.addEventListener(evt,fn,false);
		}
		else if (element.attachEvent) {
			element.attachEvent("on"+evt,fn);
		}
		else {
			element["on"+evt] = fn;
		}
	},
	removeEvent: function(element,evt,fn) {
		if (element.removeEventListener) {
			element.removeEventListener(evt,fn,false);
		}
		else if (element.detachEvent) {
			element.detachEvent("on"+evt,fn);
		}
		else {
			element["on"+evt] = '';
		}
	},
	cancelEvent: function(e) {
		if(e.stopPropagation) {
			e.stopPropagation();
		}
		if(e.preventDefault) {
			e.preventDefault();
		}				
		e.cancelBubble = true;
		e.cancel = true;
		e.returnValue = false;
		return false;	
	},
	getVScrollDirection: function(data) {
		var ie = false;
		if (/MSIE/.test(navigator.userAgent)) ie = true;
		if ((ie && (data < 0)) || (!ie && (data > 0))) {
			return "down";
		}
		else if ((ie && (data > 0)) || (!ie && (data < 0))) {
			return "up";
		}
	},
	create: function(element,ih) {
		element = document.createElement(element);
		if (ih) { element.innerHTML = ih; }
		return element;
	},
	prependElement: function(element,container,ih) {
		if (typeof element == "string") {
			element = as.create(element,ih);
		}
		container.insertBefore(element,container.firstChild);
		return element;
	},
	appendElement: function(element,container,ih) {
		if (typeof element == "string") {
			element = as.create(element,ih);
		}
		container.appendChild(element);
		return element;
	},
	insertBefore: function(element,container,before,ih) {
		if (typeof element == "string") {
			element = as.create(element,ih);
		}
		container.insertBefore(element,before);
		return element;
	},
	removeChild: function(element) {
		element.parentNode.removeChild(element);
	},
	getParent: function(target,params) {
		//params: tagName or attribute
		while (target.parentNode) {
			target = target.parentNode;
			if (
				this.hasTagName(target,params.tn) && 
				this.hasAttribute(target,params.attr) &&
				this.hasClassName(target,params.cn)
			) { return target; }
		}
		return null;
	},
	hasTagName: function(target,tn) {
		if (!tn) { return true;}
		return target.tagName == tn.toUpperCase();
	},
	hasAttribute: function(target,attr) {
		if (!attr) { return true; }
		if (target[attr]) { return true }
		else { return false; }
	},
	hasClassName: function(target,cn) {
		if (!cn) { return true; }
		if (target.className.match(new RegExp("(^| )"+cn+"($| )"))) { return true; }
		else { return false; }
	},	
	ajax: function() {
		this.responseText='';
		this.loadStatus;
		this.getter=null;
		this.func=null; // func for backlink action (this function will be exec when XML data is loaded)
		
		this.makeRequest=function (url,func,getter) {
			this.getter = getter;
			this.http_request = false;
			this.func=func;
			if (window.XMLHttpRequest) { // Mozilla, Safari,...
				this.http_request = new XMLHttpRequest();
				if (this.http_request.overrideMimeType) {
					this.http_request.overrideMimeType('text/xml');
					// See note below about this line
				}
			} else if (window.ActiveXObject) { // IE
				try {
					this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
						this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {}
				}
			}
			if (!this.http_request) {
				throw new Error('Cannot create an XMLHTTP instance. You shutdown ActiveX ?\n Compatibility: IE 5.0, Mozilla 1.7, Firefox 1.0, Opera 8.0');
				return false;
			}
			this.http_request.onreadystatechange = this.alertContents;
			this.http_request.open('GET', url, true);
			this.http_request.send(null);
		}
		
		this.makeRequestPost=function (url,func,data) {
			this.http_request = false;
			this.func=func;
			if (window.XMLHttpRequest) { // Mozilla, Safari,...
				this.http_request = new XMLHttpRequest();
				if (this.http_request.overrideMimeType) {
					this.http_request.overrideMimeType('text/xml');
					// See note below about this line
				}
			} else if (window.ActiveXObject) { // IE
				try {
					this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
						this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {}
				}
			}
	
			if (!this.http_request) {
				alert('Cannot create an XMLHTTP instance. You shutdown ActiveX ?\n Compatibility: IE 5.0, Mozilla 1.7, Firefox 1.0, Opera 8.0');
				return false;
			}
			this.http_request.onreadystatechange = this.alertContents;
			this.http_request.open('POST', url, true);
			this.http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.http_request.send(data);
		}
	
		this.alertContents=function (e) {
			var ajax=this;//window['storage'].ajax;
			var http_request=this.http_request;
			this.send_status(http_request.readyState);
			if (http_request.readyState == 4) {
				if (http_request.status == 200) {
					this.responseText=http_request.responseText;
					if(http_request.responseXML.documentElement == null)
					{
						try
						{
							http_request.responseXML.loadXML(http_request.responseText)
						} catch (e) {}
					}
					this.func(http_request.responseText,http_request.responseXML);
				} else {
					this.loadStatus=http_request.status;
					this.func('',null);
				}
			}
		}.bind(this)
		
		this.send_status=function(status)
		{
			window.status=status;
		}
	},	
	ready: {
		fnList: [],		
		add: function() {
			for (var i=0;i<arguments.length;i++) {
				var fn = arguments[i];
				if (!this.fnList.inArray(fn)) {
					this.fnList[this.fnList.length] = fn;
				}

			}
		},		
		init: function() {
			/* for Mozilla */
			if (document.addEventListener) {
				document.addEventListener("DOMContentLoaded", this.initFn, false);
				as.ready.dcl = true;
			}
			
			/* for Internet Explorer */
			/*@cc_on @*/
			/*@if (@_win32)
				document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
				var script = document.getElementById("__ie_onload");
				script.onreadystatechange = function() {
					if (this.readyState == "complete") {
						as.ready.initFn(); // call the onload handler
					}
				};
			/*@end @*/			
			/* for Safari */
			if (/WebKit/i.test(navigator.userAgent)) { // sniff
				if (as.ready.dcl) { return; }
				var _timer = setInterval(function() {
					if (/loaded|complete/.test(document.readyState)) {
						this.initFn(); // call the onload handler
						clearInterval(_timer);
					}
				}, 10);
			}
			
			/* for other browsers */
			window.onload = this.initFn;
		},		
		initFn: function() {
			if (arguments.callee.done) return;
			arguments.callee.done = true;
			var self = as.ready;
			for (i=0;i<self.fnList.length;i++) {
				self.fnList[i]();
			}
			as.ready.dcl = "undefined";
		}
	},
	
	controller: {
		list: {},
		create: function(controllerName,className,tagName) {
			this.list[controllerName] = {
				init: function() {
					this.list = [];
					var _self_ = this;
					var elements = as.getBCN(className,tagName);
					as.map(
						elements,
						function(element) {
							_self_.list[_self_.list.length] = new window[controllerName](element);
							_self_.list[_self_.list.length-1].init();
						}
					);
				}
			}
			this.list[controllerName].init();
		}
	}
}

