package {
	import com.bit101.components.Window;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import com.netgrind.core.*;
	
	/**
	 * ...
	 * @author netgrind
	 */
	public class ModalWindow extends Window {
		
		public var overlay:Sprite
		
		public function ModalWindow(s:Stage, title:String = '', closeOnBackClick:Boolean = false):void {
			overlay = UtilsArt.makeRectangle(s.width, s.height, 0xffffff, .5);
			s.addChild(overlay);
			var r:Rectangle = s.getBounds(s);
			overlay.x = r.x;
			overlay.y = r.y;
			super(s,0,0, title);
			addEventListener(Event.REMOVED_FROM_STAGE, cleanup);
			if (closeOnBackClick) {
				overlay.addEventListener(MouseEvent.CLICK, onClose);
			}
		}
				
		public function cleanup(e:Event = null):void {
			removeEventListener(Event.REMOVED_FROM_STAGE, cleanup);
			overlay.removeEventListener(MouseEvent.CLICK, onClose);
			Utils.removeChild(overlay);
		}
		
		override public function draw():void {
			super.draw();
			center();
		}
		
		public function center():void {
			if (!stage) return;
			this.x = stage.stageWidth * .5 - this.width * .5;
			this.y = stage.stageHeight * .5 - this.height * .5;
		}
		
	}
	
}