// ==================================================
// $id: dragdrop, $ver: 0.01, $last: 10.01.2008  Exp$
// $dsc: drag'n'drop functionality for popup divs
// $seealso: popdiv.js, functions.js
// ==================================================

var this_x, this_y;
var floatDiv;

var False = function(){return false;}

// --------------------------------------------------
// AdjustShadow()
// --------------------------------------------------
var AdjustShadow = function(div, dxShadow) {
	var sh = ge(div.id+"_shadow");
	if (sh) {
		if (isNaN(dxShadow)) dxShadow = 5;

		sh.style.width = div.offsetWidth+'px';
		sh.style.height = div.offsetHeight+'px';
		sh.style.left = parseInt(div.style.left)+dxShadow+'px';
		sh.style.top = parseInt(div.style.top)+dxShadow+'px';
	}
} // AdjustShadow()

// --------------------------------------------------
// Move()
// --------------------------------------------------
var Move = function(div, x, y, dxShadow) {
	if (!div) return;

	var left = parseInt(div.style.left)+x;
	var top = parseInt(div.style.top)+y;
	div.style.left = left+'px';
	div.style.top = top+'px';

	AdjustShadow(div, dxShadow);
} // Move()
	
// --------------------------------------------------
// StartDrag()
// --------------------------------------------------
var StartDrag = function(e) {
	if (!e) e = window.event;
	div = ge('popdiv');
	this_x = e.clientX + document.body.scrollLeft;
	this_y = e.clientY + document.body.scrollTop;
	floatDiv = div;

	addEvent(document, "mousemove", MoveDrag);
	document.onmouseup = StopDrag;
	//if (document.body.setCapture) document.body.setCapture();

	var b = document.body;
  //b.ondrag = False();
  //b.onselectstart = False();
  if (mybw.gecko) b.style.MozUserSelect = 'none';
  b.style.cursor = 'move';
} // StartDrag()

// --------------------------------------------------
// StopDrag()
// --------------------------------------------------
var StopDrag = function(e) {
	//if (document.body.releaseCapture) document.body.releaseCapture();

	removeEvent(document, "mousemove", MoveDrag);
	document.onmouseup = null;
	floatDiv = null;

	var b = document.body;
	//b.ondrag = null;
	//b.onselectstart = null;
	if (mybw.gecko) b.style.MozUserSelect = '';
	b.style.cursor = '';
} // StopDrag()

// --------------------------------------------------
// MoveDrag()
// --------------------------------------------------
var MoveDrag = function(e) {
	var x = e.clientX + document.body.scrollLeft;
	var y = e.clientY + document.body.scrollTop;
	if (this_x == x && this_y == y) return;

	Move(floatDiv, (x - this_x), (y - this_y), 10);
	this_x = x;
	this_y = y;
} // MoveDrag()

