// object movement by mouse:
// usage:
//<div id="objid" style="position:absolute;" onmousedown="javascript:setBoxToMove(this,window.event.offsetX,window.event.offsetY);"></div>

var currentNetDoc = null;
var isMouseDown = false;
var movingObj = null;
var movingBox = null;
var movingLine = null;
var offsX,offsY;
var orgX,orgY;
var rootX,rootY;
var moving = false;
var ctrlMode = false;
var leftButton = true;

function objMove(movX,movY) {
  if (movingObj != null) {
    movingObj.style.left  = (x - offsX) + "px";
    movingObj.style.top = (y - offsY) + "px";
    }
  if (movingBox != null) {
    if (!moving) {
      moving = true;  
      if (Math.abs(movX-orgX) > Math.abs(movY-orgY)) {
        movingBox.movObj(movX - offsX,orgY - offsY);
        }
      else {  
        movingBox.movObj(orgX - offsX,movY - offsY);
        }
      moving = false;  
      }  
    }
  if (movingLine != null) {
    if (!moving) {
      moving = true;  
      if (movingLine.isVert()) {
        movingLine.movObj(movX - offsX,orgY - offsY);
        }
      else {  
        movingLine.movObj(orgX - offsX,movY - offsY);
        }
      moving = false;  
      }  
    }
}

function setLineToMove(obj,evt) {
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  
  currentNetDoc.clearLinesC();

  isMouseDown = true;
  movingLine = currentNetDoc.lineObjFromId(tagId(obj));
  if (movingLine == null) {
    isMouseDown = false;
//    alert("not found " + tagId(obj));
    return;
    }
//    alert("found " + movingLine.fullSubId());
  if (evt.pageX) {
    offsX = evt.layerX;
    offsY = evt.layerY;
    orgX = evt.layerX+movingLine.div.getLeftC();
    orgY = evt.layerY+movingLine.div.getTopC();
    rootX = evt.pageX-evt.layerX-movingLine.div.getLeftC();
    rootY = evt.pageY-evt.layerY-movingLine.div.getTopC();
    }
  else {  
    offsX = evt.offsetX;
    offsY = evt.offsetY;
    orgX = evt.x;    
    orgY = evt.y;    
    }
}

function setBoxToMove(obj,evt) {
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  
  currentNetDoc.clearLinesC();

  isMouseDown = true;
  movingBox = currentNetDoc.getNodeByLId(lIdFromBoxId(tagId(obj)));
  if (evt.pageX) {
    offsX = evt.layerX;
    offsY = evt.layerY;
    orgX = evt.layerX+movingBox.div.getLeft();
    orgY = evt.layerY+movingBox.div.getTop();
    rootX = evt.pageX-evt.layerX-movingBox.div.getLeft();
    rootY = evt.pageY-evt.layerY-movingBox.div.getTop();
    ctrlMode = (evt.modifiers & Event.CONTROL_MASK);
    leftButton = (evt.which == 1);
    }
  else {  
    offsX = evt.offsetX;
    offsY = evt.offsetY;
    orgX = evt.x;    
    orgY = evt.y;    
    ctrlMode = evt.ctrlKey;
    leftButton = (evt.button == 1);
    }
}

function MouseMove(event) {
  if (!isMouseDown) return;
  
  if (!event) event = window.event;
  
  var movX,movY;
  if (event.pageX) {
    movX = event.pageX-rootX;
    movY = event.pageY-rootY;
    }
  else {  
    movX = event.x;    
    movY = event.y;
    }

  objMove(movX,movY);  
}

function MouseUp(event) {
  isMouseDown = false;
  ctrlMode = false;
//  if ((movingObj != null) || (movingBox != null) || (movingLine != null)) {
//    }
  if ((movingBox != null) || (movingLine != null)) currentNetDoc.addCrossingsC();
  movingObj = null;
  movingBox = null;
  movingLine = null;
  offsX = 0;
  offsY = 0;
  orgX = 0;
  orgY = 0;
}

function highlightOn(obj,evt) {
  var box = currentNetDoc.getNodeByLId(lIdFromBoxId(tagId(obj)));
  box.highlight(true,true);
}

function highlightOff(obj,evt) {
  var box = currentNetDoc.getNodeByLId(lIdFromBoxId(tagId(obj)));
  box.highlight(false,true);
}

document.onmouseup = MouseUp;
document.onmousemove = MouseMove;


