
function centerDivX(divObj) {
  divObj.scrollLeft = (divObj.scrollWidth - divObj.clientWidth) / 2;
}

function centerDivY(divObj) {
  divObj.scrollTop = (divObj.scrollHeight - divObj.clientHeight) / 2;
}

function autoScrollDivX(divObj, mouseX) {
  var maxScrollLeft = divObj.scrollWidth - divObj.clientWidth;
  // Calculate relative mouse position inside the div, scaling it so that it approaches 0
  // at the left edge and the max scrollLeft value of the div at the right edge.
  var relMouseX = Math.round( maxScrollLeft / divObj.clientWidth * (mouseX - divObj.offsetLeft) );

  // Scroll the div until scrollLeft reaches the relative mouse position.
  if (relMouseX < divObj.scrollLeft) {
	for (; divObj.scrollLeft > 0 && relMouseX < divObj.scrollLeft; divObj.scrollLeft = divObj.scrollLeft - 2) {
	  // Updating an invisible input forces Firefox to redraw on every iteration and scroll smoothly
	  // even when first moving the cursor over the div. Without this the div would seem to just jump
	  // to the new position.
	  document.getElementById('autoscrolltmp').value = divObj.scrollLeft;
	}
  } else if (relMouseX > divObj.scrollLeft) {
	for (i=1; divObj.scrollLeft < maxScrollLeft && relMouseX > divObj.scrollLeft; divObj.scrollLeft = divObj.scrollLeft + 2) {
	  document.getElementById('autoscrolltmp').value = divObj.scrollLeft;
	}
  }
}

function autoScrollDivY(divObj, mouseY) {
  var maxScrollTop = divObj.scrollHeight - divObj.clientHeight;
  // Calculate relative mouse position inside the div, scaling it so that it approaches 0
  // at the top edge and the max scrollTop value of the div at the bottom edge.
  // Note: in Firefox and Safari offsetTop returns the actual Y coordinate of the div but in IE
  // it returns the coordinate within an enclosing div: adding offsetParent.offsetTop gets the actual
  // coordinate if there's only one enclosing div; more would require looping through them.
  var relMouseY = Math.round( maxScrollTop / divObj.clientHeight * (mouseY - (divObj.offsetTop + divObj.offsetParent.offsetTop)) );

  // Scroll the div until scrollTop reaches the relative mouse position.
  if (relMouseY < divObj.scrollTop) {
	for (; divObj.scrollTop > 0 && relMouseY < divObj.scrollTop; divObj.scrollTop = divObj.scrollTop - 2) {
	  // Updating an invisible input forces Firefox to redraw on every iteration and scroll smoothly
	  // even when first moving the cursor over the div. Without this the div would seem to just jump
	  // to the new position.
	  document.getElementById('autoscrolltmp').value = divObj.scrollTop;
	}
  } else if (relMouseY > divObj.scrollTop) {
	for (; divObj.scrollTop < maxScrollTop && relMouseY > divObj.scrollTop; divObj.scrollTop = divObj.scrollTop + 2) {
	  document.getElementById('autoscrolltmp').value = divObj.scrollTop;
	}
  }
}
