package
{


  import flash.display.*;
  import flash.events.*;
  import flash.utils.*;

  import ThreeDObjectMaker;  //in the same folder as this class and with the same package structure
  //don't forget, papervision3d is also needed for the threedobjectmaker

  import com.tadSrc.tadsClasses.DOMEx;


  public class DOMExMouseFollow extends Sprite
  {


	private var theCube:ThreeDObjectMaker;

	private var mousePossession:Boolean = true; //true when the wrapper has possession of the mouse
	
	public function get possessionStatus():Boolean { return mousePossession; }
	public function set possessionStatus(toThis:Boolean):void { mousePossession = toThis; }
	

	public function DOMExMouseFollow()
	{

		if (stage) ini()
		else
		addEventListener(Event.ADDED_TO_STAGE, ini, false, 0, true);

	}

	private function ini(e:Event = null):void {

		theCube = new ThreeDObjectMaker(30, null, false); //this needs to be false, else run time error.
		//this class will have the domex support, so we must turn it off in the object maker


		var sW:Number = stage.stageWidth;
		var sH:Number = stage.stageHeight;

		theCube.loadDirectObjectSpecs("thesespecs", null, 
		{"materialType":"bitmapFile", "bitmap":"cubeSkin1.png", "objectRadius":(sW - 3),  
		 "toRotate":0, "viewWidth":sW, "viewHeight":sH});

		addChild(theCube);
		

		var thisDOMExTimer:Timer = new Timer(1000, 1);
		thisDOMExTimer.addEventListener(TimerEvent.TIMER_COMPLETE, wrapperMouseFollowSetup, false, 0, true);
		thisDOMExTimer.start();
		
		removeEventListener(Event.ADDED_TO_STAGE, ini);

	}
	
	private function wrapperMouseFollowSetup(e:TimerEvent):void
	{

		//The DOMEx class takes care of everything!
		//All one has to do is embed the flash in the wrapper with an id!

		//The cube will change direction and rotation based on the mouse, no matter where it is!

		//And no focus on the flash is needed, it will all happen right away!!


		if (DOMEx.addCall("jsToasMouseFollow", new DOMEx(this).invokeViaJavaScript))
		{DOMEx.makeMasterTalk("wMouseFollow", "jsToasMouseFollow", DOMEx.idGet());}

		stage.addEventListener(Event.MOUSE_LEAVE, turnOnJsCom);
		stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);

		var establishJavaScript:XML = 
		<script>
		<![CDATA[
		function () {
			 wMouseFollow("possessionStatus", "=", true);
			
			//we use document because window does not work in IE
			document.onmousemove =  function (e) {
			  if (wMouseFollow("possessionStatus") == true) {
				var thee = (e != undefined) ? e : event;
  				var mX = thee.clientX;var mY = thee.clientY;
				wMouseFollow("mouseEventHandler", [mX, mY]);
			  } 
			}
		}
		]]>
		</script>;

		DOMEx.call(establishJavaScript);


	}

	private function turnOnJsCom(e:Event):void
	{

		 possessionStatus = true;

	}

	public function get mouseEventHandler():Function { return mouseMoveHandler; }

	private function mouseMoveHandler(e:* = null):void 
	{
		if (e as Array == null) possessionStatus = false;

		var possibleJavaScriptX:Number = (e as Array != null) ? Number(e[0]) : e.target.mouseX;
		var possibleJavaScriptY:Number = (e as Array != null) ? Number(e[1]) : e.target.mouseY;

		var xDistance:Number = possibleJavaScriptX - (stage.stageWidth/2);
		var yDistance:Number = possibleJavaScriptY - (stage.stageHeight/2);

		if (theCube.constructed) {
			theCube.threeDObject.rotationY = (xDistance/2);
			theCube.threeDObject.rotationX = (yDistance/2);
		}

	}

  }

}