package
{

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

  	import flash.external.*;

  	import com.actiontad.javascript.events.DOMExEvent;
  	import com.actiontad.javascript.events.DOMExEventDispatcher;


  	[SWF(frameRate = '20', backgroundColor = '0x000000', width = '800', height = '300')]
	public class wrapperDragResponse extends Sprite
	{


		private var ballDragStatus:Boolean = false; //true when the ball is being dragged

		private var wrapperPassage:DOMExEventDispatcher;

		private var wrapperPassage2:DOMExEventDispatcher;

		private var theBall:Sprite;

		public function get dragStatus():Boolean { return ballDragStatus; }
  

		public function wrapperDragResponse() {

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

		}

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


			theBall = new Sprite();

			with(theBall.graphics) { beginFill(0x00FF00); drawCircle(0,0,25); }

			theBall.x = stage.stageWidth/2;
			theBall.y = stage.stageHeight/2;


			addChild(theBall);


			wrapperPassage = new DOMExEventDispatcher("document.onmousedown", ["clientX", "clientY"]);
			wrapperPassage.addEventListener(DOMExEvent.DOMEX_EVENT, dragHandler);

			wrapperPassage2 = new DOMExEventDispatcher("document.onmouseup", ["clientX", "clientY"]);
			wrapperPassage2.addEventListener(DOMExEvent.DOMEX_EVENT, dragRelease);

     
			theBall.addEventListener(Event.ENTER_FRAME, considerBallPosition);

			stage.addEventListener(MouseEvent.MOUSE_UP, dragRelease);


			var theOnMoveJavaScript:XML = 
			<script>
			<![CDATA[
			function () {

				dragStatus = false;
				document.onmousemove = function (e) {

					var thee = (e != undefined) ? e : event;
					if (dragStatus == true) {
						document.getElementById("aBall").style.left = thee.clientX -(52/2) +"px";
						document.getElementById("aBall").style.top = thee.clientY -(54/2)+ "px";
					}
				};

			}
			]]>
			</script>;

			setTimeout(function ():void {ExternalInterface.call(theOnMoveJavaScript);}, 500);

		}

		private function considerBallPosition(e:Event):void {

			theBall.alpha = 1;

			if (theBall.x >= stage.stageWidth - theBall.width/2 || theBall.x <= theBall.width/2)
			{theBall.x = -30;theBall.alpha -= .2}

			if (theBall.y >= stage.stageHeight - theBall.height/2 || theBall.y <= theBall.height/2)
			{theBall.y = -30;theBall.alpha -= .2;}

		}

		private function dragHandler(e:Event):void {

			var dragStatusToTrue:XML = <script><![CDATA[function () { dragStatus = true; }]]></script>;
			ExternalInterface.call(dragStatusToTrue);
			theBall.startDrag(true);theBall.visible = false;
			ballDragStatus = true;

			setTimeout(function ():void { theBall.visible = true; }, 50);

		}

		private function dragRelease(e:Event):void {

			var dragStatusToFalse:XML = <script><![CDATA[function () { dragStatus = false; }]]></script>;
			ExternalInterface.call(dragStatusToFalse);
			theBall.stopDrag();
			ballDragStatus = false;

		}

 	}
}