   onMouseDown: (e) ->
     e.preventDefault()
     @element.parentNode.appendChild(@element) #move to end of container for z index
-    if (Math.abs e.layerX - (@canvasXMax-@canvasXMin)) < 10
+    if (Math.abs @elementXOffset(e) - (@canvasXMax-@canvasXMin)) < 10
       @startingPoint = @canvasXMin
       @dragging = true
-    else if e.layerX < 10
+    else if @elementXOffset(e) < 10
       @startingPoint = @canvasXMax
       @dragging = true
-    else if e.layerY > 15
+    else if @elementYOffset(e) > 15
       @moving = true
       @pointerOffset = ((e.pageX) - @canvasXMin)
 

    @dataXMax = @canvasGraph.toDataXCoord(@canvasXMax)

  updateCursor: (e) ->
    if e.layerY < 15
      @element.style.cursor = "pointer"
    else if ((Math.abs e.layerX - (@canvasXMax-@canvasXMin)) < 10) || e.layerX < 10
      @element.style.cursor = "ew-resize"
    else
      @element.style.cursor = "move"


  toCanvasXPoint: (e) -> e.pageX-@canvas.getBoundingClientRect().left - window.scrollX

  elementXOffset: (e) -> e.offsetX || e.clientX - e.target.offsetLeft + window.pageXOffset

  elementYOffset: (e) -> e.offsetY || e.clientY - e.target.offsetTop + window.pageYOffset


module?.exports = CanvasGraph: CanvasGraph, Marks: Marks, Mark: Mark
