/**
  @author David Piegza

  Implements a selection for objects in a scene.
  
  It invokes a callback function when the mouse enters and when it leaves the object.
  Based on a Three.js selection example.
  
  Parameters:
    domElement: HTMLDomElement
    selected: callback function, passes the current selected object (on mouseover)
    clicked: callback function, passes the current clicked object
 */

THREE.ObjectSelection = function(parameters) {
  var parameters = parameters || {};

  this.domElement = parameters.domElement || document;
  this.projector = new THREE.Projector;
  this.INTERSECTED;
  
  var _this = this;
  
  var callbackSelected = parameters.selected;
  var callbackClicked = parameters.clicked;
  var mouse = { x: 0, y: 0 };

  this.domElement.addEventListener( 'mousemove', onDocumentMouseMove, false );
  function onDocumentMouseMove( event ) {
    mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
    mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  }

  this.domElement.addEventListener( 'click', onDocumentMouseClick, false );
  function onDocumentMouseClick( event ) {
    if(_this.INTERSECTED) {
      if(typeof callbackClicked === 'function') {
        callbackClicked(_this.INTERSECTED);
      }
    }
  }

  this.render = function(scene, camera) {
    // find intersections
    camera.update();

    var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
    this.projector.unprojectVector( vector, camera );

    var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );

    var intersects = ray.intersectScene( scene );
    if( intersects.length > 0 ) {
      if ( this.INTERSECTED != intersects[ 0 ].object ) {
        if ( this.INTERSECTED ) {
          this.INTERSECTED.materials[ 0 ].color.setHex( this.INTERSECTED.currentHex );
        }

        this.INTERSECTED = intersects[ 0 ].object;
        this.INTERSECTED.currentHex = this.INTERSECTED.materials[ 0 ].color.getHex();
        this.INTERSECTED.materials[ 0 ].color.setHex( 0xff0000 );
        if(typeof callbackSelected === 'function') {
          callbackSelected(this.INTERSECTED);
        }
      }
    } else {
      if ( this.INTERSECTED ) {
        this.INTERSECTED.materials[ 0 ].color.setHex( this.INTERSECTED.currentHex );
      }
      this.INTERSECTED = null;
      if(typeof callbackSelected === 'function') {
        callbackSelected(this.INTERSECTED);
      }
    }
  }  
}







@@ -70,18 +70,16 @@
  scene.%0A
-  
 %0A  It in
@@ -198,18 +198,16 @@
 xample.%0A
-  
 %0A  Param
@@ -576,16 +576,18 @@
 rojector
+()
 ;%0A  this
@@ -600,18 +600,16 @@
 SECTED;%0A
-  
 %0A  var _
@@ -621,18 +621,16 @@
 = this;%0A
-  
 %0A  var c
@@ -1300,56 +1300,8 @@
 ) %7B%0A
-    // find intersections%0A    camera.update();%0A%0A
     
@@ -1423,16 +1423,22 @@
  var ray
+caster
  = new T
@@ -1445,18 +1445,23 @@
 HREE.Ray
+caster
 (
- 
 camera.p
@@ -1483,14 +1483,9 @@
 .sub
-Self( 
+(
 came
@@ -1495,17 +1495,16 @@
 position
- 
 ).normal
@@ -1508,17 +1508,16 @@
 malize()
- 
 );%0A%0A    
@@ -1536,16 +1536,22 @@
 ts = ray
+caster
 .interse
@@ -1556,24 +1556,30 @@
 sect
-Scene( scene 
+Object(scene, true
 );%0A
+%0A
     
@@ -1727,38 +1727,32 @@
 RSECTED.material
-s%5B 0 %5D
 .color.setHex( t
@@ -1902,30 +1902,24 @@
 TED.material
-s%5B 0 %5D
 .color.getHe
@@ -1948,38 +1948,32 @@
 RSECTED.material
-s%5B 0 %5D
 .color.setHex( 0
@@ -2178,22 +2178,16 @@
 material
-s%5B 0 %5D
 .color.s
@@ -2379,15 +2379,7 @@
 %0A  %7D
-  %0A%7D%0A%0A%0A%0A%0A
 %0A
+%7D
 %0A

Update three.js calls in ObjectSelection