package
{
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getTimer;
	import flash.system.Capabilities;



	public class jerkymouseevents extends Sprite
	{
		[Embed(source="krpano_logo.png")]		public static var krpanologo:Class;


		public var textfield : TextField = null;
		public var fsbutton  : Sprite    = null;
		public var logo      : Sprite    = null;


		public function jerkymouseevents()
		{
			// setup stage
			stage.scaleMode = "noScale";
			stage.align     = "TL";
			stage.frameRate = 60;


			// create a textfield for event traces
			textfield = new TextField();
			textfield.background      = true;
			textfield.backgroundColor = 0xFFFFFF;
			textfield.selectable      = false;

			var textformat:TextFormat = new TextFormat();
			textformat.font = "_typewriter";
			textformat.size = 11.0;
			textfield.defaultTextFormat = textformat;

			this.addChild(textfield);


			// create a fullscreen button
			var fsbutton_txt:TextField = new TextField();
			fsbutton_txt.border          = true;
			fsbutton_txt.background      = true;
			fsbutton_txt.backgroundColor = 0xEEEEEE;
			fsbutton_txt.selectable      = false;
			fsbutton_txt.autoSize        = "left";
			fsbutton_txt.text            = "SWITCH TO FULLSCREEN";

			fsbutton = new Sprite();
			fsbutton.addChild(fsbutton_txt);
			fsbutton.addEventListener(MouseEvent.CLICK, switchToFullscreen);

			this.addChild(fsbutton);


			// create a image (for stressing the flashplayer renderer)
			var krlogo:Bitmap = new krpanologo();
			krlogo.x = (-krlogo.bitmapData.width  * 0.5) * 3.0;
			krlogo.y = (-krlogo.bitmapData.height * 0.5) * 3.0;
			krlogo.alpha = 0.5;
			krlogo.scaleX = krlogo.scaleY = 3.0;
			krlogo.smoothing = true;

			logo = new Sprite();
			logo.addChild( krlogo );

			this.addChild(logo);


			// add events
			stage.addEventListener(Event.RESIZE,      resizeHandler);
			stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

			stage.addEventListener(MouseEvent.MOUSE_DOWN,  mouseHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP,    mouseHandler);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, mouseHandler);


			// call the resizeHandler the first time for correct sizes
			resizeHandler();


			ttrace("Flashplayer 10.1 jerky mouse events bug (only on Mac Firefox in Fullscreen mode)");
			ttrace("your Flashplayer version: " + Capabilities.version + "\n");
		}


		public function ttrace(text:String):void
		{
			textfield.appendText(text + "\n");
			textfield.scrollV = textfield.maxScrollV;
		}


		public function switchToFullscreen(event:Event=null):void
		{
			stage.displayState = "fullScreen";
		}


		public function resizeHandler(event:Event=null):void
		{
			textfield.width  = stage.stageWidth;
			textfield.height = stage.stageHeight;

			logo.x = stage.stageWidth * 0.5;
			logo.y = stage.stageHeight * 0.5;

			fsbutton.x = stage.stageWidth - 20 - fsbutton.width;
			fsbutton.y = 20;
		}


		public function enterFrameHandler(event:Event):void
		{
			// stress the flashplayer a bit by rendering a rotating bitmap

			logo.rotationX += 0.14;
			logo.rotationY -= 0.15;
			logo.rotationZ += 0.16;
		}


		public function mouseHandler(event:MouseEvent):void
		{
			ttrace("Event." + String(event.type).toUpperCase() + " pos=" + event.stageX + "," + event.stageY + " time=" + Number(getTimer()/1000.0).toFixed(3) +" sec");

			if (event.type == "mouseDown")
			{
				// start tracing the MOUSE_MOVE events
				stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseHandler);
			}
			else if (event.type == "mouseUp")
			{
				// stop tracing
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseHandler);
			}
		}

	}
}