var util={
	//根据id获取元素
	$: function(e){
		return util.isString(e)?document.getElementById(e):e
	},
    //是否是字符串
    isString: function(a){
        return (typeof a=='string')
    },
    //创建AJAX请求
    req: function(){
    	var req;
		try {
    		req = new XMLHttpRequest();
  		} catch (trymicrosoft) {
    		try {	
    			req = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (othermicrosoft) {
     			try {
        			req = new ActiveXObject("Microsoft.XMLHTTP");
      			} catch (failed) {
        			req = false;
     			}
    		}    
    	}
    	return req;
    },
    /*发送AJAX请求(opt参数 sync:表示同步执行,resp:默认是json)*/
    ajax: function(url,fn,opt){
    	var req=util.req();
    	if(req){
    		opt=opt||{method:'GET'};
    		req.open(opt.method||'GET', url, !opt.sync);
    		if(opt.method='post'){
    			if(opt.data){
    				req.setRequestHeader("Content-Length",opt.data.length);
    			}
    			req.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
    		} 
     		req.onreadystatechange = function(){
     			if(req.readyState == 4){
     				var data=req.responseText;
     				if('text'!=opt.resp)try{eval("data="+data)}catch(e){};    
     				fn.call(req,data)				    				
     			}
     		};    
     		req.send(opt.data||null)		
    	}
    },
    /*绑定对像事件(调用格式:obj,event,fn)*/
    $aev: function(t,e,fn){
    	t=util.$(t);
		if(window.addEventListener){
			t.addEventListener(e,fn,false)
		}else if(window.attachEvent){
			t.attachEvent("on"+e,fn)
		}else{
			t["on"+e]=fn
		}
	},
    /*对数组中部分元素执行fn功能(调用格式:arr,fn,startIndex,endIndex)*/
    map: function(a,fn,/*optional*/f,l){
        var i=f||0,l=l||a.length;
        for(i;i<l;i++){
            var v=fn(a[i],i);
            if(v!=undefined)
                return v
        }
    },
    /*把obj绑定到方法fn(调用格式:obj,fn,argu..)*/
	$b: function(){
		var a=Array.prototype.slice.call(arguments),o=a.shift(),f=a.shift();
		return function(){
		   return f.apply(o,a.concat(Array.prototype.slice.call(arguments)))
		}		
	},
	/*设置属性(调用格式:elm1,elm2..,attrObj)*/
	setAttrib:function(){
		var a=Array.prototype.slice.call(arguments),o=a.pop();
		util.map(a,function(e){
			e=util.$(e);
			for(k in o){
				var p=o[k];             
				if(k=="style")
				   util.isString(p)? e.style.cssText=p : util.setStyle(e,p);
				else if(k=="class"||k=='className')
				   e.className=p;
				else
				   'innerHTML'==k?e.innerHTML=p:e.setAttribute(k,p)
			}							
		})
	},
    /*设置元素css样式(调用格式:elm1,elm2..,styleObj)*/
    setStyle: function() {
		var a=Array.prototype.slice.call(arguments),o=a.pop();
		util.map(a,function(e){
			e=util.$(e);
			var t=e.style,v;
			for(var p in o){
				v=o[p];
				t[p]=p.toLowerCase()!='z-index'?(util.isString(v)?v:v+"px"):v; 
			}							
		});
	},    
    //是否是字符串
    isString:function(a){
        return (typeof a=='string')
    },
	//是否是HTML元素
	isHTMLElement:function(e){
		return e&&typeof e=="object"&&e.nodeName
	},
    /*设置回车键跳转(调用格式:elm1,elm2..,fn),如果fn必选没有传null,elm可以是{src:'',foc:fn}*/
    setKeyFocus:function(){
    	var a=Array.prototype.slice.call(arguments),l=a.length-1,f=a[l];
    	util.map(a,function(n,i){
    		if(n)util.$aev(n.src||n,'keypress',function(e){
    			e=e||event;
    			if(e&&e.keyCode==13){
    				for(var j=i+1,x;j<=l;j++){
    					x=a[j];
    					if(x){j<l?util.$(x.src||x).focus():(f&&f());return}
    				}
    			}
    		})
    	},0,l)	
    },
    //等比缩放图片(this，宽，高)
    drawImage : function(e, W, H, f, c){
		var w=W||e.width,h=H||e.height,l = new Image(),i;l.src=e.src;
		if(l.width > 0 && l.height > 0){
			if(l.width/l.height >= w/h){
				if(l.width > w){
					e.width = w;
					e.height = (l.height*w)/l.width
				}else{
					e.width = l.width;
					e.height = l.height
				}
			}else{    
				if(l.height > h){
					e.height = h;
					e.width = (l.width*h)/l.height
				}else{
					e.width = l.width;
					e.height = l.height
				}
			}
		}
		i=h-e.height;
 		if(i>0)e.style.marginTop=i+'px';
 		if(util.mozilla&&w>e.width)e.style.marginLeft=(w-e.width)/2+'px';	
		e.style.visibility='visible';
		//e.parentNode.style.visibility='visible'
	}
};
//设置浏览器和对应的版本
(function(o,u){
	o.version=(u.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/)||[])[1];
	o.opera=/opera/.test(u);
	o.ie=(/msie/.test(u))&&(!/opera/.test(u));
	o.mozilla=(/mozilla/.test(u))&&(!/(compatible|webkit)/.test(u))
})(util,navigator.userAgent.toLowerCase());
