/*
*@author:tiger
*@fielName:EasyUI拖拽类EasyDragDrop.js
*/
var easyDragDrop = function(dragArea,moveArea,aMatrix,dashedMove){//可拖动元素，需要移动的元素，可拖动范围[上，右，下，左]，是否虚框拖拽
	this.dragArea = dragArea;//拖拽对象
	this.moveArea = moveArea;//移动对象
	this.aMatrix = aMatrix;//可移动范围
	this.dashedMove = dashedMove;//是否开启虚线框移动
	this.dashedBox = null;//虚框
	this.cm = (aMatrix&&aMatrix.length);//是否指定移动范围
	this.onbeforedrag = null;//拖动前事件，形参x,y
	this.ondrag = null;//拖动时事件，形参x,y
	this.ondrop = null;//拖动放开后事件，形参x,y
	this.x = 0;
	this.y = 0;
	this.init();
};
easyDragDrop.prototype = {
	getMASize:function(){
		var cw = parseInt(easyUI.getStyle(this.moveArea,'width')),ch = parseInt(easyUI.getStyle(this.moveArea,'height'));
		var imaw = Math.max(this.moveArea.offsetWidth,cw),imah = Math.max(this.moveArea.offsetHeight,ch);
		return [imaw,imah].slice(0);
	},
	mDown:function(e){
		var pos = easyUI.getPosition(this.moveArea);
		if(this.onbeforedrag&&this.onbeforedrag.constructor==Function){this.onbeforedrag(pos.x,pos.y);}
		this.x = e.clientX-pos.x;
		this.y = e.clientY-pos.y;
		pos = null;
		var dm = this.dashedMove,dbox = this.dashedBox,darea = this.dragArea,xdde = document.documentElement,xdb = document.body;
		if(dm){
			var icwh = this.getMASize();
			dbox.style.width = icwh[0]+'px';
			dbox.style.height = icwh[1]+'px';
			easyUI.setPosition(dbox,(e.clientX-this.x),(e.clientY-this.y));
			dbox.style.cursor = "move";
			dbox.style.display = 'block';
			if(dbox.setCapture){dbox.setCapture();}else{e.preventDefault();}
		}else{
			if(darea.setCapture){darea.setCapture();}else{e.preventDefault();}
			darea.style.cursor = "move";
		}
		var xdw = Math.max(xdde.clientWidth,xdde.scrollWidth,xdb.clientWidth,xdb.scrollWidth);
		var xdh = Math.max(xdde.clientHeight,xdde.scrollHeight,xdb.clientHeight,xdb.scrollHeight);
		var xdsl = 0,xdst = 0;
		if(xdde.style.overflow=='hidden'||xdb.style.overflow=='hidden'){
			var msie = /msie/i.test(navigator.appVersion);
			xdsl = Math.max(xdde.scrollLeft,xdb.scrollLeft);
			xdst = Math.max(xdde.scrollTop,xdb.scrollTop);
			xdw = (msie)?xdsl+Math.min(xdde.offsetWidth,xdb.offsetWidth):xdsl+Math.min(xdde.offsetWidth,xdde.clientWidth,xdb.offsetWidth,xdb.clientWidth);
			xdh = (msie)?xdst+Math.min(xdde.offsetHeight,xdb.offsetHeight):xdst+Math.min(xdde.offsetHeight,xdde.clientHeight,xdb.offsetHeight,xdb.clientHeight);
		}
		if(!this.cm){this.aMatrix = [xdst,xdw,xdh,xdsl];}
		xdde.onmousemove = this.mMove.bind(this);
		xdde.onmouseup = this.mUp.bind(this);
	},
	mMove:function(e){
		var darea = this.dragArea,marea = this.moveArea;
		e = e||window.event;
		var mx = (e.clientX - this.x),my = (e.clientY - this.y);
		var dm = this.dashedMove,dbox = this.dashedBox;
		if(dm){
			var mow = dbox.offsetWidth,moh = dbox.offsetHeight;
		}else{
			var mow = marea.offsetWidth,moh = marea.offsetHeight;
		}
		var aMatrix = this.aMatrix;
		mx = (mx<=aMatrix[3])?aMatrix[3]:(mx>=aMatrix[1]-mow)?aMatrix[1]-mow:mx;
		my = (my<=aMatrix[0])?aMatrix[0]:(my>=aMatrix[2]-moh)?aMatrix[2]-moh:my;
		if(dm){
			easyUI.setPosition(dbox,mx,my);
		}else{
			easyUI.setPosition(marea,mx,my);
		}
		if(this.ondrag&&this.ondrag.constructor==Function){this.ondrag(mx,my);}
	},
	mUp:function(e){
		var darea = this.dragArea,marea = this.moveArea,xdde = document.documentElement;
		e = e||window.event;
		var mx = (e.clientX - this.x),my = (e.clientY - this.y);
		var dm = this.dashedMove,dbox = this.dashedBox;
		if(dm){
			var mow = dbox.offsetWidth,moh = dbox.offsetHeight;
			dbox.style.cursor = "default";
		}else{
			var mow = marea.offsetWidth,moh = marea.offsetHeight;
			darea.style.cursor = "default";
		}
		var aMatrix = this.aMatrix;
		mx = (mx<=aMatrix[3])?aMatrix[3]:(mx>=aMatrix[1]-mow)?aMatrix[1]-mow:mx;
		my = (my<=aMatrix[0])?aMatrix[0]:(my>=aMatrix[2]-moh)?aMatrix[2]-moh:my;
		if(dm){
			easyUI.setPosition(dbox,mx,my);
			if(dbox.releaseCapture){dbox.releaseCapture();}
			dbox.style.display = 'none';
		}
		easyUI.setPosition(marea,mx,my);
		if(darea.releaseCapture){darea.releaseCapture();}
		xdde.onmousemove = function(){};
		xdde.onmouseup = function(){};
		if(this.ondrop&&this.ondrop.constructor==Function){this.ondrop(mx,my);}
	},
	init:function(){
		if(this.dashedMove){
			var pos = easyUI.getPosition(this.moveArea);
			this.x = pos.x;
			this.y = pos.y;
			pos = null;
			this.dashedBox = document.createElement('div');
			var icwh = this.getMASize();
			this.dashedBox.style.cssText = 'display:none;position:absolute;left:'+this.x+'px;top:'+this.x+'px;width:'+icwh[0]+'px;height:'+icwh[1]+'px;overflow:hidden;background:#000;-moz-opacity:0.3;opacity:0.3;filter:alpha(opacity=30);z-index:10001;';
			document.body.appendChild(this.dashedBox);
		}
		this.moveArea.style.position = 'absolute';
		this.moveArea.style.zIndex = 20000;
		this.dragArea.onmousedown = this.mDown.bind(this);
	}
};