/*
	Version: 1.0; 
	last-modify: 2008-12-09 by talentwyh;
	note: my util.js created, yeah!
	author:talentwyh
	mail: wyhtalent@gmail.com
-------------------------------------------------------------------------------------------------------------------------
	Version: 1.1; 
	last-modify: 2008-12-11 by talentwyh;
	note: add util.setOpacity(), util.cut() method
	author:talentwyh
	mail: wyhtalent@gmail.com
-------------------------------------------------------------------------------------------------------------------------
	Version: 1.2; 
	last-modify: 2008-12-18 by talentwyh;
	note: add util.gT() method
	author:talentwyh
	mail: wyhtalent@gmail.com
*/
if(typeof HTMLElement != "undefined"){
	HTMLElement.prototype.__defineGetter__("currentStyle", function(){
		return this.ownerDocument.defaultView.getComputedStyle(this, null);
	});
}
var util = {
	/*
		@id [string] Element`s id
		@return element
	*/
	G: function(id){return (id.nodeType==1||id.nodeType==9)?id:document.getElementById(id);},
	/*
		@arg [array|object] 
		@return array
	*/
	A: function(arg){var a = new Array(arg.length); var i=arg.length-1;while(i>=0){ a[i]=arg[i]; i--;} return a;},
	/*
		@s [object]
		@d [object]
		@flag [0|1]
		@return void
	*/
	ext:function(s,d,flag){flag=flag||0; for(var k in s){ if(flag==1&&typeof d[k]!="undefined") return ""; d[k]=s[k];}},
	trim: function(str){return str.replace(/(^[\s\t\xa0\u3000]+)|([\u3000\xa0\s\t]+$)/g, "");},
	rc: function(e, c){
		if(!e) return;
		var t = util.trim, r = new RegExp("(^|\\s{1,})" + t(c) + "(\\s{1,}|$)", "g");
		e.className = t(e.className.replace(r, "$2"));
	},
	ac: function(e, c){
		if(!e) return;
		var a = e.className.split(" "), t = util.trim;
		if(!new RegExp("(^|\\s{1,})" + t(c) + "(\\s{1,}|$)").test(e.className))
			e.className = t(a.concat(c).join(" "));
	},
	hc:function(e,c){return e.className.indexOf(c)==-1?false:true;},
	tc:function(e,c){if(util.hc(e,c)) util.rc(e,c); else util.ac(e,c);},
	each: function(d, fun){
		if(d.length||d.length==0) for(var i=0,l=d.length;i<l;i++)fun(d[i],i);
		else if(typeof d=="object") for(var k in d) fun(k,d[k]);
	},
	format:function (str, obj){
		return str.replace(/\{\s*(\w+)\s*\}/g, function(m, i){
			return typeof obj[i]=="undefined"?"":obj[i];
		});
	},
	up:function (e,tag){
		var p=e.parentNode; if(tag=="body") return document.body;
		while(p.tagName!=tag.toUpperCase()){p=p.parentNode; if(p.tagName=="BODY") return null;}
		return p;
	},
	childs:function(e){
		var c=e.childNodes,re=[];
		util.each(c,function(d){if(d.nodeType==1) re.push(d);});
		return re;
	},
	get:function(e){
		var valueT = 0, valueL = 0, e = util.G(e);
		do {
			valueT += e.offsetTop  || 0;
			valueL += e.offsetLeft || 0;
			e = e.offsetParent;
		} while (e);
		return {top: valueT, left: valueL};
	},
	getStyle:function(e,s){
		var re = e.style[s]||e.currentStyle[s];
		return isNaN(parseInt(re))?re:parseInt(re);
	},
	cut: function(value, byteLength, prefix){
		var newvalue = value.replace(/[^\x00-\xff]/g, "**");
       	var length = newvalue.length;
		var limitDate = newvalue.substr(0, byteLength);
      	var count = size = 0; var limitvalue = "";
		for (var i = 0; i < limitDate.length; i++) {
			var flat = limitDate.substr(i, 1);
			if (flat == "*") count++;
		 }
		 var istar = newvalue.substr(byteLength * 1 - 1, 1);
		 size = (count%2==0?count:count-1)/ 2 + (byteLength * 1 - count);
		 limitvalue = value.substr(0, size);
		return limitvalue+(prefix||"");
	},
	h: function(){
		return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	}, 
	w: function(){
		return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
	},
	fullh:function(){return Math.max(util.h(),document.body.scrollHeight);},
	fullw:function(){return Math.max(util.w(),document.body.scrollWidth);},
	gT: function(e, tag){return util.G(e).getElementsByTagName(tag);},
	/**
	 * @param Object ele 表格插入到的元素
	 * @param String data 需要插入的HTML代码
	 * @param Boolean clear 插入时是否检测清理多余TR标签[默认不清理]
	 */
	insert: function(ele, data, clear){
		var clear = clear || false;
		while(ele.childNodes.length > 0){
			ele.removeChild(ele.childNodes[0]);}
		if(data=="") return "";
		var o=document.createElement("div"),ol,nl;
	    o.innerHTML="<table>"+data+"</table>";
	    ol=o.childNodes[0].tBodies[0].rows;
	    if (clear && ele.nodeType == 1 && ele.nodeName == 'TR') {
	    	for (var i = 0; i < ol.length; i++) {
	    		nl = ol[i].cells;
	    		while(nl.length>0) {
					ele.appendChild(nl[0]);
	    		}
	    	}
	    }
	    else {
	    	while(ol.length>0) {
				ele.appendChild(ol[0]);
	    	}
	    } 
	}
};
util.ext({
	bind:function(){
		var __method = this, args = util.A(arguments), object = args.shift();
		return function() {
		  return __method.apply(object, args.concat(util.A(arguments)));
		}
	}
}, Function.prototype);
util.ext({
	setOpacity: function(element, value) {
    	var style = element.style;
		if(document.all){
    		if (value < 0.00001) value = 0;
    		style.filter = 'alpha(opacity=' + (value * 100) + ')';
		}else
			style.opacity = (value == 1 || value === '') ? '' :
     								 (value < 0.00001) ? 0 : value;
	}
},util);