/* CODE BY ANDREW BURKE */
  clickposx = 0;
  clickposy = 0;

  scrollstartleft = 0;
  scrollstarttop = 0;

  ismousedown = false;

  maxleft = 0;
  maxtop = 0;

  function setClickPos(event, targetDiv) {
  
    clickposx = event.screenX;
    clickposy = event.screenY;

    scrollstartleft = parseInt(targetDiv.style.left);
    scrollstarttop = parseInt(targetDiv.style.top);
    
    var framingDiv = document.getElementById("main-window");

    maxleft = parseInt(targetDiv.style.width) - parseInt(framingDiv.style.width);
    maxtop = parseInt(targetDiv.style.height) - parseInt(framingDiv.style.height);

    ismousedown = true;
  }

  function mouseup(event, target) {
    ismousedown = false;
  }

  function moveTarget(event, target) {
    if (ismousedown == true) {
      var newposx = event.screenX;
      var newposy = event.screenY;
      
      var difx = newposx - clickposx;
      var dify = newposy - clickposy;

      var newleft = scrollstartleft + difx;
      if (newleft > 0) { newleft = 0; }
      if (newleft < (0 - maxleft)) { newleft = (0 - maxleft); }

      var newtop = scrollstarttop + dify;
      if (newtop > 0) { newtop = 0; }
      if (newtop < (0 - maxtop)) { newtop = (0 - maxtop); }

      moveToPosition(target, newleft, newtop);
    }
  }

  function moveToPosition(target, x, y) {
    target.style.left = "" + x + "px";
    target.style.top = "" + y + "px";
  }