var browser = {
	isIE : (window.ActiveXObject) ? true : false,
	isFF : (document.getBoxObjectFor) ? true : false,
	isOpera : (window.opera) ? true : false,
	isSafari : (window.openDatabase) ? true : false,
	isChrome : (window.MessageEvent && !document.getBoxObjectFor) ? true : false
};

function $Dom(sId){
	if(typeof sId !== 'undefault'){
		if(this === window) return new $Dom(sId);
		if(sId instanceof $Dom) return sId;

		this.elem = null;
		if(typeof sId === 'string'){
			this.elem = (/<([a-z]+|h[1-5])>/i.test(sId)) ? document.createElement(sId) : document.getElementById(sId);
		}else{
			this.elem = sId;
		}
	}else{
		alert('请输入相应的对象或是ID。');
	}
};

$Dom.prototype = {
	nameSplace : function(splace){
		var cur_domain = window;

		splace = splace || null;
		if(splace){
			if(splace.indexOf('.') != -1){
				var domains = splace.split('.');

				for(var i = 0, len = domains.length; i < len; i++){
					if(!cur_domain[domains[i]]){
						cur_domain[domains[i]] = {};
						cur_domain = cur_domain[domains[i]];
					}else{
						alert('【'+domains[i]+'】已经有相同的域名存在。');
						return;
					}
				}
			}else{
				if(!cur_domain[splace]){
					cur_domain[splace] = {};
					cur_domain = cur_domain[splace];
				}else{
					alert('【'+splace+'】已经有相同的域名存在。');
					return;
				}
			}
			return cur_domain;
		}else{
			alert('没有输入相应的域名空间。');
		}
	},

	setOnload : function(fn){
		if(typeof fn == 'function'){
			window.addEventListener ? window.addEventListener('load',fn,false) : window.attachEvent('onload',fn);
		}else{
			alert(fn+' 不是函数。');
		}
	},

	getElement : function(){
		return this.elem;
	},

	hasClass : function(className){
		return new RegExp('\\b'+className+'\\b').test(this.elem.className);
	},

	addClass : function(className){
		if(!this.hasClass(className)){
			this.elem.className = this.elem.className ? this.elem.className +' '+className : className;
		}
	},

	removeClass : function(className){
		if(this.hasClass(className)){
			var aClass = this.elem.className.split(/\s+/),
				 newClass = [];

			for(var i = 0, len = aClass.length; i < len; i++){
				if(aClass[i] !== className){
					newClass[newClass.length] = aClass[i];
				}
			}
			this.elem.className = newClass.join(' ');
		}else{
			return false;
		}
	},

	replaceClass : function(oldClassName,newClassName){
		var node = this.elem;

		if(this.hasClass(oldClassName)){
			var reg = new RegExp('\\b'+oldClassName+'\\b','g');
			node.className = node.className.replace(reg,newClassName);
		}
	},

	getElementsByClassName : function(className){
		var nodes = this.elem.getElementsByTagName('*'),
			 newNodes = [];

		for(var i = 0, len = nodes.length; i < len; i++){
			if($Dom(nodes[i]).hasClass(className)){
				newNodes[newNodes.length] = nodes[i];
			}
		}
		return newNodes;
	},

	getParent : function(){
		return this.elem ? this.elem.parentNode : null;
	},

	getNextSibling : function(){
		var nextNode = this.elem;

		while(nextNode){
			nextNode = nextNode.nextSibling;
			if(nextNode && nextNode.nodeType === 1){
				return nextNode;
			}
		}
		return null;
	},

	getPreviousSibling : function(){
		var preNode = this.elem;

		while(preNode){
			preNode = preNode.previousSibling;
			if(preNode && preNode.nodeType === 1){
				return preNode;
			}
		}
		return null;
	},

	getFirstChild : function(){
		if(this.elem.hasChildNodes()){
			var nodes = this.elem.childNodes,
				firstNode = nodes[0];

			return (firstNode.nodeType !== 1) ? $Dom(firstNode).getNextSibling() : firstNode;
		}else{
			return null;
		}
	},

	getLastChild : function(){
		if(this.elem.hasChildNodes()){
			var nodes = this.elem.childNodes,
					lastNode = nodes[nodes.length-1];

			return (lastNode.nodeType !== 1) ? $Dom(lastNode).getPreviousSibling() : lastNode;
		}else{
			return null;
		}
	},

	getChildren : function(){
		if(this.elem.hasChildNodes()){
			var nodes = this.elem.childNodes,
					aNodes = [];

			for(var i = 0, len = nodes.length; i < len; i++){
				if(nodes[i].nodeType === 1){
					aNodes[aNodes.length] = nodes[i];
				}
			}
			return aNodes;
		}
	},

	appendChild : function(node){
		if(this.elem && node){
			this.elem.appendChild(node);
			return node;
		}else{
			alert('参数错误');
		}
	},

	removeChild : function(node){
		return (node) ? this.elem.removeChild(node) : null;
	},

	replaceChild : function(newNode){
		return (newNode) ? this.elem.parentNode.replaceChild(newNode,this.elem) : null;
	},

	getNode : function(className){
		var parent = this.elem;
		while(parent = parent.parentNode){
			if(parent.tagName.toLowerCase() == 'html'){
				alert('没有匹配的结点。');
				return null;
			}
			if($Dom(parent).hasClass(className)){
				return parent;
			}
		}
	},

	getFirstChild : function(){
		var node = this.elem;

		return this.elem.firstChild.nodeType === 1 ? this.elem.firstChild : $Dom(this.elem.firstChild).getNextSibling();
	},

	getLastChild : function(){
		var node = this.elem;

		return this.elem.lastChild.nodeType === 1 ? this.elem.lastChild : $Dom(this.elem.lastChild).getPreviousSibling();
	},

	insertAfter : function(newNode){
		var referenceNode = this.elem;

		if(!newNode || !referenceNode || !referenceNode.parentNode){
			return null;
		}
		return (referenceNode.nextSibling) ? referenceNode.parentNode.insertBefore(newNode,referenceNode.nextSibling) : referenceNode.parentNode.appendChild(newNode);
	},

	insertBefore : function(newNode){
		var referenceNode = this.elem;

		if(!newNode || !referenceNode || !referenceNode.parentNode){
			return null;
		}
		return referenceNode.parentNode.insertBefore(newNode,referenceNode);
	},

	getStyle : function(property){
		var node = this.elem,
			sValue = node.style[property],
			computed;

		if(window.getComputedStyle){	// for FF Opera Safari etc.
			if(!sValue){
				computed = node.ownerDocument.defaultView.getComputedStyle(node,null);
				if(computed){
					sValue = computed[property];
				}
			}
			return sValue;
		}else if(document.documentElement.currentStyle){	 // for IE
			var value;

			switch(property){
				case 'opacity' :
					value = 100;
					try{
						value = node.filters['DXImageTransform.Microsoft.Alpha'].opacity;
					}catch(e){
						try{
							value = node.filters('alpha').opacity;
						}catch(err){
//							alert('IE filter failed!');
						}
					}
					return (value / 100);
				case 'float' :
					property = 'styleFloat';
				default :
					value = node.currentStyle ? node.currentStyle[property] : null;
					return (node.style[property] || value);
			}
		}
	},

	setStyle : function(property,val){
		if(window.getComputedStyle){
			if(this.elem){
				if(property == 'float'){
					property = 'cssFloat';
				}
				this.elem.style[property] = val;
			}else{
				alert('This element is undefined.');
			}
		}else if(document.documentElement.currentStyle){
			if(this.elem){
				switch(property){
					case 'opacity' :
						this.elem.style.filter = 'alpha(opacity='+val*100+')';
						break;
					case 'float' :
						property = 'styleFloat';
					default :
						try{
							this.elem.style[property] = val;
						}catch(e){ }

				}
			}else{
				alert('This element is undefined.');
			}
		}
	},

	setAttribute : function(property,value){
		if(this.elem.nodeType === 1){
			this.elem.setAttribute(property,value);
			return this.elem;
		}else{
			alert('对象无些属性。');
		}
	},

	getAttribute : function(property){
		if(this.elem.nodeType === 1){
			return this.elem.getAttribute(property);
		}else{
			alert('对象无些属性。');
		}
	},

	toggle : function(){
		(this.getStyle('display') == 'block' || this.getStyle('display') == '') ? this.setStyle('display','none') : this.setStyle('display','block');
	},

	getXY : function(){
		var left = this.elem.offsetLeft,
			top = this.elem.offsetTop,
			parent = this.elem;

		while(parent.tagName.toLowerCase() != 'body'){
			parent = parent.parentNode;
			if($Dom(parent).getStyle('position') == 'absolute' || $Dom(parent).getStyle('position') == 'relative'){
				left+= parent.offsetLeft;
				top+= parent.offsetTop;
				break;
			}else{
				left+= parent.offsetLeft;
				top+= parent.offsetTop;
			}
		}

		return{
			'x' : left,
			'y' : top
		}
	},

	setXY : function(x,y){
		if(this.elem){
			this.setStyle('position','absolute');
			this.setStyle('left',x+'px');
			this.setStyle('top',y+'px');
		}else{
			alert('无此对象。');
		}
	},

	setX : function(x){
		if(this.elem){
			if(this.getStyle('position') == 'absolute' || this.getStyle('position') == 'relative'){
				this.setStyle('left',x+'px');
			}else{
				this.setStyle('position','absolute');
				this.setStyle('left',x+'px');
			}
		}else{
			alert('无此对象。');
		}
	},

	setY : function(y){
		if(this.elem){
			if(this.getStyle('position') == 'absolute' || this.getStyle('position') == 'relative'){
				this.setStyle('top',y+'px');
			}else{
				this.setStyle('position','absolute');
				this.setStyle('top',y+'px');
			}
		}else{
			alert('无此对象。');
		}
	},

	getViewportHeight : function(){
		var height = self.innerHeight,	 // Safari, Opera
			doc = this.elem || document,
			mode = doc.compatMode;

		if((mode || browser.isIE) && !browser.isOpera){	// IE
			height = (mode == 'CSS1Compat') ? doc.documentElement.clientHeight : doc.body.clientHeight;
		}
		return height;
	},

	getViewportWidth : function(){
		var width = self.innerWidth,
			doc = this.elem || document,
			mode = document.compatMode;

		if(mode || browser.isIE){
			width = (mode == 'CSS1Compat') ? doc.documentElement.clientWidth : doc.body.clientWidth;
		}
		return width;
	},

	getDocumentHeight : function(){
		var doc = this.elem || document,
			scrollHeight = (doc.compatMode != 'CSS1Compat' || browser.isSafari) ? doc.body.scrollHeight : doc.documentElement.scrollHeight,
			height = Math.max(scrollHeight,this.getViewportHeight());

		return height;
	},

	getDocumentWidth : function(){
		var doc = this.elem || document,
			scrollWidth = (doc.compatMode != 'CSS1Compat' || browser.isSafari) ? doc.body.scrollWidth : doc.documentElement.scrollWidth,
			width = Math.max(scrollWidth,this.getViewportWidth());

		return width;
	},

	getDocumentScrollLeft : function(){
		var doc = this.elem || document;
		return (!doc.documentElement) ? doc.scrollLeft : doc.documentElement.scrollLeft;
	},

	getDocumentScrollTop : function(){
		var doc = this.elem || document;
		return (!doc.documentElement) ? doc.scrollTop : doc.documentElement.scrollTop;
	},

	setStop : function(evt){
		evt = evt || window.event;
		evt.preventDefault ? evt.preventDefault() : evt.returnValue = false;
		evt.stopPropagation ? evt.stopPropagation() : evt.cancelBubble = true;
	},

	setImgSize : function(wSize,hSize){
		var bSize, proportion,
				wImg = parseFloat(this.getStyle('width')),
				hImg = parseFloat(this.getStyle('height'));

		bSize = (wImg > hImg);

		if(bSize){	// 宽图
			sImg = wSize || wImg;
			proportion = wImg/sImg;
			this.setStyle('width',sImg+'px');
			this.setStyle('height',hImg/proportion+'px');
		}else{	// 窄图
			sImg = hSize || hImg;
			proportion = sImg/hImg;
			this.setStyle('height',sImg+'px');
			this.setStyle('width',wImg*proportion+'px');
		}
	}
}