﻿//拖放程序
var xDrag = Class.create();
xDrag.prototype = {
  //拖放对象
  initialize: function(xDrag, options) {
	this.xDrag = $getObj(xDrag);//拖放对象
	this._x = this._y = 0;//记录鼠标相对拖放对象的位置
	this._marginLeft = this._marginTop = 0;//记录margin
	this._zIndex = 0;//记录z-index
	//事件对象(用于绑定移除事件)
	this._fM = BindAsEventListener(this, this.Move);
	this._fS = Bind(this, this.Stop);
	
	this.SetOptions(options);
	
	this.Limit = !!this.options.Limit;
	this.mxLeft = parseInt(this.options.mxLeft);
	this.mxRight = parseInt(this.options.mxRight);
	this.mxTop = parseInt(this.options.mxTop);
	this.mxBottom = parseInt(this.options.mxBottom);
	
	this.LockX = !!this.options.LockX;
	this.LockY = !!this.options.LockY;
	this.Lock = !!this.options.Lock;
	
	this.onStart = this.options.onStart;
	this.onMove = this.options.onMove;
	this.onStop = this.options.onStop;
	
	//this._Handle = $getObj(this.options.Handle) || this.xDrag;
	//alert(cleanWhitespaces(this.xDrag).length);
	this._Handle = $getObj(this.options.Handle) || cleanWhitespaces(this.xDrag)[0];
	
	this._mxContainer = $getObj(this.options.mxContainer) || null;
	
	this.xDrag.style.position = "absolute";
	//设置初始位置
	this.xDrag.style.width = this.options.Width + "px";
	this.xDrag.style.height = this.options.Height + "px";
	this.xDrag.style.left = this.options.marginLeft + "px";
	this.xDrag.style.top = this.options.marginTop + "px";
	this.xDrag.style.zIndex = this.options.zIndex;
	
	//透明
	if(Browser.ie && !!this.options.Transparent){
		//填充拖放对象
		with(this._Handle.appendChild(document.createElement("div")).style){
			width = height = "100%"; backgroundColor = "#fff"; filter = "alpha(opacity:0)";
		}
	}
	
	//修正范围
	this.Repair();
	//根据前台页面传递的是否可移动执行是否可以被拖动


//阻止标题栏链接默认跳转事件	
	this._Handle.onclick=function(e){
        //阻止默认浏览器动作(W3C)
        if ( e && e.preventDefault ){
            e.preventDefault();
        }else{
            //IE中阻止函数器默认动作的方式
            window.event.returnValue = false;
        }
	}
	
	addEventHandler(this._Handle, "mousedown", BindAsEventListener(this, this.Start));
  },
  //设置默认属性
  SetOptions: function(options) {
	this.options = {//默认值
		Width:			0,//容器宽度
		Height:			0,//容器高度
		marginLeft:		0,//设置左边距
		marginTop:		0,//设置右边距
		zIndex:			0,//设置层级
		Handle:			"",//设置触发对象（不设置则使用拖放对象）
		Limit:			false,//是否设置范围限制(为true时下面参数有用,可以是负数)
		mxLeft:			0,//左边限制
		mxRight:		9999,//右边限制
		mxTop:			0,//上边限制
		mxBottom:		9999,//下边限制
		mxContainer:	"",//指定限制在容器内
		LockX:			false,//是否锁定水平方向拖放
		LockY:			false,//是否锁定垂直方向拖放
		Lock:			false,//是否锁定
		Transparent:	false,//是否透明
		onStart:		function(){},//开始移动时执行
		onMove:			function(){},//移动时执行
		onStop:			function(){}//结束移动时执行
	};
	Extend(this.options, options || {});
  },
  //准备拖动
  Start: function(oEvent) {

	if(this.Lock){ return; }
	this.Repair();
	//记录鼠标相对拖放对象的位置
	this._x = oEvent.clientX - this.xDrag.offsetLeft;
	this._y = oEvent.clientY - this.xDrag.offsetTop;
	//记录margin
	this._marginLeft = parseInt(CurrentStyle(this.xDrag).marginLeft) || 0;
	this._marginTop = parseInt(CurrentStyle(this.xDrag).marginTop) || 0;
	//记录z-index
	var _obj = cleanWhitespaces($getObj("father"));//读取所有子节点,并清除空白节点
	if(_obj.length == 0){
	    _maxZ = 0;
	}else{
		var _maxZ = new Array;
	    for (var i=0;i<_obj.length;i++){
		    _maxZ.push(_obj[i].style.zIndex);
	    }
	    _maxZ = Math.max.apply(null,_maxZ);
	}
	//alert(_maxZ);
	this._zIndex = ++_maxZ || 0;
	this.xDrag.style.zIndex = this._zIndex;

//	this._marginLeft = this.marginLeft || 0;
//	this._marginTop = this.marginTop || 0;	
	//mousemove时移动 mouseup时停止
	addEventHandler(document, "mousemove", this._fM);
	addEventHandler(document, "mouseup", this._fS);
	if(Browser.ie){
		//焦点丢失
		addEventHandler(this._Handle, "losecapture", this._fS);
		//设置鼠标捕获
		this._Handle.setCapture();
	}else{
		//焦点丢失
		addEventHandler(window, "blur", this._fS);
		//阻止默认动作
		oEvent.preventDefault();
	};
	//附加程序
	this.onStart();
  },
  //修正范围

  Repair: function() {
	if(this.Limit){
		//修正错误范围参数
		this.mxRight = Math.max(this.mxRight, this.mxLeft + this.xDrag.offsetWidth);
		this.mxBottom = Math.max(this.mxBottom, this.mxTop + this.xDrag.offsetHeight);
		//如果有容器必须设置position为relative来相对定位，并在获取offset之前设置
		!this._mxContainer || CurrentStyle(this._mxContainer).position == "relative" || (this._mxContainer.style.position = "relative");
	}
  },

  //拖动
  Move: function(oEvent) {
	//判断是否锁定
	if(this.Lock){ this.Stop(); return; };
	//清除选择
	window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
	//设置移动参数
	var iLeft = oEvent.clientX - this._x, iTop = oEvent.clientY - this._y;
	//设置范围限制
	if(this.Limit){
		//设置范围参数
		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 || this._mxContainer.offsetWidth));
			//mxBottom = Math.min(mxBottom, this._mxContainer.clientHeight);
		};
		//修正移动参数
		iLeft = Math.max(Math.min(iLeft, mxRight - this.xDrag.offsetWidth), mxLeft);
		iTop = Math.max(Math.min(iTop, mxBottom - this.xDrag.offsetHeight), mxTop);
	}
	//设置位置，并修正margin
	if(!this.LockX){ this.xDrag.style.left = iLeft - this._marginLeft + "px"; }
	if(!this.LockY){ this.xDrag.style.top = iTop - this._marginTop + "px"; }
	//附加程序
	this.onMove();
  },

  //停止拖动
  Stop: function() {
	//移除事件
	removeEventHandler(document, "mousemove", this._fM);
	removeEventHandler(document, "mouseup", this._fS);
	if(Browser.ie){
		removeEventHandler(this._Handle, "losecapture", this._fS);
		this._Handle.releaseCapture();
	}else{
		removeEventHandler(window, "blur", this._fS);
	};
	//附加程序
	this.onStop();
  }
};
