         return
 
       # TODO: else
-
-      centerX = Math.round( @viewportX + (mouseX) - (@canvas.width / 2) )
-      centerY = Math.round( @viewportY + (mouseY) - (@canvas.height / 2) )
-
-      height = @msa.zoomer.columnHeight
-      width = @msa.zoomer.columnWidth
-      @viewportX = centerX
-      @viewportY = centerY
-      @viewportX = Math.round(centerX / width * (width + @dblClickVx))
-      @viewportY = Math.round(centerY / height * (height + @dblClickVy))
-
-
-      @msa.zoomer.columnWidth += @dblClickVx
-      @msa.zoomer.columnHeight += @dblClickVy
-      @refreshZoom()
+      return [mouseX,mouseX]
+
+    _onDblClick: (e) ->
+      #if not @rect? or @rect?.left is 0
+      #  @rect = @canvas.getBoundingClientRect()
+      @draglock = undefined
+
+      [mouseX,mouseY]= getMouseCoords e
+
+      @moveCenter mouseX,mouseY
+      @zoomCanvas @dblClickVx,@dblClickVy
+      @draw()
+
+      #console.log "#mouse:" + mouseX + ",y:" + mouseY
+      #console.log "#viewix:" + @viewportX + ",y:" + @viewportY
+
+    moveCanvasDragEvent: (e) ->
+      distX = e.pageX - @dragStartX
+      distY = e.pageY - @dragStartY
+
+      @viewportX = @dragViewStartX - distX
+      @viewportY = @dragViewStartY - distY
 
       @checkPos()
-
       @draw()
-
-      @msa.log.log "viewportX:" + @viewportX + ",viewportY:" + @viewportY
-
-      @ctx.fillStyle = "#ff0000"
-      @ctx.fillRect mouseX,mouseY,10,10
-      @ctx.fillRect @canvas.width/2,0,1,@canvas.height
-
-      console.log "#mouse:" + mouseX + ",y:" + mouseY
-      #console.log "#center:" + centerX + ",y:" + centerY
-      console.log "#viewix:" + @viewportX + ",y:" + @viewportY
-      @draglock = undefined
+      @pauseEvent e
+
+    # moves the center of the canvas to relative x,y
+    moveCenter: (mouseX,mouseY) ->
+
+      @viewportX = Math.round( @viewportX + (mouseX) - (@canvas.width / 2) )
+      @viewportY = Math.round( @viewportY + (mouseY) - (@canvas.height / 2) )
+
+      #TODO: do we need to check here -> redundant with zoomer
+      @checkPos()
+
+    # this rescales the center for vx and vy
+    zoomCanvas: (vx, vy) ->
+
+      newVx = @msa.zoomer.columnWidth * vx
+      newVy = @msa.zoomer.columnHeight * vy
+
+      if newVx < 1 or newVy < 1
+        console.log "invalid zoom level - x:" + newVx + "y:" + newVy
+      else
+        if vx isnt 0
+          @viewportX = Math.round(@viewportX / @msa.zoomer.columnWidth * (newVy))
+        if vy isnt 0
+          @viewportY = Math.round(@viewportY / @msa.zoomer.columnHeight * (newVx))
+
+        @msa.zoomer.columnWidth += vx
+        @msa.zoomer.columnHeight += vy
+
+        @refreshZoom()
+
+        #TODO: do we need to check here -> redundant with zoomer
+        @checkPos()
+
+    # goes through all events all checks for callbacks
+    checkForEvents: (mouseX, mouseY) ->
+      for name,arr of @events
+
+        if (arr[0] <= mouseX and mouseX <= ar[2]) and
+        (arr[1] <= mouseY and mouseY <= arr[3])
+          arr[4]()
 
     checkPos: ->
       [@viewportX,@viewportY] = @_checkPos @viewportX,@viewportY

      @canvas.id = "can"
      @ctx = @canvas.getContext "2d"
      @canvas.setAttribute "id","#{@globalID}_canvas"

      @canvasWrapper = document.createElement "div"
      @canvasWrapper.appendChild @canvas

      @tilesX = Math.ceil(@canvas.width / @tileSize)
      @tilesY = Math.ceil(@canvas.height / @tileSize)

      #console.log "#tiles x:" + @tilesX + ",y:" + @tilesY

      @canvas.addEventListener "mousemove", (e) =>
        @_onMouseMove e

      @canvas.addEventListener "dblclick", (e) =>
        @_onDblClick e

      @canvas.addEventListener "contextmenu", (e) =>
        @_onContextMenu e

      @canvas.addEventListener "mousedown", (e) =>
        @_onMouseDown e

      @canvas.addEventListener "mouseup", (e) =>
        @_onMouseUp e

      @canvas.addEventListener "mouseout", (e) =>
        @_onMouseOut e

      @events = {}

      @map = []

      @viewportX = 120
      @viewportY = 100
      @dblClickVx = 2
      @dblClickVy = 2

      #@animate()
      @timestamp = 0

    _onMouseMove: (e) ->
      if @dragStartX? and @draglock?
        @moveCanvasDragEvent e

    _onMouseUp: (e) ->
      if @dragStartX? and @draglock?
        @moveCanvasDragEvent e

      @draglock = undefined

    _onMouseOut: (e) ->
      @draglock = undefined

    _onMouseDown: (e) ->
      [mouseX,mouseY]= getMouseCoords e

      unless @checkForEvents mouseX,mouseY
        @pauseEvent e
        @dragStartX = e.pageX
        @dragStartY = e.pageY
        @dragViewStartX = @viewportX
        @dragViewStartY = @viewportY
        @draglock = true

    _onContextMenu: (e) ->
      #TODO: only on dblclick
      @zoomCanvas 1 / @dblClickVx, 1 / @dblClickVy
      @draw()

    # TODO: move to utils
    getMouseCoords: (e) ->
      # center the view on double click
      mouseX = e.offsetX
      mouseY = e.offsetY
