package{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.geom.Point;
	
	
	public class spyd extends MovieClip{
	
	private var p:Point = new Point()
	
		public function spyd(){
			addEventListener(Event.ADDED_TO_STAGE, onadd);
		}
		
		private function onadd(event:Event){
			x = stage.stageWidth / 2;
			y = stage.stageHeight / 2;
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, updateMouse, false, 0, true);
			stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler, false, 0, true);
			stage.addEventListener(Event.ADDED, updateStack, false, 0, true);
			
		}
		private function updateStack(event:Event){
			stage.addChild(this);
		}
		private function mouseLeaveHandler(event:Event){
			visible = false;
			Mouse.show(); 
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseReturnHandler, false, 0, true);
		}
		private function mouseReturnHandler(event:Event){
			visible = true;
			Mouse.hide(); //in case of right click
			removeEventListener(MouseEvent.MOUSE_MOVE, mouseReturnHandler);
		}
		private function updateMouse(event:MouseEvent){
			x = stage.mouseX;
			y = stage.mouseY;
			
			var vinkel:Number = Math.atan2(y - p.y, x - p.x) * 180 / Math.PI;
			if (vinkel - rotation > 180) 
					vinkel -= 360;
			else if (rotation - vinkel > 180)
					vinkel += 360;
			
			rotation -= (rotation - vinkel) / 10;
			
			p.x = x;
			p.y = y;
			
			event.updateAfterEvent();
		}
		
	}//END class
}//END package