// função para permitir o drag
function enableDrag (el) {
	el.onmousedown = function (event) { doDrag(event, this.parentNode); };
}
if(typeof(enable_WinDrag) != 'function') {	// compatibilidade
	function enable_WinDrag(el)	{
		var obj = el.getElementsByTagName('DIV').item(0);		
		enableDrag(obj);
	}
}
// Função para drag de objetos
var doDrag_restore = document.onselectstart;
function doDrag(ev, obj) {
	var startPos = getMouseXY(ev);
	var win = getWinSize(window);
	
	this.onmouseup = function (event) { undoDrag(event, obj); };
	if(textsel_enabled) {
		doDrag_restore = true;
	}
	disableTextSel();
	
	if(typeof(obj.onstartdrag) == 'function') {
		obj.onstartdrag(ev, obj);
	}
	
	startPos.x = startPos.x - getElX(obj);
	startPos.y = startPos.y - getElY(obj);
	if(typeof(obj.style.width) == 'undefined' && !getStyle(obj, 'width')) {
		obj.style.width = getElWidth(obj) + 'px';
	}
	obj.style.position = 'absolute';
	
	obj.style.left = getElX(obj) + 'px';
	obj.style.top = getElY(obj) + 'px';
	obj.style.marginLeft = '0';
	obj.style.marginTop = '0';
	
	this.onmousemove = function (ev) {
		var coord = getMouseXY(ev);
		var newX  = coord.x - startPos.x;
		var newY  = coord.y - startPos.y;
		if(newX - 20 > 0) {
			if(newX + getElWidth(obj) + 20 < win.w) {
				obj.style.left = newX + 'px';
			}
		}
		if(newY - 20 > 0) {
			if(newY + getElHeight(obj) + 20 < win.h) {
				obj.style.top = newY + 'px';
			}
		}
	}
}
// função para parar o drag
function undoDrag(ev, obj) {
	if(doDrag_restore) {
		enableTextSel();
	}
	this.onmouseup = null;
	this.onmousemove = null;
	
	if(typeof(obj.onfinishdrag) == 'function') {
		obj.onfinishdrag(ev, obj);
	}
}
