//拖放程序
var SimpleDrag = Class.create();
SimpleDrag.prototype.initialize = function(drag, soption ){	//拖放对象,触发对象
	this.Drag = my$(drag);
	this.mxLeft = 0,//左边限制
	this.mxRight=9999,//右边限制
	this.mxTop=0,//上边限制
	this.mxBottom=9999,//下边限制
	this.setOptions( soption );

	this._x = this._y = 0;
	this._fM = BindAsEventListener_my(this, this.Move);
	this._fS = BindAsEventListener_my(this, this.Stop);
	this.Drag.style.position = "absolute";

	this._mxContainer = my$(this.option.op_mxContainer) || null; //
	this._handle = my$(this.option.op_handle) || this.Drag;		//拖拽对象
	
	this.Repair();
	addEventHandler(this._handle, "mousedown", BindAsEventListener_my(this, this.Start));
	};

//传入参数
SimpleDrag.prototype.setOptions = function( option ){
	this.option = {
		op_mxContainer : "" , //限制范围层的ID号
		op_handle      : ""   //被拖动的层中，鼠标触发的层id
	};
	Extend(this.option , option );
};

  //修正范围
SimpleDrag.prototype.Repair =  function() {
	//修正错误范围参数
	this.mxRight = Math.max(this.mxRight, this.mxLeft + this.Drag.offsetWidth);
	this.mxBottom = Math.max(this.mxBottom, this.mxTop + this.Drag.offsetHeight);
	//如果有容器必须设置position为relative或absolute来相对或绝对定位，并在获取offset之前设置
	!this._mxContainer || CurrentStyle(this._mxContainer).position == "relative" || CurrentStyle(this._mxContainer).position == "absolute" || (this._mxContainer.style.position = "relative");
};

  //准备拖动
SimpleDrag.prototype.Start =  function(oEvent) {
	this.Repair();

	this._x = oEvent.clientX - this.Drag.offsetLeft;
	this._y = oEvent.clientY - this.Drag.offsetTop;

	//记录margin
	this._marginLeft = parseInt(CurrentStyle(this.Drag).marginLeft) || 0;
	this._marginTop = parseInt(CurrentStyle(this.Drag).marginTop) || 0;

	addEventHandler(document, "mousemove", this._fM);
	addEventHandler(document, "mouseup", this._fS);

	if(isIE){
		//焦点丢失
		addEventHandler(this._handle, "losecapture", this._fS);
		//设置鼠标捕获
		this._handle.setCapture();
	}else{
		//焦点丢失
		addEventHandler(window, "blur", this._fS);
		//阻止默认动作
		oEvent.preventDefault();
	};
  };

  //拖动
SimpleDrag.prototype.Move  =  function(oEvent) {
	//清除选择，防止ondrag()事件
	window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
	//设置移动参数
	var iLeft = oEvent.clientX - this._x, iTop = oEvent.clientY - this._y;
	//设置范围参数
	var mxLeft = this.mxLeft, mxRight = this.mxRight, mxTop = this.mxTop, mxBottom = this.mxBottom;
	if(!!this._mxContainer){
		//如果设置了容器，再修正范围参数
			mxLeft = Math.max(mxLeft, 0);
			mxTop = Math.max(mxTop, 0);
			mxRight = Math.min(mxRight, this._mxContainer.clientWidth);
			mxBottom = Math.min(mxBottom, this._mxContainer.clientHeight);
	}
		//修正移动参数
	iLeft = Math.max(Math.min(iLeft, mxRight - this.Drag.offsetWidth), mxLeft);
	iTop = Math.max(Math.min(iTop, mxBottom - this.Drag.offsetHeight), mxTop);

	this.Drag.style.left = iLeft -  this._marginLeft + "px";
	this.Drag.style.top = iTop -  this._marginTop  + "px";
  };

  //停止拖动
SimpleDrag.prototype.Stop =  function() {
	removeEventHandler(document, "mousemove", this._fM);
	removeEventHandler(document, "mouseup", this._fS);
	if(isIE){
		removeEventHandler(this._handle, "losecapture", this._fS);
		this._handle.releaseCapture();
	}else{
		removeEventHandler(window, "blur", this._fS);
	};
  };
