     @renderer.setClearColor(@clear, @alpha)
     @container.appendChild(@renderer.domElement)
     @camera.position.z = 100
-    @mouse = new THREE.Vector2(0, 0)
+    @mouseh = new MouseHandler(this)
     @raycaster = new THREE.Raycaster()
 
   render: ->
     @renderer.render(@scene, @camera)
 
+#Mouse handler encapsulates the logic of dealing with mouse events
+class MouseHandler
+
+  constructor: (@ve) ->
+    @pos = new THREE.Vector2(0, 0)
+
+  ondown: (event) -> @baseDown(event)
+  
   updateMouse: (event) ->
-    @mouse.x =  (event.layerX / @container.offsetWidth ) * 2 - 1
-    @mouse.y = -(event.layerY / @container.offsetHeight) * 2 + 1
-    console.log(@mouse.x + "," + @mouse.y)
-
-  handle_mousedown: (event) ->
+    @pos.x =  (event.layerX / @ve.container.offsetWidth ) * 2 - 1
+    @pos.y = -(event.layerY / @ve.container.offsetHeight) * 2 + 1
+    console.log(@pos.x + "," + @pos.y)
+
+  #onmousedown handlers
+  baseDown: (event) ->
     @updateMouse(event)
-    @raycaster.setFromCamera(@mouse, @camera)
-    ixs = @raycaster.intersectObjects(@scene.children, true)
-    console.log "!" if ixs.length > 0
-
+    @ve.raycaster.setFromCamera(@pos, @ve.camera)
+    ixs = @ve.raycaster.intersectObjects(@ve.scene.children, true)
+    if ixs.length > 1 and
+      ixs[1].object.userData instanceof ElementBox and
+      ixs[0].object.userData.cyjs?
+        e = ixs[0].object.userData
+        console.log "! ebox select -- " + e.constructor.name
+        console.log e
+        @ve.container.onmousemove = (eve) => @placingMove(eve)
+        @ve.container.onmousedown = (eve) => @placingDown(eve)
+
+  placingDown: (event) ->
+    console.log "plop"
+    @ve.container.onmousemove = null
+    @ve.container.onmousedown = (eve) => @baseDown(eve)
+
+  #onmousemove handlers
+  placingMove: (event) ->
+    @updateMouse(event)
+

    @height = @ve.height
    @width = @ve.width
    @baseRect = new Shapes.Rectangle(0x262626, 0, 0, 0, @width, @height)
    @ve.scene.add(@baseRect.obj3d)

#VisualEnvironment holds the state associated with the Threejs objects used


#Global event handlers
root.vz_mousedown = (event) ->
  g.ve.mouseh.ondown(event)

#Global state holder
g = {}
