var dragX,dragY;
var oldCenterX,oldCenterY;
var dragApproved=false;
var wait=0;

//=============================================================================
function stopDrag()
 {
  if (document.captureEvents) document.releaseEvents(Event.MOUSEMOVE);
  document.onmousemove=null;
  document.onmouseup=null;
  return false;
 }
//=============================================================================
function initDrag(e,over,move,drop)
 {
 	if (!e)
 	 {
 	  e = window.event;
 	  targ = e.srcElement;
 	 } else
 	 {
 	  targ = e.target;
 	 }
//	 alert(targ.id);
	 if (targ.nodeType == 3) targ = targ.parentNode;
  if (over)
  if (targ.id!=over.id) return true;

  dragX=e.clientX;
  dragY=e.clientY;

  if (document.captureEvents) document.captureEvents(Event.MOUSEMOVE);
  document.onmousemove=move;
  document.onmouseup=drop;
  return false;
 }
//=============================================================================
//=============================================================================
//=============================================================================
//=============================================================================
//=============================================================================
//=============================================================================

function moveMap(e)
 {
  if (wait || !dragApproved) return false;
  wait = 1;
 	if (!e) e = window.event;
  setTimeout('wait = 0;', 40);
  nx= e.clientX;
  ny= e.clientY;
  setTimeout('centerTo('+(oldCenterX-nx+dragX)+','+(oldCenterY-ny+dragY)+');',50);
  return false;

 }
//=============================================================================
function dropMap()
 {
  dragApproved=false;
  xBasis=0;
  centerTo(centerX,centerY);
  return stopDrag();
 }
//=============================================================================
function dragMap(e)
 {
 	a=e;
 	if (!e) e = window.event;
 	if (e.which) rightclick = (e.which == 3);
	 else if (e.button) rightclick = (e.button == 2);
	 if (rightclick) { return cancelEvent(e);}
	 e=a;
  oldCenterX=centerX;
  oldCenterY=centerY;
  dragApproved=true;
  return initDrag(e,over,moveMap,dropMap);
 }
//=============================================================================
//=============================================================================
//=============================================================================
function dragRSB(e)
 {
  dragApprovedRSB=true;
  rsbPos=parseInt(rsb.offsetTop);
  return initDrag(e,rsb,moveRSB,dropRSB);
 }
//=============================================================================
function moveRSB(e)
 {
  if (wait || !dragApprovedRSB) return false;
  wait = 1;
 	if (!e) e = window.event;
  setTimeout('wait = 0;', 40);
  nx= e.clientX;
  ny= e.clientY;
  ny=ny-dragY+rsbPos;
  if (ny<40) ny=40;
  if (ny>366) ny=366;
  rsb.style.top=(ny)+'px';
  centerTo(centerX,yVisSize2+(max_y-2*yVisSize2)*(ny-40)/326);
  return false;

 }
//=============================================================================
function dropRSB()
 {
  dragApprovedRSB=false;
  return stopDrag();
 }
//=============================================================================
function dragBSB(e)
 {
  dragApprovedBSB=true;
  bsbPos=parseInt(bsb.offsetLeft);
  return initDrag(e,bsb,moveBSB,dropBSB);
 }
//=============================================================================
function moveBSB(e)
 {
  if (wait || !dragApprovedBSB) return false;
  wait = 1;
 	if (!e) e = window.event;
  setTimeout('wait = 0;', 40);
  nx= e.clientX;
  ny= e.clientY;
  nx=nx-dragX+bsbPos;
  if (nx<64) nx=64;
  if (nx>566) nx=566;
  bsb.style.left=(nx)+'px';
  centerTo(xVisSize2+(max_x-2*xVisSize2)*(nx-64)/502,centerY);
  return false;

 }
//=============================================================================
function dropBSB()
 {
  dragApprovedBSB=false;
  return stopDrag();
 }
//=============================================================================
function setBarsTo(x,y)
 {
  if (dragApprovedBSB || dragApprovedRSB) return;
  nx=(x-xVisSize2)*502/(max_x-2*xVisSize2)+64;
  ny=(y-yVisSize2)*326/(max_y-2*yVisSize2)+40;
  bsb.style.left=(nx)+'px';
  rsb.style.top=(ny)+'px';

 }
//=============================================================================
//=============================================================================
//=============================================================================
//=============================================================================
//=============================================================================
//=============================================================================
