 
   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

    @element.addEventListener 'mouseover', (e) => @hovering = true
    @element.addEventListener 'mouseout', (e) => @hovering = false
    @element.addEventListener 'mousedown', (e) => @onMouseDown(e)
    @element.addEventListener 'click', (e) => @canvasGraph.marks.remove(@) if e.layerY < 15
    window.addEventListener 'mousemove', (e) => @onMouseMove(e)
    window.addEventListener 'mouseup', (e) => @onMouseUp(e)


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

