﻿function dragClass(){
	this.mouseState = document.all ? 1 : 0 ;
	this.x;
	this.y;
	this.dragId = null;
	this.index = 100;
	//document.onselectstart = function(){return false;};
}
dragClass.prototype = {
	dragStar:function(dragId,moveId){//拖动入口函数
		var _this = this;	
		_this.$(dragId).style.cursor = "move";	
		_this.$(dragId).onmousedown = function(e){
			var e = e ? e : event;
			//_this.$(moveId).style.zIndex = _this.index++;
			
			if(e.button == _this.mouseState)
			{
				_this.setDragInfo(e,moveId,moveId);
				_this.dragPro(moveId);
			}			
		},
		_this.$(moveId).onmousedown = function(e){
			_this.$(moveId).style.zIndex = _this.index++;
		},		
		_this.$(dragId).onmouseup = function(){
			_this.clearDragId();
		}
		document.onmouseup = function(){
			_this.clearDragId();
		}
	},
	setDragInfo:function(e,dragId,moveId){//拖动初始化
		this.x = e.clientX;
		this.y = e.clientY;
		this.dragId = dragId;
		if(this.$(moveId).style.position != "absolute")
		{
			this.$(moveId).style.position = "absolute";
			this.$(moveId).style.left = this.$(moveId).offsetLeft + "px";
			this.$(moveId).style.top = this.$(moveId).offsetTop + "px";
		}				
	},
	setDragReadcc:function(xx,yy,dragId,moveId){//拖动初始化------用于首页面加载-----
		this.x =xx;
		this.y = yy;
		this.dragId = dragId;
		if(this.$(moveId).style.position != "absolute")
		{
			this.$(moveId).style.position = "absolute";
			this.$(moveId).style.left = this.$(moveId).offsetLeft + "px";
			this.$(moveId).style.top = this.$(moveId).offsetTop + "px";
		}				
	},
	clearDragId:function(){ //清除拖动ID
		this.dragId = null;
	},
	dragPro:function(moveId){
		var _this = this;
		document.onmousemove = function(e){
			var e = e ? e : event;
			if(e.button == _this.mouseState && _this.dragId != null)
			{
				var x = e.clientX;
				var y = e.clientY;
				_this.$(moveId).style.left = (parseInt(_this.$(moveId).style.left) + (x - _this.x)) + "px";
				_this.$(moveId).style.top = (parseInt(_this.$(moveId).style.top) + (y - _this.y)) + "px";
				_this.x = x;
				_this.y = y;
				//alert(_this.$(dragId).style.left);
			}
			
		}
	},
	$:function(o){//获取对象
		if(typeof(o) == "string")
		{
			if(document.getElementById(o))
			{
				return document.getElementById(o);
			}
			else
			{
				alert("errId \""+ o + "\"!");
				return false;
			}
		}
		else
		{
			return o;
		}
	}
}