/**
 *
 *
 * boot.js
 * @author nathena
 */
(function(_window)
{	
	Function.prototype._extends = function($superClass)
	{
		this.prototype = _ns.isFunction($superClass)?
				new ($superClass)():_ns.isObject($superClass)?$superClass:{};
		this.prototype.contruct = this;
		
		return this;
	}
	
	//版本号
	var Version = "1.2.1";
	
	//框架支持
	var W = _window,D=W.document,OpenCache = false;
	
	//缓存
	var b = {};
	
	//路径
	var P = "";
	
	//命名空间|boot核心引擎
	var Framework = W.ns||"A"; //App
	var _ns = window[Framework] = function(_el)
	{
		var el = _ns.get(_el);
		
		if( !el )
		{
			_el= _el.toLowerCase();

			if(!b[_el])
			{
				 b[_el]=D.createElement(_el);
			}
			
			el = b[_el].cloneNode(false);
		}
		
		return _extends( el );
	}
	
	_ns.get = function(el)
	{
		if( ( _ns.isElement(el) || _ns.isDocument(el) || _ns.isWin(el) ) )
		{
			return el;
		}
		
		if( _ns.isString(el) || _ns.isNumber(el))
		{
			 return D.getElementById(el+"");
		}
		
		return;
	}
	
	//扩展
	function _extends( el )
	{
		if( !el._extends )
		{
			_ns.Extends(el,new Element(),true);
		}
		return el;
	}
	
	function find(s,wrap)
	{
		var eleExpr =  /([a-zA-Z0-9]*)([#\.]?)(\w+)[^\s+]*/g;
		var result = [wrap||D];

		while(eleExpr.test(s)) 
		{
			var tag = RegExp.$1,
				type = RegExp.$2,
				content = RegExp.$3,
				elems = [];
			for(var i=0; i<result.length; i++) 
			{
				var temp;
				switch(type) 
				{
					case "#":
						temp = [result[i].getElementById(content)];
						break;
					case ".":
						temp = getElemsByClassName(content,result[i],tag);
						break;
					default:
						temp = result[i].getElementsByTagName(tag+content);
				}
				
				for(var key in temp )
				{
					if(temp[key].nodeType == 1) elems.push(temp[key]);
				}								
			}
			result = [];

			for(var key in elems)
			{
				if(elems[key].nodeType == 1) result.push(elems[key]);
			}	
		}
		
		var len = result.length;
		for(var i=0;i<len;i++)
		{
			_extends( result[i] );
		}

		return len == 1 ? result[0] : result;
		
		function getElemsByClassName(classname,elem,tag) 
		{
			var result = [],
				tag = tag || "*",
				allElems = elem.getElementsByTagName(tag) || elem.all;
			for(var i=0; i<allElems.length; i++)
			{
				var list = allElems[i].className.split(" ");
				for(var j=0; j<list.length; j++)
				{
					if(list[j] == classname) result.push(allElems[i]);
				}       
			}
			return result;
		}

	}
	_ns.find = find;

	//Browser	
	_ns.isStrictMode = D.compatMode!="BackCompat";
	_ns.IE = !!(W.attachEvent&&!W.opera);
	_ns.IE6 = navigator.userAgent.indexOf("MSIE 6")>-1;
	_ns.IE7 = navigator.userAgent.indexOf("MSIE 7")>-1;
	_ns.IE8 = navigator.userAgent.indexOf("MSIE 8")>-1;
	_ns.Opera = !!W.opera;
	_ns.WebKit = navigator.userAgent.indexOf("AppleWebKit/")>-1;
	_ns.Gecko = navigator.userAgent.indexOf("Gecko")>-1&&navigator.userAgent.indexOf("KHTML")==-1;

	//Extends
	_ns.Extends = function(_s,_p,_unoverride)
	{
		for(var i in _p)
		{
			if( _unoverride && _s[i] )
			{
				continue;
			}
			_s[i]=_p[i];
		}
	}
	
	//Type
	_ns.isString = function(_1)
	{
		return typeof _1 == "string";
	}
	_ns.isElement = function(_1)
	{
		return  _1&& _1.nodeType == 1;
	}
	_ns.isDocument = function(_l)
	{
		return  _l&& _l.nodeType == 9;
	}
	_ns.isWin = function(_l)
	{
		return  _l&& _l.navigator;
	}
	_ns.isUndefined = function(_1)
	{
		return typeof _1=="undefined";
	}
	_ns.isFunction = function(_1)
	{
		return typeof _1=="function";
	}
	_ns.isObject = function(_1)
	{
		return typeof _1=="object";
	}
	_ns.isArray = function(_1)
	{
		return Object.prototype.toString.call(_1)==="[object Array]";
	}
	_ns.isNumber = function(_1)
	{
		return typeof _1=="number";
	}

	var _prototype_ = {
		trim:function()
		{
			return this.ltrim().rtrim();
		},
		ltrim:function()
		{
			return this.replace(/(^[\uFEFF|\u00A0|\s|\u0000]*)/g,"");
		},
		rtrim:function()
		{
			return this.replace(/([\uFEFF|\u00A0|\s|\u0000]*$)/g,"");
		},
		encodehtml:function()
		{
			var html=this;
			html=html.replace( /</g,"&lt;");
			html=html.replace( />/g,"&gt;");
			html=html.replace(/["']/g,"&quot;");
			//去掉\0结束符
			html=html.replace(/\u0000/g,"");
			return html;
		},
		bytelength:function()
		{
			var smatch = this.match(/[^\x00-\x80]/g);
			return (this.length + (! smatch ? 0 : smatch.length));
		},
		getLength:function()
		{
			var charset = document.charset; 
			var len = 0;
			for(var i = 0; i < this.length; i++) 
			{
				len += this.charCodeAt(i) < 0 || this.charCodeAt(i) > 255 ? (charset == "utf-8" ? 3 : 2) : 1;
			}
			return len;
		},
		limit:function(len)
		{
			var num = 0;
			var strlen = 0;
			var newstr = "";
			var strArr = this.split("");
			for(var i = 0; i < strArr.length; i ++) 
			{
				var _len = strArr[i].bytelength();
				if(i < len && (num + _len) <= len) 
				{
					num += _len;
					strlen = i + 1;
				}
			}
			if(this.length > strlen) 
			{
				newstr = this.substr(0, strlen);
			} 
			else 
			{
				newstr = this;
			}
			return newstr;
		}
	}
	_ns.Extends(String.prototype,_prototype_);

	 _prototype_ = {

		have:function(v)
		 {
			if( this.indexOf(v)>-1 )
			{
				return true;
			}
			return false;
		 },
		 indexOf:function(v)
		 {
			 var len = this.length;
			 for(var i=0;i<len;i++)
			 {
				 if( this[i] == v )
					 return i;
			 }
			 return -1;
		 },
		 $delete:function(i)
		 {
			 this.splice(i,1);
			 return this;
		 },
		 $deleteV:function(v)
		 {
			 var index = this.indexOf(v);
			 if( index>-1 )
			 {
				 return this.$delete(index);
			 }
			 return this;
		 },
		 each:function(fn,_o )
		 {
			 var len = this.length;
			 for(var i=0;i<len;i++)
			 {
				 fn.call(_o,this[i]);
			 }
		 },
		 input:function( _a )
		 {
			 return this.have(_a);
		 },
		 put:function( _v )
		 {
			 if( this.indexOf(_v)>-1)
				 return;
			 this.push(_v);
		 }		
	}
	_ns.Extends(Array.prototype,_prototype_);

	//Element
	function Element()
	{
		this._extends = true;
		this.getNode = function(_i)
		{
			var nodes=[];
			var _nodes = this.childNodes,len=_nodes.length;
			for(var i=0;i<len;i++)
			{
				if( _nodes[i].nodeType == 1 )
				{
					nodes.push(_nodes[i]);
				}
			}
			return _ns.isNumber(_i)?nodes[Math.min(_i,nodes.length-1)]:nodes;
		}
		this.setStyle = function( c )
		{
			this.style.cssText = c;
			return this;
		}
		this.addStyle = function(c)
		{
			this.style.cssText+=";"+c;
			return this;
		}
		this.removeStyle = function( c )
		{
			this.style.cssText = this.style.cssText.replace(new RegExp("\\b"+c+"\\s*:\\s*[\\d\\w]+?(?:;|$)","g"),"");
			return this;
		}
		this.getStyle = function(_es)
		{
			_es=_es=="float"?"cssFloat":_es;
			var _eb=this.style[_es];
			if(!_eb)
			{
				if( this.currentStyle )
				{
					var css = this.currentStyle;
				}
				else
				{
					var css=document.defaultView.getComputedStyle(this,null);
				}
				_eb=css?css[_es]:null;
			}
			if(_es=="opacity")
			{
				return _eb?parseFloat(_eb):1;
			}
			return _eb=="auto"?null:_eb;
		}
		this.hasClass = function(c)
		{
			return new RegExp("(^|\\s+)"+c+"(\\s+|$)").test(this.className);
		}
		this.setClass = function( c )
		{
			if( !this.hasClass(c))
			{
				this.className = c;
			}
			return this;
		}
		this.addClass = function( c )
		{
			if( !this.hasClass(c))
			{
				this.className+=" "+c;
			}
			return this;
		}
		this.removeClass = function( c )
		{
			this.className=this.className.replace(new RegExp("(^|\\s+)"+c+"(\\s+|$)","g"),"");
			return this;
		}
		this.removeAll = function()
		{
			if( _ns.isElement(this) )
			{
				this.innerHTML = "";
			}
		}
		this.hide = function( type )
		{
			type= type||"";
			
			if( "v" == type)
			{
				this.style.visibility = "hidden";
			}
			else
			{
				this.style.display = "none";
			}
		}
		this.show = function( type )
		{
			type= type||"";
			
			if( "v" == type)
			{
				this.style.visibility = "";
			}
			else
			{
				this.style.display = type || "";
			}
		}
		this.render = function(container)
		{
			if( container)
			{
				container.appendChild(this);
			}
			else
			{
				D.body.appendChild(this);
			}
			return this;
		}
		this.remove = function()
		{
			var p = this.parentNode;
			if( _ns.isElement(p) )
			{
				p.removeChild(this);
			}
		}
		this.position = function()
		{
			if( this.getBoundingClientRect )
			{
				var _postion = this.getBoundingClientRect();
				return [_postion.left,_postion.top,_postion.right,_postion.bottom];
			}
			var o = this;
			var l=o.offsetLeft,t=o.offsetTop;
			while(o=o.offsetParent)
			{
				l+=o.offsetLeft;
				t+=o.offsetTop;
			}
			return [l,t,l+this.offsetWidth,t+this.offsetHeight];
		}
		this.setOpacity = function(n)
		{
			this.style.cssText = "filter:alpha(opacity="+ n*100 +"); -moz-opacity:"+ n +"; opacity:"+ n;
		}
		this.on = function(a,b,c)
		{
			return _ns.Event.addEvent(this,a,b,c);
		}
		this.un = function(a,b,c)
		{
			return _ns.Event.delEvent(this,a,b,c);
		}
		
		this.load = function(url)
		{
			var catainer = this;
			_ns.doGet(url,function(xhr)
			{
				catainer.innerHTML = xhr.responseText;
			});
		}
		this.previousNode = function()
		{
			var node = this;
			do
			{
				node = node.previousSibling;
			}
			while(node&&node.nodeType!=1)
			
			return node;
		}
		this.nextNode = function()
		{
			var node = this;
			do
			{
				node = node.nextSibling;
			}
			while(node&&node.nodeType!=1)
			
			return node;
		}
	}//~end Element	
	
	//cookie
	_ns.getCookie = function(_name)
	{
		var __name=_name+"=";
		var ca=document.cookie.split(";");
		for(var i=0;i<ca.length;i++)
		{
			var c=ca[i];
			while(c.charAt(0)==" ")
			{
				c=c.substring(1,c.length);
			}
			if(c.indexOf(__name)==0)
			{
				return decodeURIComponent(c.substring(__name.length,c.length));
			}
		}
		return null;
	}
	_ns.setCookie = function(_name,_value,_expires,_path,_domain,_secure)
	{
		var expires;
		 if( _ns.isNumber(_expires) )
		 {
			 var time=new Date();
			 time.setTime(time.getTime()+(_expires*24*60*60*1000));
			 expires=time.toGMTString();
		 }
		 else
		 {
			 if(_ns.isString(_expires))
			 {
				 expires=_expires;
			 }
			 else
			 {
				 expires=false;
			 }
		 }
		 document.cookie=_name+"="+encodeURIComponent(_value)+(expires?";expires="+expires:"")+(_path?";path="+_path:"")+(_domain?";domain="+_domain:"")+(_secure?";secure":"");
	}
	_ns.removeCookie = function(_5d,_5e,_5f,_60)
	{
		this.setCookie(_5d,"",-1,_5e,_5f,_60);
	}

	//to Extends
	var _nsExtends = {
		win:function( url,name,_w,_h )
		{
			var DescriptiveWindowName = name?'App':name;
			var h = _h||W.screen.availHeight/2,w = _w||W.screen.availWidth/2;
			var l = (W.screen.availWidth - w)/2,t = (W.screen.availHeight - h)/2;
			var strWindowFeatures = "resizable=yes,scrollbars=yes,status=yes," +
					"fullscreen=yes,menubar=no,toolbar=no,location=0,directories=no," +
					"personalbar=no,dependent=no,dialog=no,minimizable=yes,modal=yes" +
					",width="+w+
					",height="+h+
					",left="+l+
					",top="+t;
			$reference = W.open( url,DescriptiveWindowName,strWindowFeatures);
			if( $reference )
			{
				$reference.focus();
			}
		},		
		pageHeight:function()
		{
			return this.isStrictMode?Math.max(D.documentElement.scrollHeight,D.documentElement.clientHeight):Math.max(D.body.scrollHeight,D.body.clientHeight);
		},
		pageWidth:function()
		{
			return this.isStrictMode?Math.max(D.documentElement.scrollWidth,D.documentElement.clientWidth):Math.max(D.body.scrollWidth,D.body.clientWidth);
		},
		winWidth:function()
		{
			return this.isStrictMode?D.documentElement.clientWidth:D.body.clientWidth;
		},
		winHeight:function()
		{
			return this.isStrictMode?D.documentElement.clientHeight:D.body.clientHeight;
		},
		scrollTop:function()
		{
			if(_ns.WebKit)
			{
			 return W.pageYOffset;
			}
			return _ns.isStrictMode?D.documentElement.scrollTop:D.body.scrollTop;
		},
		scrollLeft:function()
		{
			if(_ns.WebKit)
			{
				return W.pageXOffset;
			}
			return _ns.isStrictMode?D.documentElement.scrollLeft:D.body.scrollLeft;
		},
		shadowRef:0,
		popuplevel:[],
		createShadow:function(popup,degree)
		{
			if( !this.shadow )
			{
				degree=degree||0.1;
				var el=_ns("div");
				this.shadow=el;
				el.setStyle(["position:absolute;","top:0;","left:0;","background:#000;","z-index:99;","opacity:"+degree+";","filter:alpha(opacity="+(degree*100)+");"].join(""));
				el.innerHTML=["<iframe width=\"100%\" height=\"100%\" frameBorder=\"0\" style=\"position:absolute;top:0;left:0;z-index:1;\"></iframe>","<div style=\"position:absolute;top:0;left:0;width:100%;height:100%;background-color:#000000;z-index:2;height:expression(this.parentNode.offsetHeight);\"></div>"].join("");
				function resize()
				{
					el.hide();
					el.style.height=_ns.pageHeight()+"px";
					el.style.width=_ns.pageWidth()+"px";
					el.show();
				}
				resize();
				_ns.Event.addEvent(W,"resize",function(e)
				{
					if(el&&el.style.display!="none")
					{
						resize();
					}
				});
				el.render();
			}
			this.shadow.show();
			if( this.shadowRef>0 )
			{			
				this.popuplevel[this.shadowRef-1].style.zIndex='90';
			}
			
			if( popup )
			{
				if( this.popuplevel.indexOf(popup)< 0 )
				{
					this.popuplevel[this.shadowRef]=popup;
					this.shadowRef++;
				}
			}
			return this.shadow;
		},
		destoryShadow:function()
		{
			--this.shadowRef;
			if( 0 >= this.shadowRef && this.shadow )
			{
				this.shadow.hide();
				this.shadowRef = 0;
				this.popuplevel = [];
			}
			else if( this.popuplevel[this.shadowRef-1] )
			{
				this.popuplevel[this.shadowRef-1].style.zIndex='100';
			}
		}
	};
	_ns.Extends(_ns,_nsExtends,true);
	
	//Ajax
	function Result()
	{
		this.uninitialized = function(){}
		this.loading = function(){}
		this.loaded = function(){}
		this.interactive = function(){}
		this.completed = function(){}
	}
	_ns.Result = Result;

	function Ajax()
	{
		var classid = ['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'];
		var $this = this;
		
		var headerNames = [];
		var headerValues = [];
		var event = {};
		var xhr;
		
		var overrideMimeType = false;
		
		this.resultset = new Result();
		
		this.autoDestory = false;
		this.dataCache = false;
		this.dataType = "default";
		this.sync = true;
		this.timeout = null;
		
		this.addEvent = function(_eventname,_event)
		{
			event[_eventname] = _event;
		}
		
		this.requestData = function(_data)
		{
			if( !_data ) return;
			if( _ns.isObject(_data) )
			{
				var data = [];
				for(var i in _data)
				{
					data.push(i);
					data.push("=");
					data.push(encodeURIComponent(_data[i]));
	                data.push("&");
				}
				
				return data.join("");
			}
			else
			{
				return _data;
			}
		}
		
		this.setHeader = function(name,value)
		{
			headerNames.push(name);
			headerValues.push(value);
		}
		
		this.getHeader = function()
		{
			return [headerNames,headerValues];
		}
		
		this.start = function()
		{
			if( xhr ) return;
			
			for(var i=0;i<classid.length;i++)
			{
				try
				{
					xhr = new ActiveXObject(classid[i]);
					return;
				}catch(ex){}
			}
			 
			xhr = new XMLHttpRequest();
			if( xhr.overrideMimeType ) 
			{
				overrideMimeType = true;
				if( 'XML' == this.dataType )
				{
					xhr.overrideMimeType('text/xml');
				}
			}
		}
		
		this.doGet = function(url)
		{
			this.open("GET", url, this.sync);
			this.sendHeaders();
		}
		
		this.doPost = function(url,cache)
		{
			this.open("POST", url, this.sync);
			this.sendHeaders();
			this.sendHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		}
		
		this.send = function(data)
		{
			_ns.Event.call(event['send']);
			
			if( this.timeout )
			{
				setTimtout(function(){$this.abort();},this.timeout);
			}
			
			if( this.sync )
			{
				var _this = this;
				xhr.onreadystatechange = function(data)
				{
					_this.response();
				}
			}
			
			xhr.send(this.requestData(data));
			
			if( this.sync === false )
			{
				this.resultset.completed(xhr);
			}
			
			
		}
		
		this.abort = function()
		{
			if( xhr )
			{
				try{xhr.abort();}catch(e){}
				try{xhr.onreadystatechange = function(){}}catch(e){} //IE
			}
		}
		
		this.destory = function()
		{
			xhr = null;
		}
		
		this.open = function(_method,_url,_sync)
		{
			_ns.Event.call(event['open']);
			if( _url.indexOf("?")> -1 )
			{
				_url+="&inajax="+new Date().getTime();
			}
			else
			{
				_url+="?inajax="+new Date().getTime();
			}
			xhr.open(_method,_url,_sync);
		}
		
		this.sendHeaders = function()
		{
			this.sendHeader("X-Requested-With", "XMLHttpRequest");
			
			for(var i=0;i<headerNames.length;i++ )
			{
				this.sendHeader(headerNames[i],headerValues[i]);
			}
				
			if( !this.dataCache )
			{
				this.sendHeader("If-Modified-Since","0");
			}
		}
		
		this.sendHeader = function(_name,_value)
		{
			xhr.setRequestHeader(_name,_value);
		}
		
		this.response = function()
		{
			if( xhr.readyState == 0 )
			{
				_ns.Event.call(event['uninitialized']);
				this.resultset.uninitialized(xhr);
			}
			else if( xhr.readyState == 1 )
			{
				_ns.Event.call(event['loading']);
				this.resultset.loading(xhr);
			}
			else if( xhr.readyState == 2 )
			{
				_ns.Event.call(event['loaded']);
				this.resultset.loaded(xhr);
			}
			else if( xhr.readyState == 3 )
			{
				_ns.Event.call(event['interactive']);
				this.resultset.interactive(xhr);
			}
			else if( xhr.readyState == 4 )
			{
				if( xhr.status == 301 )
				{
					W.location.href=xhr.getResponseHeader("X-"+framework+"-Location");
				}
				else if( xhr.status == 500 )
				{
					alert("服务器错误，请求失败");
				}
				else
				{
					_ns.Event.call(event['completed']);
					this.resultset.completed(xhr);
					if( this.autoDestory )this.destory();
				}
			}
		}
	}
	_ns.Ajax = Ajax;
	_ns.xhr = function(call,options)
	{
		var xhr = new Ajax();
		if( this.isFunction(call))
		{
			xhr.resultset.completed = call;
		}
		else
		{
			this.Extends(xhr.resultset,call||{});
		}
		
		if( this.isObject(options)) 
		{
			if( options.type )
			{
				xhr.dataType = options.type;
			}
			
			if( options.sync )
			{
				xhr.sync = options.sync;
			}
			
			if( options.cache )
			{
				xhr.dataCache = options.cache;
			}
			
			if( options.Header )
			{
				for(var i in options.Header)
					xhr.setHeader(i,options.Header[i]);
			}
		}
		
		return xhr;
	}
	
	_ns.doGet = function(url,call,options)
	{
		call = call || {}
		options = options || {}
		
		var xhr = this.xhr(call,options);
		xhr.autoDestory = true;
		xhr.start();
		xhr.doGet(url);
		xhr.send();
		
	}
	
	_ns.doPost = function(url,data,call,options)
	{
		call = call || {}
		options = options || {}
		
		var xhr = this.xhr(call,options);
		xhr.autoDestory = true;
		xhr.start();
		xhr.doPost(url);
		xhr.send(data);
	}

	_ns.doForm = function(form,call)
	{
		var name = "iframe"+new Date().getTime();
		var container = _ns("div");
		container.style.cssText = "width:0px;height:0px;border:0px;position:absolute;top:-10px;left:-10px;";
		container.innerHTML = '<iframe style="width:0px;height:0px;border:0px;border:0px;" id="'+name+'" name="'+name+'"></iframe>';
		container.render();
		
		var iframe = _ns(name);
		
		call = _ns.isFunction(call)?call:function(){}
		
		iframe.on('load',function(){
			
			iframe.un('load',arguments.callee);
			try
			{
				call(iframe.contentWindow.document.body.innerHTML);
			}catch(ex){call();}
			iframe.src="about:bank";
			iframe.remove();
			container.remove();	
			
		 });
		
		form.target = name;		
		form.submit();
	}

	var ajax = new Ajax(); 
	_ns.getSource = function( uri )
	{
		var load = ajax;
		var source;
		load.resultset.completed = function(xhr){source = xhr.responseText}
		load.sync = false;
		load.start();
		load.doGet(uri);
		load.send();
		
		return source;
	}

	//Event
	_ns.Event = {
		get:function()
		{
			if(W.event)
			{
		        return W.event;
		    } 
		    return _ns.Event.get.caller.arguments[0];
		},
		init:function(e)
		{
			return e||W.event;
		},
		isCapsLockOn:function(e)
		{
			e = this.init(e);
			var c=e.keyCode||e.which;
			var s=e.shiftKey;
			if(((c>=65&&c<=90)&&!s)||((c>=97&&c<=122)&&s))
			{
				return true;
			}
			return false;
		},
		keyCode:function(e)
		{
			e = this.init(e);
			return e.keyCode||e.which;
		},
		element:function(e)
		{
			e = this.init(e);
			var n=e.target||e.srcElement;
			return _ns(this.resolveTextNode(n));
		},
		relatedTarget:function(e)
		{
			e = this.init(e);
			var t=e.relatedTarget;
			if(!t)
			{
				if(e.type=="mouseout"||e.type=="mouseleave")
				{
					t=e.toElement;
				}
				else
				{
					if(e.type=="mouseover")
					{
						t=e.fromElement;
					}
				}
			}
			return this.resolveTextNode(t);
		},
		pointerX:function(a)
		{
			a = this.init(a);
			return a.pageX||(a.clientX+(D.documentElement.scrollLeft||D.body.scrollLeft));
		},
		pointerY:function(b)
		{
			b = this.init(b);
			return b.pageY||(b.clientY+(D.documentElement.scrollTop||D.body.scrollTop));
		},
		clientX:function(a)
		{
			a = this.init(a);
			return a.clientX;
		},
		clientY:function(a)
		{
			a = this.init(a);
			return a.clientY;
		},
		offset:function(e)
		{
			e = this.init(e);
			if( e.offsetX && e.offsetY)return [e.offsetX,e.offsetY];
	 
			var o = this.element(e);
			var postion = o.position();
	 
			return [e.pageX-position[0],e.pageY-position[1]];
		},
		offsetPosition:function(e)
		{
			 e = this.init(e);
			 var o = this.element(e);
			 var x = o.offsetWidth/2;
			 var y = o.offsetHeight/2;
			 
			 if( e.offsetX && e.offsetY)
			 {
				 return [(x>=e.offsetX?1:0),(y>=e.offsetY?1:0)];
			 }
			 
			 var position = o.position();
			 return [(x>=e.pageX-position[0]?1:0),(y>=e.pageY-position[1]?1:0)];
		 },
		 stop:function(e)
		 {
			 e = this.init(e);
			 if(_ns.IE )
			 {
				 e.returnValue=false;
				 e.cancelBubble=true;
			 }
			 else
			 {
				 e.preventDefault();
				 e.stopPropagation();
			 }
		 },
		 start:function(e)
		 {
			 e = this.init(e);
			 if(_ns.IE )
			 {
				 e.returnValue=true;
				 e.cancelBubble=false;
			 }
		 },
		 addEvent:function(a,b,c,d)
		 {
			 c = _ns.isFunction(c)?c:function(){};
			 a=_ns(a);
			 if( W.attachEvent&&!_ns.Opera)
			 {
				 if(b=="keypress")
				 {
					b="keydown";
				 }
				 if(b=="input")
				 {
					b="propertychange";
				 }
				 a.attachEvent("on"+b,c);
				 a.onlosecapture=function(){a.releaseCapture()};
				 return a;
			 }
			 else if( W.addEventListener )
			 {
				 if(b=="keypress"&&_ns.WebKit)
				 {
					b="keydown";
				 }
				 a.addEventListener(b,c,d);
				 return a;
			 }
			 else
			 {
				 a['on'+b] = c;
			 }
		 },
		 delEvent:function(a,b,c,d)
		 {
			 c = _ns.isFunction(c)?c:function(){};
			 a=_ns(a);
			 if( W.attachEvent&&!_ns.Opera)
			 {
				 if(b=="keypress")
				 {
					b="keydown";
				 }
				 if(b=="input")
				 {
					b="propertychange";
				 }
				 a.detachEvent("on"+b,c);
				 return a;
			 }
			 else if( W.addEventListener )
			 {
				 if(b=="keypress"&&_ns.WebKit)
				 {
					b="keydown";
				 }
				 a.removeEventListener(b,c,d);
				 return a;
			 }
			 else
			 {
				 a['on'+b]=null;
			 }
		 },
		 resolveTextNode:function(n)
		 {
			try
			{
				if(n&&n==this.nodeType)
				{
					return n.parentNode;
				}
			}
			catch(e){}
			return n;
		 },
		 call:function(_fn)
		 {
			 if( _ns.isFunction(_fn) )
			 {
				 return _fn.call();
			 }
			 return true;
		 }
	};
	
	_ns.DD = function()
	{
		var element;
		var x,y,pageX,pageY,maxX,maxY;
		var event = {};
		var $this = this;
		this.addEvent = function(_event,_fn)
		{
			event[_event] = function(){fn.call(element)};
		}
		
		this.register = function(_element)
		{
			_ns.Event.call(event['beforRegister']);
			
			var e = arguments.length == 2?arguments[1]:W.event;
			
			element = _ns(_element);
			x = _ns.Event.pointerX(e),y = _ns.Event.pointerY(e);
			pageX = _ns.pageWidth(),pageY = _ns.pageHeight();
			maxX = pageX - element.offsetWidth;
			maxY = pageY - element.offsetHeight;
			
			if( element.getStyle("position").toLowerCase() != "absolute" )
			{
				element.addStyle("position:absolute");
			}
			
			_ns.Event.addEvent(D,'mousemove',drag);
			_ns.Event.addEvent(D,'mouseup',dragEnd);
			
			_ns.Event.call(event['afterRegister']);
		}
		
		function pointerPosition(event)
		{
			var px = _ns.Event.pointerX(event),py = _ns.Event.pointerY(event);
			var _x = px - x,_y = py - y;
			
			x = px,y = py;
			return [_x,_y];
		}
		
		function currentPosition()
		{
			var _x = element.getStyle('left'),_x = _x?parseInt(_x,10):0;
			var _y = element.getStyle('top'),_y = _y?parseInt(_y,10):0;
			
			return [_x,_y];
		}
		
		function toPositon(_x,_y)
		{
			element.style.top=_y+"px";
			element.style.left=_x+"px";
		}
		
		function drag(event)
		{
			_ns.Event.call(event['beforDrag']);
			
			var xy = pointerPosition(event);
			var exy = currentPosition();
			var _x = exy[0]+xy[0],_y=exy[1]+xy[1];
			_x = _x<0?0:_x,_x = _x>maxX?maxX:_x;
			_y = _y<0?0:_y,_y = _y>maxY?maxY:_y;
			
			toPositon(_x,_y);
			
			_ns.Event.call(event['afterDrag']);
		}
		
		function dragEnd(event)
		{
			_ns.Event.call(event['beforeDragEnd']);
			
			_ns.Event.delEvent(D,'mousemove',drag);
			_ns.Event.delEvent(D,'mouseup',dragEnd);
			
			x = 0;
			y = 0;
		}
	}
	
	_ns.Move = new _ns.DD();

	//Popup
	_ns.Popup = function()
	{
		var html = "";
		var popup;
		
		this.setContent = function(_html)
		{
			html = _html;
		}
		
		this.setHtml = function(_html)
		{
			html = _html;
			popup.innerHTML = html;
		}
		
		this.build = function()
		{
			if( popup ) return popup;
			
			popup = _ns('div');
			popup.setStyle('position:absolute;z-index:100;');
			
			return popup;
		}
		
		this.render = function(_position,container)
		{
			_ns.createShadow(popup);
			
			popup.innerHTML = html;
			popup.render();
			
			popup.hide("v");
			_ns.PositionLayout.call(popup,_position);
			popup.show("v");
		}
		
		this.remove = function()
		{
			if(jQuery)
			{
				//$(popup).animate({top:'+=50',opacity:0.1},function(){popup.remove()});
				$(popup).animate({top:'-=50'},function(){popup.remove()});
			}
			else
			{
				popup.remove()
			}
			
			_ns.destoryShadow();
		}
		  
		this.getEntity = function()
		{
			return popup;
		}
		this.show = function()
		{
			if(jQuery)
			{
				//$(popup).animate({top:'+=50',opacity:0.1},function(){popup.show("v");});
				$(popup).animate({top:'+=50'},function(){popup.show("v");});
			}
			else
			{
				popup.show("v");
			}
			
			_ns.createShadow(popup);
		}
		this.hide = function()
		{
			if(jQuery)
			{
				//$(popup).animate({top:'+=50',opacity:0.1},function(){popup.hide("v");});
				$(popup).animate({top:'-=50'},function(){popup.hide("v");});
			}
			else
			{
				popup.hide("v");
			}
			
			_ns.destoryShadow();
		}

	}; //~Popup END
	
	//Layout
	_ns.PositionLayout = function( _position )
	{
		if( !_ns.isElement(this) ) return;
		
		_position = _position || "center";
		
		if(jQuery)
		{
			//$(this).css("opacity",0.1);
		}
		
		if( "lt" == _position )
		{
			this.addStyle("top:0px;left:0px;");
		}
		else if( "rt" == _position )
		{
			this.addStyle("top:0px;right:0px;");
		}
		else if( "lb" == _position )
		{
			this.addStyle("bottom:0px;left:0px;");
		}
		else if( "rb" == _position )
		{
			this.addStyle("bottom:0px;right:0px;");
		}
		else if( "tc" == _position )
		{
			 var w = this.offsetWidth;
			 var _w = _ns.winWidth();
			 var left = Math.ceil((_w-w)/2);
			 var scroll = _ns.scrollTop();
			 var top = parseInt(120+scroll-50,10);
			 this.addStyle('left:'+left+'px;top:'+top+'px;');
		}
		else if( "center" == _position )
		{
			 var w = this.offsetWidth;
			 var h = this.offsetHeight;
			  
			 var _w = _ns.winWidth(),_h = _ns.winHeight();
			 
			 var left = Math.ceil((_w-w)/2),top = Math.ceil((_h-h)/2);
			 top = top>0?top:0;
			 var scroll = _ns.scrollTop();
			 var top = parseInt(top+scroll-50,10);
			 this.addStyle('left:'+left+'px;top:'+top+'px;');
		}
		else
		{
			this.addStyle(_position);
		}
		
		if(jQuery)
		{
			//$(this).animate({top:'+=50',opacity:1});
			$(this).animate({top:'+=50'});
		}
		
		return this;
	}	
	
	//store
	function Store(_domain)
	{
		var d;
		var strorage;
		var domain = _domain || D.domain;
		
		//static
		try
		{
			if( !d )
			{
				if( W.globalStorage )//globalStorage
				{
					d = W.globalStorage;
					strorage = d[domain];
				}
				else
				{
					d = D.documentElement;
					d.style.behavior = "url('#default#userData')";
				}
			}
			else if( strorage )
			{
				strorage = d[domain];
			}
			else
			{
				d.load(domain);
			}
			
		}catch(_){}
		
		this.set = function(key,value)
		{
			try
			{
				if( strorage )
				{
					strorage.setItem(key,value);
				}
				else
				{
					d.setAttribute(key,value);
					d.save(domain);
				}
			}catch(_){}
		}
		
		this.get = function(key)
		{
			try
			{
				var value;
				if( strorage )
				{
					value = strorage.getItem(key);
				}
				else
				{
					value = d.getAttribute(key);
				}
				return value;
			}catch(_){}
		}
		
		this.remove = function(key)
		{
			try
			{
				if( strorage )
				{
					strorage.removeItem(key);
				}
				else
				{
					d.removeAttribute(key);
					d.save(domain);
				}
			}catch(_){}
		}
	}
	_ns.Store = Store;
	
	_ns.execScript = function(data)
	{
		if ( data && /\S/.test(data) )
		{
			if( W.execScript )
			{
				W.execScript(data);
			}
			else
			{
				var head = D.getElementsByTagName("head")[0] || D.documentElement;
				var script = _ns("script");
				script.type = "text/javascript";				
				script.text = data;				
				//script.appendChild( document.createTextNode( data ) );
				head.insertBefore( script, head.firstChild );
				head.removeChild( script );
			}
		}
	}	
	
	var Include = function()
	{
		var head = D.getElementsByTagName("head")[0] || D.documentElement;
		var rp = /^.+\.(\w+)$/;
		this.load = function()
		{
			for(var i=0;i<arguments.length;i++ )
			{
				if( rp.test(arguments[i]))
				{
					switch(RegExp.$1.toLowerCase())
					{
						case "js":
							loadJS(arguments[i]);
						break;
						case "css":
							loadCSS(arguments[i]);
						break;
					}
				}
			}
		}
		
		function loadJS(filename)
		{
			var script = _ns("script");
			script.type = "text/javascript";				
			script.src = P+filename;
			listenState(script)
			
			head.insertBefore( script, head.firstChild );
		}
		
		function loadCSS(filename)
		{
			var css = _ns("link");
			css.type = "text/css";				
			css.href = P+filename;
			css.rel="stylesheet";
			listenState(css)
			
			head.insertBefore( css, head.firstChild );
		}
		
		function listenState(element)
		{
//			element.onload = function()
//			{alert(head);
//				head.removeChild( element );
//			}
//			element.onreadystatechange = function()
//			{
//				if( "complete" == element.readyState || "loaded" == element.readyState)
//					head.removeChild( element );
//			}
		}
	}
	
	var include = new Include();
	_ns.Include = function(){include.load.apply(null,arguments);return this;};
	
	(function()
	{
		var scripts = D.getElementsByTagName("script"),src;
		for( var i=0;i<scripts.length;i++ )
		{
			var temp  = scripts[i],src = temp.src.toLowerCase();
			if( (index = src.indexOf("boot.js"))>-1 )
			{
				P = src.substring(0,index);
				break;
			}
		}
		_ns.path = P;
	})();
	
	_ns.__onReady__ = false;
	_ns.onReady = function(func)
	{	
		func = this.isFunction(func)?func:function(){}
		var loaded = false;
		if(  _ns.__onReady__ )
		{  
			func();
			return;
		}
		
		function onReady()
		{
			if( !loaded )func();
			loaded = true;
			_ns.__onReady__ = true;
		}
		
		if ( D.addEventListener ) 
		{		
			D.addEventListener( "DOMContentLoaded", function()
			{				
				D.removeEventListener( "DOMContentLoaded", arguments.callee, false );
				onReady();
			}, false );
			
			W.addEventListener( "load" , function()
			{				
				W.removeEventListener("load", arguments.callee, false);
				onReady();
			}, false );
		}
		else if ( document.attachEvent )
		{
			D.attachEvent("onreadystatechange", function()
			{
				if( D.readyState === "complete" ) 
				{						
					D.detachEvent( "onreadystatechange", arguments.callee );
					onReady();
				}
			});
			W.attachEvent( "onload" , function()
			{					
				W.detachEvent("onload", arguments.callee);
				onReady();
			});
		}
		else
		{
			W.onload = function()
			{
				onReady();
			}
		}
	}
	if( _ns.IE6 )
	{
		D.execCommand("BackgroundImageCache", false, true);
	}
	_ns.onReady(function()
			{
//				var scripts = D.getElementsByTagName("script"),src,index,param;
//				while( scripts.length>0 )
//				{
//					_ns(scripts[0]).remove();
//				}
			});
})(window);

