/**
 * @author richt / http://richt.me
 * @author WestLangley / http://github.com/WestLangley
 *
 * W3C Device Orientation control (http://w3c.github.io/deviceorientation/spec-source-orientation.html)
 */

THREE.DeviceOrientationControls = function( object ) {

	var scope = this;

	this.object = object;
	this.object.rotation.reorder( "YXZ" );

	this.enabled = true;

	this.deviceOrientation = {};
	this.screenOrientation = 0;

	this.alphaOffset = 0; // radians

	var onDeviceOrientationChangeEvent = function( event ) {

		scope.deviceOrientation = event;

	};

	var onScreenOrientationChangeEvent = function() {

		scope.screenOrientation = window.orientation || 0;

	};

	// The angles alpha, beta and gamma form a set of intrinsic Tait-Bryan angles of type Z-X'-Y''

	var setObjectQuaternion = function() {

		var zee = new THREE.Vector3( 0, 0, 1 );

		var euler = new THREE.Euler();

		var q0 = new THREE.Quaternion();

		var q1 = new THREE.Quaternion( - Math.sqrt( 0.5 ), 0, 0, Math.sqrt( 0.5 ) ); // - PI/2 around the x-axis

		return function( quaternion, alpha, beta, gamma, orient ) {

			euler.set( beta, alpha, - gamma, 'YXZ' ); // 'ZXY' for the device, but 'YXZ' for us

			quaternion.setFromEuler( euler ); // orient the device

			quaternion.multiply( q1 ); // camera looks out the back of the device, not the top

			quaternion.multiply( q0.setFromAxisAngle( zee, - orient ) ); // adjust for screen orientation

		}

	}();

	this.connect = function() {

		onScreenOrientationChangeEvent(); // run once on load

		window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
		window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );

		scope.enabled = true;

	};

	this.disconnect = function() {

		window.removeEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
		window.removeEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );

		scope.enabled = false;

	};

	this.update = function() {

		if ( scope.enabled === false ) return;

		var alpha = scope.deviceOrientation.alpha ? THREE.Math.degToRad( scope.deviceOrientation.alpha ) + this.alphaOffset : 0; // Z
		var beta = scope.deviceOrientation.beta ? THREE.Math.degToRad( scope.deviceOrientation.beta ) : 0; // X'
		var gamma = scope.deviceOrientation.gamma ? THREE.Math.degToRad( scope.deviceOrientation.gamma ) : 0; // Y''
		var orient = scope.screenOrientation ? THREE.Math.degToRad( scope.screenOrientation ) : 0; // O

		setObjectQuaternion( scope.object.quaternion, alpha, beta, gamma, orient );

	};

	this.dispose = function() {

		this.disconnect();

	};

	this.connect();

};

@@ -2056,21 +2056,22 @@
 %0A%0A%09%09var 
-alpha
+device
  = scope
@@ -2084,24 +2084,67 @@
 eOrientation
+;%0A%0A%09%09if ( device ) %7B%0A%0A%09%09%09var alpha = device
 .alpha ? THR
@@ -2161,39 +2161,22 @@
 gToRad( 
-scope.deviceOrientation
+device
 .alpha )
@@ -2178,20 +2178,21 @@
 pha ) + 
-this
+scope
 .alphaOf
@@ -2206,16 +2206,18 @@
 0; // Z%0A
+%0A%09
 %09%09var be
@@ -2221,39 +2221,22 @@
  beta = 
-scope.deviceOrientation
+device
 .beta ? 
@@ -2256,39 +2256,22 @@
 gToRad( 
-scope.deviceOrientation
+device
 .beta ) 
@@ -2281,18 +2281,20 @@
 ; // X'%0A
+%0A
 %09%09
+%09
 var gamm
@@ -2297,39 +2297,22 @@
 gamma = 
-scope.deviceOrientation
+device
 .gamma ?
@@ -2333,39 +2333,22 @@
 gToRad( 
-scope.deviceOrientation
+device
 .gamma )
@@ -2360,16 +2360,18 @@
  // Y''%0A
+%0A%09
 %09%09var or
@@ -2463,16 +2463,17 @@
 // O%0A%0A%09%09
+%09
 setObjec
@@ -2537,24 +2537,30 @@
  orient );%0A%0A
+%09%09%7D%0A%0A%0A
 %09%7D;%0A%0A%09this.d
@@ -2584,20 +2584,21 @@
 () %7B%0A%0A%09%09
-this
+scope
 .disconn

Clean up