/*中文UTF*/

function iedom4moz(win) {
	//文档兼容
	win.HTMLDocument.prototype.__defineGetter__("all", function(){
		return this.getElementsByName("*");
	});
	
	win.HTMLCollection.prototype.item = function(s){
		return this[s];
	};
		
	//事件兼容
	win.constructor.prototype.attachEvent = win.HTMLDocument.prototype.attachEvent = win.HTMLElement.prototype.attachEvent = function(e, f) {
		var e = e.replace(/on/,"");
    	this.addEventListener(e, function(e){
			win.event = e;
      		return f();
      	}, false);

		//this.addEventListener(e.replace(/^on/i,""), f, false);
	}
	
	win.constructor.prototype.detachEvent = win.HTMLDocument.prototype.detachEvent = win.HTMLElement.prototype.detachEvent = function(e, f) {
		this.removeEventListener(e.replace(/^on/i,""),f,false);
	}
	
	with(win.Event.constructor.prototype){
		__defineGetter__("srcElement", function(){
			return this.target;
		});
	
		__defineSetter__("returnValue", function(b){
			if(!b) this.preventDefault();
		});
	
		__defineSetter__("cancelBubble",function(b){
			if(b)this.stopPropagation();
		});
		
		__defineGetter__("fromElement",function(){
			var o = (this.type == "mouseover" && this.relatedTarget) || (this.type == "mouseout" && this.target) || null;
			if(o) {
				while(o.nodeType!=1) {
					o=o.parentNode;
				}
			}
			return o;
		});
		
		__defineGetter__("toElement",function(){
			var o=(this.type=="mouseover"&&this.target)||(this.type=="mouseout"&&this.relatedTarget)||null;
			if(o)
				while(o.nodeType!=1)
					o=o.parentNode;
			return o;
		});
		
		__defineGetter__("x",function(){
			return this.pageX;
		});
		
		__defineGetter__("y",function(){
			return this.pageY;
		});
		
		__defineGetter__("offsetX",function(){
			return this.layerX;
		});
		
		__defineGetter__("offsetY",function(){
			return this.layerY;
		});
	}
		
	//节点操作兼容
	win.Node.prototype.replaceNode = function(o){
		this.parentNode.replaceChild(o,this);
	}
	
	win.Node.prototype.removeNode = function(o){
		if(b)
			return this.parentNode.removeChild(this);
		var range=  document.createRange();
		range.selectNodeContents(this);
		return this.parentNode.replaceChild(range.extractContents(),this);
	}
	
	win.Node.prototype.swapNode = function(o){
		return this.parentNode.replaceChild(o.parentNode.replaceChild(this,o), this);
	}
	
	win.Node.prototype.contains = function(o){
		return o ? (o == this ? true : arguments.callee(o.parentNode)) : false;
	}
	
	//HTML元素兼容
	with(win.HTMLElement.prototype){
		__defineGetter__("parentElement",function(){
			return this.parentNode == this.ownerDocument ? null : this.parentNode;
		});
	
		__defineGetter__("children",function(){
			var c = [];
			for(var i = 0, cs = this.childNodes; i < cs.length; i++){
				if(cs[i].nodeType==1)
					c.push(cs[i]);
			}
			return c;
		});
	
		__defineGetter__("canHaveChildren",function(){
			return !/^(area|base|basefont|col|frame|hr|img|br|input|isindex|link|meta|param)$/i.test(this.tagName);
		});
	
		__defineSetter__("outerHTML", function(s){
			var r = this.ownerDocument.createRange();
			r.setStartBefore(this);
			void this.parentNode.replaceChild(r.createContextualFragment(s),this);
			return s;
		});
		
		__defineGetter__("outerHTML", function(){
			var as = this.attributes;
			var str = "<" + this.tagName;
			for(var i = 0,al = as.length; i < al; i++){
				if(as[i].specified)
					str += " " + as[i].name + "=\"" + as[i].value + "\"";
			}
			return this.canHaveChildren ? str + ">" : str + ">" + this.innerHTML + "</" + this.tagName + ">";
		});
	
		__defineSetter__("innerText", function(s){
			return this.innerHTML = win.document.createTextNode(s);
		});
		
		__defineGetter__("innerText", function(){
			var r = this.ownerDocument.createRange();
			r.selectNodeContents(this);
			return r.toString();
		});
	
		__defineSetter__("outerText", function(s){
			void this.parentNode.replaceChild(win.document.createTextNode(s),this);
			return s;
		});
		
		__defineGetter__("outerText", function(){
			var r = this.ownerDocument.createRange();
			r.selectNodeContents(this);
			return r.toString();
		});
	}
	
	win.HTMLElement.prototype.insertAdjacentElement = function(s, o){
		return (s=="beforeBegin"&&this.parentNode.insertBefore(o,this))||(s=="afterBegin"&&this.insertBefore(o,this.firstChild))||(s=="beforeEnd"&&this.appendChild(o))||(s=="afterEnd"&&((this.nextSibling)&&this.parentNode.insertBefore(o,this.nextSibling)||this.parentNode.appendChild(o)))||null;
	}
	
	win.HTMLElement.prototype.insertAdjacentHTML = function(s,h){
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		this.insertAdjacentElement(s,r.createContextualFragment(h));
	}
	
	win.HTMLElement.prototype.insertAdjacentText = function(s,t){
		this.insertAdjacentElement(s, win.document.createTextNode(t));
	}
	
	//XMLDOM兼容
	win.ActiveXObject = function(s){
		switch(s){
			case "XMLDom":
			win.document.implementation.createDocument.call(this,"text/xml","", null);
			//domDoc = document.implementation.createDocument("text/xml","", null);
			break;
		}
	}
	
	win.XMLDocument.prototype.LoadXML = function(s){
		for(var i=0,cs=this.childNodes,cl=childNodes.length;i<cl;i++)
			this.removeChild(cs[i]);
		this.appendChild(this.importNode((new DOMParser()).parseFromString(s,"text/xml").documentElement,true));
	}
	
	win.XMLDocument.prototype.selectSingleNode = win.Element.prototype.selectSingleNode = function(s){
		return this.selectNodes(s)[0];
	}
	
	win.XMLDocument.prototype.selectNodes = win.Element.prototype.selectNodes = function(s){
		var rt=[];
		for(var i=0,rs=this.evaluate(s,this,this.createNSResolver(this.ownerDocument==null?this.documentElement:this.ownerDocument.documentElement),XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null),sl=rs.snapshotLength;i<sl;i++)
			rt.push(rs.snapshotItem(i));
		return rt;
	}
	
	win.XMLDocument.prototype.__proto__.__defineGetter__("xml", function(){
		try{
			return new XMLSerializer().serializeToString(this);}
		catch(e){
			return win.document.createElement("div").appendChild(this.cloneNode(true)).innerHTML;
		}
	});
	
	win.Element.prototype.__proto__.__defineGetter__("xml", function(){
		try{
			return new XMLSerializer().serializeToString(this);}
		catch(e){
			return win.document.createElement("div").appendChild(this.cloneNode(true)).innerHTML;
		}
	});
	
	win.XMLDocument.prototype.__proto__.__defineGetter__("text", function(){
		return this.firstChild.textContent;
	});
	
	win.Element.prototype.__proto__.__defineGetter__("text", function(){
		return this.textContent;
	});
	
	win.Element.prototype.__proto__.__defineSetter__("text", function(s){
		return this.textContent = s;
	});
	
	if (navigator.userAgent.toLowerCase().indexOf('opera') != -1 && opera.version()) {
		win.document.documentElement.clientWidth = win.document.body.clientWidth = Math.min(win.innerWidth, win.document.body.clientWidth);
		win.document.documentElement.clientHeight = win.document.body.clientHeight = Math.min(win.innerHeight, win.document.body.clientHeight);
	}
}
iedom4moz(window);