package teach
{
	import flash.events.*;
	import flash.display.*;
	import flash.text.*;
	import flash.utils.Timer;

	import com.actiontad.javascript.DOMEx;
	

	public class dragHTMLItemsIntoFlash extends Sprite
	{

		private var timer:Timer;
		private var textToShow:TextField;

		public var mouseOverFlash:Boolean; //public vars and methods will be accessible via javascript

		private var establishDrag:XML =
			;

		public function dragHTMLItemsIntoFlash() {
			
			textToShow = new TextField();
			textToShow.defaultTextFormat = new TextFormat("arial", 20, 0xFFFFFF, true);
			timer = new Timer(500, 1);
			textToShow.autoSize = TextFieldAutoSize.LEFT;
			
			if (stage) { ini(null); }
			else { addEventListener(Event.ADDED_TO_STAGE, ini); }

		}

		private function ini(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, ini);
			addChild(textToShow);
			//waiting for the stage is not enough, we must also give some time for the DOM to be ready
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, begin);
			timer.start();
		}

		private function begin(e:Event):void {
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE, begin);

			graphics.beginFill(0x0000FF, .8);
			graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);

			//this if statement will be false if the call back is not added
			if (DOMEx.addCall("swfJSLink", new DOMEx(this).invokeViaJavaScript))
			{ DOMEx.makeMasterTalk("comWithFlash", "swfJSLink", DOMEx.idGet()); }
			//the makeMasterTalk and invokeViaJavaScript methods are the magic
			//they allow javascript to access any public method or property via the comWithFlash method that is created in the DOM

		}

		public function establishElementToDrag(element:String, idOfSWF:String = "theSWF"):void {
			DOMEx.call(establishDrag, element, idOfSWF);
			//kind of a 'round a bout', javascript going through the swf to call on itself,
			//it's just that most of the javascript is contained in the SWF.
			//The comWithFlash method is javascripts communication point for all public methods and properties in the swf.
		}
		
		public function upDateTextField(toThis:String):void {
			textToShow.text = toThis;//this function gets called from javascript
		}

	}
	
}