/**
 * 命名空间：
 * 作者：
 * 时间：
 * 说明：
 */
$Dom().setOnload(function(){
	posNode = document.createElement('span');
});

// layer层的初始值
var layerPos = {
	'left' : -99999,
	'top' : -99999
}

// 缓动时的移动系数
var tween = {
	time : null,
	_fadeIn : function(x){
		return x*x-0.005;	// 缓动效果。刚开始时会有0.005个像素的后退，然后向前由快到慢的移动
	},
	_fadeOut : function(x){
		return x;
	}
}

function $Effect(sID){
	if(typeof sID !== 'undefault'){
		if(this === window) return new $Effect(sID);
		if(sID instanceof $Effect) return sID;

		this.elem = null;
		if(typeof sID === 'string'){
			this.elem = (/<(.+)>/i.test(sID)) ? document.createElement(sID) : document.getElementById(sID);
		}else{
			this.elem = sID;
		}
	}else{
		alert('请输入相应的对象或是ID。');
	}
}

$Effect.prototype = {
	getElement : function(){
		return this.elem;
	},

	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('element '+this.elem+' 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 :
						this.elem.style[property] = val;
				}
			}else{
				alert('element '+this.elem+' is undefined.');
			}
		}
	},

	getXY : function(){
		var left = this.elem.offsetLeft,
				top = this.elem.offsetTop,
				parent, computed;

		while(parent = this.elem.parentNode){
			if(parent.tagName.toLowerCase() == 'body'){
				if($Effect(parent).getStyle('position') == 'absolute' || $Effect(parent).getStyle('position') == 'relative' || ($Effect(parent).getStyle('overflow') != 'visible' && $Effect(parent).getStyle('overflow') != '' )){
					break;
				}
				left+= parent.offsetLeft;
				top+= parent.offsetTop;
			}else{
				break;
			}
		}

		return{
			'left' : left,
			'top' : 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('无此对象。');
		}
	},

	getDocumentScrollTop : function(){
		var doc = this.elem || document;
		return (!doc.documentElement) ? doc.scrollTop : doc.documentElement.scrollTop;
	},

	getRangValue : function(){
		var rang, value;

		if(browser.isIE){
			rang = document.selection.createRange();
			value = rang.text;
		}else{
			rang = window.getSelection();
			value = rang.toString();
		}
		return value;
	},

	getRangeXY : function(fn){
		var x, y, range,
				_this = this;

		if(browser.isIE){
			setTimeout(function(){
				range = document.selection.createRange();
				x = range.boundingLeft;
				y = range.boundingTop + _this.getDocumentScrollTop();
				if(typeof fn == 'function'){
					setTimeout(function(){fn(x,y)},310);
				}
			},300);
		}else{
			setTimeout(function(){
				try{
					range = window.getSelection().getRangeAt(0);
					if(!range.collapsed){
						range.insertNode(posNode);
						x = $Effect(posNode).getXY().left;
						y = $Effect(posNode).getXY().top;
						if(typeof fn == 'function'){
							fn(x,y);
						}
					}
				}catch(e){ }
			},300);
		}
	}
}