﻿/*DOM常用方法集合
接受参数:无

返回类型:无

应用技术：javascript

制作人：
黄若儒 Roy.Huang

注意:
null
*/
var DocumentUtil=function(){};

try{//设置命名空间
	Class.setNameSpace("org.developerworld.util.DocumentUtil",DocumentUtil);
}
catch(e){}

DocumentUtil.isIE;
DocumentUtil.isFireFox=typeof(HTMLElement)!="undefined" && !window.opera;
DocumentUtil.isOpera;

DocumentUtil.init=function(t){//将所有方法带出指定对象或window
	t=t?t:window;
	for(var i in this)
		if(!t[i] && i!="init" && i!="destroy")
			t[i]=this[i];
};

DocumentUtil.destroy=function(t){//将所有带出指定对象或window中的方法删除
	t=t?t:window;
	for(var i in this)
		if(t[i] && t[i]==this[i] && i!="init" && i!="destroy")
			t[i]=undefined;
};

DocumentUtil.getOuterHTML=function(t){
	t=this.GEI(t);
	if(this.isFireFox){
		var a=t.attributes,str="<"+t.tagName,i=0;
		for(;i<a.length;i++) 
			if(a[i].specified) 
				str+=" "+a[i].name+'="'+a[i].value+'"'; 
		if(!this.canHaveChildren(t)) 
			return str+" />"; 
		return str+">"+t.innerHTML+"</"+t.tagName+">";
	}
	else
		return t.outerHTML;
};

DocumentUtil.setOuterHTML=function(t,text){
	t=this.GEI(t);
	if(this.isFireFox){
		var r = t.ownerDocument.createRange(); 
		r.setStartBefore(t); 
		var df = r.createContextualFragment(text); 
		t.parentNode.replaceChild(df, t); 
		return text;
	}
	else
		t.outerHTML=text;
};

DocumentUtil.canHaveChildren=function(t){
	return !/^(area|base|basefont|col|frame|hr|img|br|input|isindex|link|meta|param)$/.test(this.GEI(t).tagName.toLowerCase());
};

DocumentUtil.GEI=function(t){//根据ID获取对象
	if(typeof(t)!="object")
		t=document.getElementById(t);
	return t;
};

DocumentUtil.GEN=function(t){//根据名获取对象集合
	t=this.GENS(t);
	if(t) t=t[0];
	return t;
};

DocumentUtil.GENS=function(t){//根据名获取对象集合
	if(typeof(t)!="object")
		t=document.getElementsByName(t);
	return t;
};

DocumentUtil.GET=function(t,o){//根据类型获取对象集合
	t=this.GETS(t,o);
	if(t) t=t[0];
	return t;
};

DocumentUtil.GETS=function(t,o){//根据类型获取对象集合
	o=o?o:document;
	if(typeof(t)!="object")
		t=o.getElementsByTagName(t);
	return t;
};

DocumentUtil.CE=function(t){//根据tag名创建元素
	return document.createElement(t);
};

DocumentUtil.AC=function(p,c){//填加元素appendChild
	p.appendChild(c);
	return p;
};

DocumentUtil.RC=function(c,p){//删除元素
	c=this.GEI(c);
	p=p?p:c.parentNode;
	p.removeChild(c);
	c=null;
};

DocumentUtil.getPosition=function(o,style){//获取对象的坐标
	o=this.GEI(o);
	if(o==null)
		return this.onError(0);
	var rst={x:0,y:0};
	rst.x=o.offsetLeft;
	rst.y=o.offsetTop;
	while(o=o.offsetParent){
		rst.x+=o.offsetLeft;
		rst.y+=o.offsetTop;
	}
	if(style){
		var temp=this.getSize();
		rst.x-=temp.scrollLeft;
		rst.y-=temp.scrollTop;
	}
	return rst;
};

DocumentUtil.getMousePosition=function(e,layer){//获取鼠标光标相对于整个页面的位置和相对于当前元素的位置。
	e=e||window.event;
	var rst={x:0,y:0};
	rst.x = !layer?e.pageX||e.clientX+document.body.scrollLeft:e.layerX||e.offsetX;
	rst.y = !layer?e.pageY||e.clientY+document.body.scrollTop:e.layerY||e.offsetY;
	return rst;
};

DocumentUtil.getSize=function(t){//获取页面的一些size
	var rst={clientWidth:0,clientHeight:0,scrollWidth:0,scrollHeight:0,offsetWidth:0,offsetHeight:0}
	var de=document.documentElement;
	var cm=document.compatMode
	if(cm && cm!="BackCompat" && (!t || t==document.body)){
		rst.clientWidth=de.clientWidth;
		rst.clientHeight=de.clientHeight;
		rst.scrollWidth=de.scrollWidth;
		rst.scrollHeight=de.scrollHeight;
		rst.scrollTop=de.scrollTop;
		rst.scrollLeft=de.scrollLeft;
		rst.offsetWidth=de.offsetWidth;
		rst.offsetHeight=de.offsetHeight;
	}
	else{
		t=t?t:document.body;
		rst.clientWidth=t.clientWidth;
		rst.clientHeight=t.clientHeight;
		rst.scrollWidth=t.scrollWidth;
		rst.scrollHeight=t.scrollHeight;
		rst.offsetWidth=t.offsetWidth;
		rst.offsetHeight=t.offsetHeight;
	}
	return rst;
};

DocumentUtil.getBody=function(){//根据标准获取body
	if(document.compatMode && document.compatMode!="BackCompat")
		return document.documentElement
	else
		return document.body;
};