<?xml version="1.0" encoding="utf-8"?>
<s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" 
		 xmlns:s="library://ns.adobe.com/flex/spark" 
		 xmlns:mx="library://ns.adobe.com/flex/mx"
		 creationComplete="onCreationComplete(event)"
		 minWidth="300"
		 >
	<fx:Script>
		<![CDATA[
			import mx.core.FlexGlobals;
			import mx.core.IVisualElement;
			import mx.events.CloseEvent;
			import mx.events.FlexEvent;
			import mx.managers.PopUpManager;
			
			import spark.components.Image;
			
			public static const YES:uint = 0x01;
			public static const NO:uint = 0x10;
			public static const OK:uint = 0x02;
			public static const CANCEL:uint = 0x20;
			
			private static const STATE_OK:String = "ok";
			private static const STATE_OKCANCEL:String = "okCancel";
			private static const STATE_YESNO:String = "yesno";
			
			private var _buttonFlag:uint;
			public function set buttonFlag(value:uint):void {
				switch ( value & 0x0F ) {
					case OK:
						button1Label = "OK";
						break;
					case YES:
						button1Label = "Yes";
						break;
				}
				switch ( value & 0xF0 ) {
					case CANCEL:
						button2Label = "Cancel";
						break;
					case NO:
						button2Label = "No";
						break;
				}
				if ( value & OK ) {
					if ( value & CANCEL ) {
						currentState = STATE_OKCANCEL;
					} else {
						currentState = STATE_OK;
					}
				} else {
					currentState = STATE_YESNO;
				}
			}
			public function get buttonFlag():uint {
				return _buttonFlag;
			}
			
			private var _message:String;
			[Bindable]
			public function set message(value:String):void {
				this._message = value;
			}
			
			public function get message():String {
				return _message;
			}
			
			private var _iconClass:Class;
			public function set iconClass(value:Class):void {
				_iconClass = value;
			}
			public function get iconClass():Class {
				return _iconClass;
			}
			
			[Bindable]
			private var button1Label:String = "";
			[Bindable]
			private var button2Label:String = "";
			
			public static function show(msg:String, title:String = "", buttonFlag:uint = OK, closeHandler:Function = null, iconClass:Class = null):void {
				var mbox:MessageBox = new MessageBox();
				mbox.message = msg;
				mbox.title = title;
				mbox.buttonFlag = buttonFlag;
				if ( closeHandler != null ) {
					mbox.addEventListener(CloseEvent.CLOSE, closeHandler);
				}
				if ( iconClass != null ) {
					mbox.iconClass = iconClass;
				}
				
				var parent:Sprite = Sprite(FlexGlobals.topLevelApplication);
				PopUpManager.addPopUp(mbox, parent, true);
				PopUpManager.centerPopUp(mbox);
			}
			
			public static function ok(msg:String, title:String = "", closeHandler:Function = null, iconClass:Class = null):void {
				show(msg, title, OK);
			}
			public static function okCancel(msg:String, title:String = "", closeHandler:Function = null, iconClass:Class = null):void {
				show(msg, title, OK | CANCEL);
			}
			public static function yesNo(msg:String, title:String = "", closeHandler:Function = null, iconClass:Class = null):void {
				show(msg, title, YES | NO);
			}
			
			protected function button1_clickHandler(event:MouseEvent):void
			{
				removeMessageBox(0x0F & buttonFlag);
			}
			
			protected function button2_clickHandler(event:MouseEvent):void
			{
				removeMessageBox(0xF0 & buttonFlag);
			}
			
			protected function onCreationComplete(event:FlexEvent):void
			{
				if ( _iconClass != null ) {
					var img:Image = new Image();
					img.source = new _iconClass();
					messageAreaGroup.addElementAt(img, 0);
				}
				button1.setFocus();
				button1.drawFocus(true);
				button1.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			}
			
			override protected function keyDownHandler(event:KeyboardEvent):void
			{
				if (event.keyCode == Keyboard.ESCAPE)
				{
					if ( (buttonFlag & 0xF0) > 0 ) {
						removeMessageBox(0xF0 & buttonFlag);
					} else {
						removeMessageBox(0x0F & buttonFlag);
					}
				}
			}
			
			protected function removeMessageBox(pressedButton:uint):void {
				this.visible = false;
				
				var closeEvent:CloseEvent = new CloseEvent(CloseEvent.CLOSE);
				closeEvent.detail = pressedButton;
				dispatchEvent(closeEvent);
				
				PopUpManager.removePopUp(this);
			}
			
		]]>
	</fx:Script>
	
	<fx:Declarations>
		<!-- 非ビジュアルエレメント (サービス、値オブジェクトなど) をここに配置 -->
	</fx:Declarations>
	
	<s:states>
		<s:State name="ok" />
		<s:State name="okCancel" />
		<s:State name="yesno" />
	</s:states>
	
	<s:layout>
		<s:VerticalLayout gap="15" horizontalAlign="center" paddingBottom="10" paddingLeft="10"
						  paddingRight="10" paddingTop="10"/>
	</s:layout>
	
	<s:HGroup id="messageAreaGroup" verticalAlign="middle">
		<s:Scroller width="100%" maxHeight="600">
			<s:Group>
				<mx:Text id="messageTextArea" text="{message}" maxWidth="800" />
			</s:Group>
		</s:Scroller>
	</s:HGroup>
	
	<s:HGroup>
		<s:Button id="button1" label="{button1Label}" click="button1_clickHandler(event)" />
		<s:Button id="button2" label="{button2Label}" click="button2_clickHandler(event)" includeIn="okCancel,yesno" />
	</s:HGroup>
	
</s:Panel>

