﻿
var yun={ver:'1.1.5',idseq:100,id:'yun',htData:[]};
yun.getGuid=function(){return this.id+this.idseq++}
yun.stop=function(event){
	if(event.preventDefault){
		event.preventDefault();
		event.stopPropagation()}
	else{
		event.returnValue=false;
		event.cancelBubble=true}
}
yun.reg=function(key,obj){this.htData[key]=obj}
yun.regJson=function(id,callBack,opt){this.reg(id,{cb:callBack,op:opt})}
yun.dojson=function(id,Jo){
	var jb=this.htData[id];
	jb.cb(Jo,jb.op);
	jb.cb=jb.opt=jb=this.htData[id]=null;
}
// 需要对＋号进行 urlencode
yun.Ajax=function(){
	this.IEXMLHttpVer=["MSXML2.XMLHTTP.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
	this.createXMLHttp=function(){var xmlHttp=null;
	if(window.XMLHttpRequest){return new XMLHttpRequest()}
	if(window.ActiveXObject){
		for(var i=0;i<this.IEXMLHttpVer.length;i++){
			try{xmlHttp=new ActiveXObject(this.IEXMLHttpVer[i]);break}catch(e){}
			}
		}
	return xmlHttp;
	}
	this.GET=function(ao){
	var xh=this.createXMLHttp();
	if(!xh){return 0}
	xh.open("GET", ao.url, true);
	ao.xh=xh;
	xh.onreadystatechange=this.callback.bind(ao);
	xh.send(null)}
	this.POST=function(ao,content){
	var xh=this.createXMLHttp();
	if(!xh){return 0}
	xh.open("POST", ao.url, true);
	ao.xh=xh;
	xh.onreadystatechange=this.callback.bind(ao);
	xh.send(content)}
	this.callback=function(){
		if(this.xh.readyState==4){
			if(this.xh.status==200){
				this.doCall(this.xh)
				}
			else if(this.opts!=undefined&&typeof(this.opts['onerror'])=='function'){
				this.opts['onerror'](this.xh);
				}
		}
	}
	this.ajaxObj=function(url,callBack,options,f){
	this.url=url;this.callBack=callBack;this.f=f;this.opts=options;
	this.doCall=function(xh){
		try{this.callBack(xh,this.opts)}catch(e){if(this.f){throw e}}
		this.callBack=null;this.url=null;this.xh=null}
	}
	this.sendGet=function(url,callBack,options){this.GET(new this.ajaxObj(url,callBack,options,this.debug?true:null))}
	this.sendPost=function(url,content,callBack,options){this.POST(new this.ajaxObj(url,callBack,options,this.debug?true:null),content)}
	this.init=function(){}
	this.debug=0;
	this.sendJs=function(url,callback,options){
		head=document.getElementsByTagName("head")[0];
		var sce=$CE('script');
		sce.id=yun.getGuid();
		yun.regJson(sce.id,callback,options);
		sce.type='text/javascript';
		if(url.indexOf('{0}')>0){url=url.format('yun.dojson',sce.id)}
		else{var qs='&cb=yun.dojson&jsonId='+sce.id;url=url.test(/\?/)?url+qs:url+'?'+qs}
		if(url.test(/[\u4E00-\u9FA5\uF900-\uFA2D]/ig))url=encodeURI(url);
		sce.src=url;
		head.appendChild(sce)
	}
}

yun.Position=function(){
	this.withIn=function(el,x,y){
	var a=this.getAbsRect(el);
	x=x||this.getAbsX()||0;
	y=y||this.getAbsY()||0;
	if(x<a[0]||x>a[1]-2){return 0}if(y<a[2]||y>a[3]-2){return 0}
	return 1}
	this.getAbsRect=function(el){
	oriObj=el=$(el);
	var y1=x1=isHide=0;
	if(oriObj.style.display=='none'){oriObj.style.display='';isHide=1}
		do{
		y1+=el.offsetTop||0;
		x1+=el.offsetLeft||0;
		el=el.offsetParent}
	while(el);
	a=[x1,(x1+oriObj.offsetWidth),y1,(y1+oriObj.offsetHeight)];
	if (isHide){oriObj.style.display='none'}
	return a}
}


yun.Cookie=function(){
	this.keys=[];
	this.extKeys=[];
	this.bak=this.bak1=[];
	this.allDomain=0;
	this.allPath=1;
	this.code=1;//0:unescape  ||  1: decodeURI
	this.expiredSec=1800;//default expired minutes
	this.defaultET=function(){return (new Date()).addDate(0,0,0,0,0,this.expiredSec).toGMTString()}
	this.init=function(){
		this.load();
		var d=a=p=[];
	try{d=location.host.split('.');
	p=location.pathname.split('/');
	for(var n=0;n<d.length;n++){a[n]=d.join('.');d.splice(n,1)}
	d=a;a=[];
	p.splice(p.length-1,1);
	for(var n=p.length-1;n>=0;n--){
		a[n]=p.join('\/');
		p.splice(n,1);
	}
	a.splice(0,1,'/');p=a;
	}catch(e){}
	this.domainArr=d;
	this.rootDomain=d[d.length-1]
	this.pathArr=p;
	}
	this.del=function(key,d,p){//domain,path
		this.keys[key]=null;
		Cookie.setValue(key,0,d?d:null,p?p:'/',Date.expired);
	}
	this.setValue=function(name,value,domain,path,et){
		var c=this.enCode(name)+'='+this.enCode(value);
	c+=this.allDomain?';domain='+this.enCode(this.rootDomain):(domain?';domain='+this.enCode(domain):'');
		c+=this.allPath?';path=/':(!path?'':';path='+this.enCode(path));
		c+=";expires="+(et?et:this.defaultET())
		document.cookie=c;
	}
	this.save=function(){for(e in this.keys){this.setValue(e,this.keys[e],null,'/',this.defaultET())}}
	this.load=function(){
		this.keys=[];
		this.extKeys=[];
		this.bak=[];
		var a1=document.cookie.replace(/\s/ig,'').split(';');
		var a2;
		for(var n=0;n<a1.length;n++){
		a2=a1[n].split('=');
		if(this.keys[a2[0]]!=null){this.extKeys[a2[0]]=a2[1]}
		else{this.keys[a2[0]]=a2[1]}
		}
		this.bak=this.keys;
	}
	this.add=function(key,value,y,m,d,h,min,s){
		if(!key){return}
		this.keys[key]=value;
		var d1=new Date();
		this.setValue(key,value,null,'/',y!=null?d1.addDate(y,m,d,h,min,s).toGMTString():(this.defaultET()));
	}
	this.set=function(key,value,y,m,d,h,min,s){this.add(key,value,y,m,d,h,min,s)}
	this.deepDel=function(key){
		with(this){
		for(var n=0;n<=domainArr.length;n++){
			for(var m=0;m<=pathArr.length;m++){this.del(key,domainArr[n],pathArr[m])}
			}
		}
	}
	this.clear=function(){
		var a=$G(this.keys);
		for(var n=0;n<a.length;n++){this.deepDel(a[n])}
		a=$G(this.extKeys);
		for(var n=0;n<a.length;n++){this.deepDel(a[n])}
	}
	this.get=function(key){
		this.load();
		var v=this.keys[key]?this.keys[key]:this.extKeys[key];
		return v?this.deCode(v):v
	}
	this.enCode=function(s){return this.code?encodeURI(s):escape(s)}
	this.deCode=function(s){return this.code?decodeURI(s):unescape(s)}
}

yun.Query=function(){
	this.keys=[];
	this.index=[];
	this.load=function(){
	this.keys=[];
	var s,a1,a2;
	s=location.search.substr(1);
	if(s.length<3){return}
	a1=s.split('&');
	for(var n=0;n<a1.length;n++){
		a2=a1[n].split('=');
		this.add(a2[0],a2[1])
		}
	}
	this.init=function(){this.load()}
	this.get=function(key){return this.keys[key]}
	this.add=function(key,value){
	if(this.keys[key]!=null){this.keys[key].push(value)}
	else{
		this.index.push(key);
		this.keys[key]=[value]}
	}
	this.del=function(key){this.keys[key]=null}
	this.toString=function(){
	var s='';
	for(var n=0;n<this.index.length;n++){
		s+=this.index[n]+'='+this.get(this.index[n])+'&';
	}
	return s
	}
}

//获取浏览器可见区域属性
yun.ViewArea = function(){
	this.scrollTop=function(){return isCSS1Dom ? document.documentElement.scrollTop : document.body.scrollTop;};
	this.scrollLeft=function(){return isCSS1Dom ? document.documentElement.scrollLeft : document.body.scrollLeft;};
	this.height=function(){var y1=document.body.clientHeight;var y2=document.documentElement.clientHeight;return isCSS1Dom ? y2 : y1;};
	this.width=function(){var x1=document.body.clientWidth;var x2=document.documentElement.clientWidth;return isCSS1Dom ? x2 : x1;};	
	}
	
var Ajax=new yun.Ajax();
var Position=new yun.Position();
//var CharSorter=new yun.CharSorter();
var Query=new yun.Query();
Query.init();

var Cookie=new yun.Cookie();
Cookie.init();

var ViewArea = new yun.ViewArea();

