@if exp="typeof(global.system_object) == 'undefined'"
@iscript

/*--- ウィンドウ上部にあるシステムメニュー部分 ---*/
class MenuWindowLayer extends Layer {
	var Buttons = [];
	var p = 'image/menu/';

	function MenuWindowLayer(win, par){
		super.Layer(...);

		loadImages(p+'bar');
		setSizeToImageSize();
		absolute = 100001;
		opacity = 0;
		hitThreshold = 256;
		createButtons();
	}

	function finalize(){
		for(var i=0; i<Buttons.count; i++) invalidate Buttons[i];
		super.finalize();
	}

	function createButtons(){
		Buttons[0] = new CustomButtonLayer(window, this, function(){if(kag.inStable)kag.callExtraConductor('saveload.ks','*save');});
		with(Buttons[0]){
			.setPos(90, 7);
			.loadImages(p+'save0', p+'save1', p+'save2', p+'save3');
			.hitThreshold = 0;
			.hint = "";
		}

		Buttons[1] = new CustomButtonLayer(window, this, function(){if(kag.inStable)kag.callExtraConductor('saveload.ks','*load');});
		with(Buttons[1]){
			.setPos(90+150, 7);
			.loadImages(p+'load0', p+'load1', p+'load2', p+'load3');
			.hitThreshold = 0;
			.hint = "";
		}

		Buttons[2] = new CustomButtonLayer(window, this, function(){kag.fullScreen=sf.Screen=!kag.fullScreen;});
		with(Buttons[2]){
			.setPos(90+150*3, 7);
			.loadImages(p+'screen0', p+'screen1', p+'screen2', p+'screen3');
			.hitThreshold = 0;
			.hint = "";
		}

		Buttons[3] = new CustomButtonLayer(window, this, function(){if(kag.inStable)kag.callExtraConductor('config.ks','*config');});
		with(Buttons[3]){
			.setPos(90+150*2, 7);
			.loadImages(p+'config0', p+'config1', p+'config2', p+'config3');
			.hitThreshold = 0;
			.hint = "";
		}

		Buttons[4] = new CustomButtonLayer(window, this, onReset);
		with(Buttons[4]){
			.setPos(90+150*4, 7);
			.loadImages(p+'reset0', p+'reset1', p+'reset2', p+'reset3');
			.hitThreshold = 0;
			.hint = "";
		}

		Buttons[5] = new CustomButtonLayer(window, this, function(){if(askYesNo("ゲームを終了しますか？"))kag.process('saveload.ks','*exit');});
		with(Buttons[5]){
			.setPos(90+150*5, 7);
			.loadImages(p+'exit0', p+'exit1', p+'exit2', p+'exit3');
			.hitThreshold = 0;
			.hint = "";
		}
	}

	function onReset(){
		if(kag.inStable){
			if(tf.label!='' && askYesNo("回想選択に戻りますか？"))
					kag.clearMessageLayers(false), kag.processCall('rpmode.ks','*rpreturn');
			else if(tf.label=='' && askYesNo("タイトルに戻りますか？")) kag.process('saveload.ks','*reset');
		}
		/*
		f.onZoom = 0;	//2010/01/05追加
		f.Zoom = 0;*/

		f.onSection = 0;	//2010/01/05追加*/
		f.section = 0;

		f.onCutinStr = 0;
		f.cutinstr = 0;
		
	}
}

/*--- ウィンドウ下部のメッセージウィンドウメニュー部分  ---*/
class MessageWindowLayer extends Layer {
	var Buttons = [];
	var Window = void;
	var Name = void;
	var Message = void;
	var timer = void;
	var DateW = void;
	var State;
	var p = 'image/window/';

	function MessageWindowLayer(win, par){
		super.Layer(...);

		setPos(-4, 551, 1024, 224);
		absolute = 100000-1;
		hitThreshold = 256;
		createButtons();

		Window = new global.Layer(window, this);
		with(Window){
			.loadImages(p+'messagewindow');
			.setSizeToImageSize();
			.absolute = absolute;
			.opacity = 255 * sf.WinAlpha \ 100;
			.hitThreshold = 256;
			.visible = true;
		}

		Name = new global.Layer(window, this);
		with(Name){
			.setPos(135, 42, 400, 50);
			.absolute = absolute + 2;
			.hitThreshold = 256;
			.font.height = 28;
			.font.face = kag.chDefaultFace;
			.font.bold = sf.chDefaultBold;
			.visible = true;
		}


		Message = new global.Layer(window, this);
		with(Message){
			.loadImages(p+'SystemMessage1');
			.setSizeToImageSize();
			.setPos(0, 0);
			.absolute = absolute * 2;
			.opacity = 0;
			.hitThreshold = 256;
			.visible = true;
		}


//	if(sf.Date!=void){
		DateW = new global.Layer(window, this);
		with(DateW){
			.loadImages(p+'date1');
			.setSizeToImageSize();
			.setPos(0, 0);
			.absolute = absolute + 2;
			.hitThreshold = 256;
			.opacity =256;
			.visible = false;
//		}
	}


		timer = new Timer(onTimer, "");
		timer.interval = 5;
		timer.enabled = false;
	}

	function finalize(){
		for(var i=0; i<Buttons.count; i++) invalidate Buttons[i];
		invalidate Window;
		invalidate Name;
		invalidate Message;
		invalidate DateW;
		super.finalize();
	}

	function onTimer(){
		with(Message){
			if(State) .opacity += 1, .left = Math.pow(.opacity,0.65)+100;
			else .opacity -= 1;

			if(.opacity >= 256){
				.opacity = 256;
				State = false;
			}
			else if(.opacity <= 0){
				.opacity = 0;
				timer.enabled = false;
			}
		}
	}
	
	//==========================================================================
	//---ゲーム系で使用する関数
	//メッセージウィンドウのボタンの使用状態を変更する
	function buttonHitStatus(i, flag){
		if(flag == true){
			Buttons[i].hitThreshold = 0;
			Buttons[i].joinFocusChain = true;
			Buttons[i].opacity = 256;
		}else if(flag == 2){
			Buttons[i].hitThreshold = 256;
			Buttons[i].joinFocusChain = false;
			Buttons[i].opacity = 0;
		}else{
			Buttons[i].hitThreshold = 256;
			Buttons[i].opacity = 160;
		}
	}
	//==========================================================================

	function nameDisplay(name){
		with(Name){
			.fillRect(0, 0, .width, .height, 0x00000000);
			.drawText(0, 0, name, 0xFFFFFF, , , 255, 0x000000, 0, 2, 2);
		}
	}

	function nameErase(){
		with(Name){
			.fillRect(0, 0, .width, .height, 0x00000000);
		}
	}

	function dateDisplay(date){
			with(DateW){
				.loadImages(p+'date'+date);
				.visible = true;
		}
	}

	function dateErase(){
			with(DateW){
				.visible = false;
		}
	}

	function Button4Display(){
		if (f.Cutin==0){
			with(Buttons[4]){
				.loadImages(p+'cutin4', p+'cutin1', p+'cutin2', p+'cutin3');
			}
			kag.fore.layers[7].visible = false;
			kag.back.layers[7].visible = false;

		} else {
			with(Buttons[4]){
				.loadImages(p+'cutin0', p+'cutin1', p+'cutin2', p+'cutin3');
			}
			

			if (f.onCutin){
			kag.fore.layers[7].visible = true;
			kag.back.layers[7].visible = true;
			}
		}
			Buttons[4].update;
	}

	function Button4Erase(){
			with(Buttons[4]){
				.loadImages(p+'cutin4', p+'cutin1', p+'cutin2', p+'cutin3');
				}
			kag.fore.layers[7].visible = false;
			kag.back.layers[7].visible = false;
			Buttons[4].update;
	}

	/*function ZoomButtonDisplay(){
		if (f.Zoom==0){
			with(Buttons[7]){
				.loadImages(p+'zoom4', p+'zoom4', p+'zoom4', p+'zoom4');
				.hitThreshold = 256;
			}
			kag.fore.layers[7].visible = false;
			kag.back.layers[7].visible = false;
			f.onZoom=0;

		} else {
			with(Buttons[7]){
				.loadImages(p+'zoom0', p+'zoom1', p+'zoom2', p+'zoom3');
				.hitThreshold = 0;
			}

			if (f.onZoom){
			kag.fore.layers[7].visible = true;
			kag.back.layers[7].visible = true;
			}
		}
			Buttons[7].update;
	}*/

	/*function ZoomButtonErase(){
			with(Buttons[7]){
				.loadImages(p+'zoom4', p+'zoom1', p+'zoom2', p+'zoom3');
				}
			kag.fore.layers[7].visible = false;
			kag.back.layers[7].visible = false;
			Buttons[7].update;
	}*/

	/*断面図カットインボタンのＯＮ・ＯＦＦ表示更新*/
	function SectionButtonDisplay(){
		if (f.section==false){
			with(Buttons[7]){
				.loadImages(p+'danmen3', p+'danmen3', p+'danmen3', p+'danmen3');
				.hitThreshold = 256;
			}
			kag.fore.layers[6].visible = false;
			kag.back.layers[6].visible = false;
			//f.onSection=0;

		} else {
			with(Buttons[7]){
				.loadImages(p+'danmen0', p+'danmen1', p+'danmen2', p+'danmen3');
				.hitThreshold = 0;
			}

			if (f.onSection){
				Buttons[7].loadImages(p+'danmen4', p+'danmen3', p+'danmen3', p+'danmen3');
				kag.fore.layers[6].visible = true;
				kag.back.layers[6].visible = true;
			}
		}
		Buttons[7].update;
	}

	function SectionButtonErase(){
		with(Buttons[7]){
			.loadImages(p+'danmen3', p+'danmen1', p+'danmen2', p+'danmen3');
			.hitThreshold = 256;
		}

		kag.fore.layers[6].visible = false;
		kag.back.layers[6].visible = false;
		Buttons[7].update;
	}

	/*文字カットインボタンのＯＮ・ＯＦＦ表示更新*/
	function CutinStrButtonDisplay(){
		if (f.cutinstr==false){
			with(Buttons[8]){
				.loadImages(p+'koka3', p+'koka3', p+'koka3', p+'koka3');
				.hitThreshold = 256;
			}
			kag.fore.layers[5].visible = false;
			kag.back.layers[5].visible = false;
			
			//f.onCutinStr=0;

		} else {
			with(Buttons[8]){
				.loadImages(p+'koka0', p+'koka1', p+'koka2', p+'koka3');
				.hitThreshold = 0;
			}

			if (f.onCutinStr){
				Buttons[8].loadImages(p+'koka4', p+'koka1', p+'koka2', p+'koka3');
				kag.fore.layers[5].visible = true;
				kag.back.layers[5].visible = true;
			}
		}
		Buttons[8].update;
	}

	function CutinStrButtonErase(){
		with(Buttons[8]){
			.loadImages(p+'koka3', p+'koka1', p+'koka2', p+'koka3');
			.hitThreshold = 256;
		}

		kag.fore.layers[5].visible = false;
		kag.back.layers[5].visible = false;
		Buttons[8].update;
	}

	/*メッセージウィンドウメニューボタンを生成*/
	function createButtons(){
		Buttons[0] = new CustomButtonLayer(window, this, onQuickSave);
		with(Buttons[0]){
			.setPos(500, 0);
			.absolute = absolute + 2;
			.loadImages(p+'qsave0', p+'qsave1', p+'qsave2', p+'qsave3');
			.hitThreshold = 0;
			.hint = "";
		}

		Buttons[1] = new CustomButtonLayer(window, this, onQuickLoad);
		with(Buttons[1]){
			.setPos(515+80, 0);
			.absolute = absolute + 2;
			.loadImages(p+'qload0', p+'qload1', p+'qload2', p+'qload3');
			.hitThreshold = 0;
			.hint = "";
		}

		Buttons[2] = new CustomButtonLayer(window, this, onSkip);
		with(Buttons[2]){
			.setPos(523+80*2, 0);
			.absolute = absolute + 2;
			.loadImages(p+'skip0', p+'skip1', p+'skip2', p+'skip3');
			.hitThreshold = 0;
			.hint = "";
		}

		Buttons[3] = new CustomButtonLayer(window, this, onAuto);
		with(Buttons[3]){
			.setPos(515+80*3, 0);
			.absolute = absolute + 2;
			.loadImages(p+'auto0', p+'auto1', p+'auto2', p+'auto3');
			.hitThreshold = 0;
			.hint = "";
		}

		Buttons[4] = new CustomButtonLayer(window, this, function(){if(kag.inStable)kag.showHistory();});
		with(Buttons[4]){
			.setPos(510+80*4, 0);
			.absolute = absolute + 2;
			.loadImages(p+'backlog0', p+'backlog1', p+'backlog2', p+'backlog3');
			.hitThreshold = 0;
			.hint = "";
		}


		Buttons[5] = new CustomButtonLayer(window, this, function(){if(f.VoicePlay!="")kag.se[f.VoiceBuff].play(%[storage:f.VoicePlay]);});
		with(Buttons[5]){
			.setPos(500+80*5, 0);
			.absolute = absolute + 2;
			.loadImages(p+'voice0', p+'voice1', p+'voice2', p+'voice3');
			.hitThreshold = 0;
			.hint = "";
		}

		Buttons[6] = new CustomButtonLayer(window, this, function(){if(kag.inStable)kag.hideMessageLayerByUser();});
		with(Buttons[6]){
			.setPos(425+80*7, 0);
			.absolute = absolute + 2;
			.loadImages(p+'close0', p+'close1', p+'close2', p+'close3');
			.hitThreshold = 0;
			.hint = "";
		}
/*
		Buttons[7] = new CustomButtonLayer(window, this, onZoom);
		with(Buttons[7]){
			.setPos(500+80*6, 0);
			.absolute = absolute + 2;
			.loadImages(p+'zoom0', p+'zoom1', p+'zoom2', p+'zoom3');
			.hitThreshold = 0;
			.hint = "";
		}
*/
		Buttons[7] = new CustomButtonLayer(window, this, onSection);
		with(Buttons[7]){
			.setPos(32+123*6, 300);
			.absolute = absolute + 2;
			.loadImages(p+'danmen3', p+'danmen1', p+'danmen2', p+'danmen3');
			.hitThreshold = 0;
			.hint = "";
		}

		Buttons[8] = new CustomButtonLayer(window, this, onCutinStr);
		with(Buttons[8]){
			.setPos(32+123*7, 300);
			.absolute = absolute + 2;
			.loadImages(p+'koka3', p+'koka1', p+'koka2', p+'koka3');
			.hitThreshold = 0;
			.hint = "";
		}
	}

	function onCutin(){
		f.Cutin = f.Cutin^1;
		kag.CutinButton();
	}

	function onSkip(){
		if(kag.skipMode==3) kag.cancelSkip();
		else if(kag.autoMode) kag.cancelAutoMode();
		else kag.skipToStop();
	}

	//断面図カットイン表示　ＯＮ・ＯＦＦ切り替え処理
	function onSection(){
		if(f.section){
			if(f.onSection){
				Buttons[7].loadImages(p+'danmen0', p+'danmen1', p+'danmen2', p+'danmen3');
				kag.fore.layers[6].freeImage();
				kag.back.layers[6].freeImage();
				f.onSection = false; //f.onSection = 0;
			}else{
				Buttons[7].loadImages(p+'danmen4', p+'danmen1', p+'danmen2', p+'danmen3');
				if(Storages.isExistentStorage(f.CGfile+'_sec.png')){
					kag.tagHandlers.image(%[storage:f.CGfile+'_sec', layer:"6", page:"fore", visible:"true", opacity:"255"]);
					kag.tagHandlers.image(%[storage:f.CGfile+'_sec', layer:"6", page:"back", visible:"true", opacity:"255"]);
				}else{
					kag.tagHandlers.image(%[storage:'void', layer:"6", page:"fore", visible:"true"]);
					kag.tagHandlers.image(%[storage:'void', layer:"6", page:"back", visible:"true"]);
					Debug.message("画像を読み込むことができません。"); 
				}

				f.onSection = true; //f.onSection = 1;
			}
		}
	}

	//文字カットイン表示　ＯＮ・ＯＦＦ切り替え処理
	function onCutinStr(){
		if(f.cutinstr){
			if(f.onCutinStr){
				Buttons[8].loadImages(p+'koka0', p+'koka1', p+'koka2', p+'koka3');
				kag.fore.layers[5].freeImage();
				kag.back.layers[5].freeImage();
				f.onCutinStr = false; //f.onCutinStr = 0;
			}else{
				Buttons[8].loadImages(p+'koka4', p+'koka1', p+'koka2', p+'koka3');
				if(Storages.isExistentStorage(f.CGfile+'_cut.png')){
					kag.tagHandlers.image(%[storage:f.CGfile+'_cut', layer:"5", page:"fore", visible:"true"]);
					kag.tagHandlers.image(%[storage:f.CGfile+'_cut', layer:"5", page:"back", visible:"true"]);
				}else{
					kag.tagHandlers.image(%[storage:'void', layer:"5", page:"fore", visible:"true"]);
					kag.tagHandlers.image(%[storage:'void', layer:"5", page:"back", visible:"true"]);
					Debug.message("画像を読み込むことができません。"); 
				}

				f.onCutinStr = true; //f.onCutinStr = 1;
			}
		}else{
			Buttons[8].loadImages(p+'koka0', p+'koka1', p+'koka2', p+'koka3');
		}
	}

	function onAuto(){
		if(kag.autoMode) kag.cancelAutoMode();
		else if(kag.skipMode==3) kag.cancelSkip();
		else kag.enterAutoMode();
	}

	function onQuickSave(){
//		if(!kag.inStable || (Storages.isExistentStorage(kag.getBookMarkFileNameAtNum(100))
//			&& !askYesNo("クイックセーブしますか？"))) return;
		kag.saveBookMark(100);
		SystemMessage(1);
	}

	function onQuickLoad(){
		if(!kag.inStable || !Storages.isExistentStorage(kag.getBookMarkFileNameAtNum(100))
			|| !askYesNo("クイックロードしますか？")) return;
		tf.i = 2, tf.num = 100;
		kag.process('saveload.ks', '*loaded');

	}

	function SystemMessage(i){
		with(Message){
			.loadImages(p+'SystemMessage'+i);
			.left = 100;
			.opacity = 0;
		}
		State = true;
		timer.enabled = true;
	}
	
	/* ウィンドウを表示 */
	function MessageWindowVisibleOn(){
		Window.visible = true;
		Name.visible = true;
		Message.visible = true;
		for(var i = 0; i < Buttons.count; i++){
			Buttons[i].hitThreshold = 0;
			Buttons[i].visible = true;
		}
	}

	/* ウィンドウを非表示 */
	function MessageWindowVisibleOff(){
		Window.visible = false;
		Name.visible = false;
		Message.visible = false;
		for(var i = 0; i < Buttons.count; i++){
			Buttons[i].visible = false;
			Buttons[i].hitThreshold = 256;
		}
	}
}

/*--- 画像を用いたセレクト命令(select2）使用時に呼び出される選択ウィンドウ ---*/
class Select2WindowLayer extends Layer {
	var Buttons = [];
	var Texts = [];
	var s;
	var n;
	var p = 'image/window/';

	function Select2WindowLayer(win, par){
		super.Layer(...);

		loadImages(p+'selecta');
		setSizeToImageSize();
		with(window.primaryLayer)
			setPos((.width-width) \ 2, (.height-height) \ 2 - 60);
		absolute = 100000+2;
		hitThreshold = 256;
		createButtons();
	}

	function finalize(){
		for(var i=0; i<Texts.count; i++) invalidate Buttons[i];
		for(var i=0; i<Texts.count; i++) invalidate Texts[i];
		super.finalize();
	}

	function createButtons(){
		for(var i=0; i<6; i++){
			Buttons[i] = new CustomButtonLayer(window, this, onSelect);
			with(Buttons[i]){
				.loadImages(p+'selectbar0a', p+'selectbar1a', p+'selectbar2a', p+'selectbar3a');
				.left = (width/3-.width)/6+(width/3*i);
				.top = (width-.width) \ 2;
				.hitThreshold = 0;
				.name = i;
				.visible = false;
			}
			Texts[i] = new global.Layer(window, this);
			with(Texts[i]){
				.setPos(Buttons[i].left, 0, Buttons[i].width, Buttons[i].height);
				.hitThreshold = 256;
				.font.height = 24;
				.visible = false;
			}
		}
	}

	function onSelect(obj){
		if(kag.inStable){
			for(var i=0; i<n; i++) Buttons[i].visible = false;
			system_object.selectErase();
			var i = +obj.name;
			kag.process(s[i][2]!='' ? s[i][2]+'.ks' : "", '*'+s[i][1]);
		}
	}

	function Display(s1, s2="", s3="", s4="", s5="", s6=""){							///
		s = [s1.split('\/'), s2.split('\/'), s3.split('\/'), s4.split('\/'), s5.split('\/'), s6.split('\/')];		///
		n = 0;

		for(var i=0; i<6; i++){
			if(s[i][0]!=""){
				with(Texts[i]){
					.fillRect(0, 0, .width, .height, 0x00000000);
					.drawText(0, (.height-.font.height) \ 2, s[i][0], 0xFFFFFF, , , 255, 0x000000, 0, 2, 2);
				}
				n++;
			} else break;
		}

		for(var i=0; i<n; i++){
				with(Texts[i]){
					.top = 0;
					.left = 0+(width/6-.width)/2+(width/6*i);
					.visible = true;
				}
				with(Buttons[i]){
					.top = Texts[i].top;
					.visible = true;
				}
		}

		for(var i=n; i<6; i++){
				with(Texts[i]){
					.visible = false;
				}
				with(Buttons[i]){
					.visible = false;
				}
		}


	}
	
	
}

/*文章を用いたセレクト命令(select)で使用時に呼び出される選択ウィンドウ*/
class SelectWindowLayer extends Layer {
	var Buttons = [];
	var Texts = [];
	var s;
	var n;
	var p = 'image/window/';

	function SelectWindowLayer(win, par){
		super.Layer(...);

		loadImages(p+'select');
		setSizeToImageSize();
		with(window.primaryLayer)
			setPos((.width-width) \ 2, (.height-height) \ 2);
		absolute = 100000+2;
		hitThreshold = 256;
		createButtons();
	}

	function finalize(){
		for(var i=0; i<Texts.count; i++) invalidate Buttons[i];
		for(var i=0; i<Texts.count; i++) invalidate Texts[i];
		super.finalize();
	}

	function createButtons(){
		for(var i=0; i<5; i++){
			Buttons[i] = new CustomButtonLayer(window, this, onSelect);
			with(Buttons[i]){
				.loadImages(p+'selectbar0', p+'selectbar1', p+'selectbar2', p+'selectbar3');
				.left = (width-.width) \ 2;
				.hitThreshold = 0;
				.name = i;
				.visible = false;
			}
			Texts[i] = new global.Layer(window, this);
			with(Texts[i]){
				.setPos(Buttons[i].left, 0, Buttons[i].width, Buttons[i].height);
				.hitThreshold = 256;
				.font.height = 24;
				.visible = false;
			}
		}
	}

	function onSelect(obj){
		if(kag.inStable){
			for(var i=0; i<n; i++) Buttons[i].visible = false;
			system_object.selectErase();
			var i = +obj.name;
			kag.process(s[i][2]!='' ? s[i][2]+'.ks' : "", '*'+s[i][1]);
		}
	}

	function Display(s1, s2="", s3="", s4="", s5=""){
		s = [s1.split('\/'), s2.split('\/'), s3.split('\/'), s4.split('\/'), s5.split('\/')];
		n = 0;

		for(var i=0; i<5; i++){
			if(s[i][0]!=""){
				with(Texts[i]){
					.fillRect(0, 0, .width, .height, 0x00000000);
					.drawText((.width-.font.getTextWidth(s[i][0])) \ 2, (.height-.font.height) \ 2, s[i][0], 0xFFFFFF, , , 255, 0x000000, 0, 2, 2);
				}
				n++;
			} else break;
		}

		for(var i=0; i<n; i++){
				with(Texts[i]){
					.top = 0+(height/n-.height)/2+(height/n*i);
					.visible = true;
				}
				with(Buttons[i]){
					.top = Texts[i].top;
					.visible = true;
				}
		}

		for(var i=n; i<5; i++){
				with(Texts[i]){
					.visible = false;
				}
				with(Buttons[i]){
					.visible = false;
				}
		}



	}
}

/*システムメニュー･メッセージウィンドウメニュー・セレクトウィンドウ１･セレクトウィンドウ２をまとめて管理*/
class SystemButtonPlugin extends KAGPlugin {
	var foreSeen = false, backSeen = false;
	var foreButtons = void, backButtons = void;
	var foreSeen2 = false, backSeen2 = false;
	var foreButtons2 = void, backButtons2 = void;
	var foreSeen3 = false, backSeen3 = false;
	var foreButtons3 = void, backButtons3 = void;
	var foreSeen4 = false, backSeen4 = false;
	var foreButtons4 = void, backButtons4 = void;

	function SystemButtonPlugin(){
		super.KAGPlugin();
		foreButtons = new MessageWindowLayer(kag, kag.fore.base);
		backButtons = new MessageWindowLayer(kag, kag.back.base);
		foreButtons2 = new MenuWindowLayer(kag, kag.fore.base);
		backButtons2 = new MenuWindowLayer(kag, kag.back.base);
		foreButtons3 = new SelectWindowLayer(kag, kag.fore.base);
		backButtons3 = new SelectWindowLayer(kag, kag.back.base);
		foreButtons4 = new Select2WindowLayer(kag, kag.fore.base);
		backButtons4 = new Select2WindowLayer(kag, kag.back.base);
	}

	function dateDisplay(date){
		foreButtons.dateDisplay(date);
		backButtons.dateDisplay(date);
	}

	function dateErase(){
		foreButtons.dateErase();
		backButtons.dateErase();
	}

	function finalize(){
		invalidate foreButtons;
		invalidate backButtons;
		invalidate foreButtons2;
		invalidate backButtons2;
		invalidate foreButtons3;
		invalidate backButtons3;
		invalidate foreButtons4;
		invalidate backButtons4;
		super.finalize();
	}
	
	//ゲーム系にて使用==========================================================
	//---メッセージウィンドウの不必要な機能を一時的に使用不可にする
	function messButton_Hitstatus(i, flag){
		foreButtons.buttonHitStatus(i, flag);
		backButtons.buttonHitStatus(i, flag);
	}

	function messButton_HitstatusOff(){
		messButton_Hitstatus(0, false);
		messButton_Hitstatus(1, false);
	}

	function messButton_HitstatusOn(){
		messButton_Hitstatus(0, true);
		messButton_Hitstatus(1, true);
	}

	//---　メッセージウィンドウメニューを非表示
	function messButton_HitStatusOffALL(){
		messButton_Hitstatus(0, 2);
		messButton_Hitstatus(1, 2);
		messButton_Hitstatus(2, 2);
		messButton_Hitstatus(3, 2);
		messButton_Hitstatus(4, 2);
		messButton_Hitstatus(5, 2);
		messButton_Hitstatus(6, 2);
		messButton_Hitstatus(7, 2);
		messButton_Hitstatus(8, 2);
	}

	//---　メッセージウィンドウメニューを非表示
	function messButton_HitStatusOnALL(){
		messButton_Hitstatus(0, true);
		messButton_Hitstatus(1, true);
		messButton_Hitstatus(2, true);
		messButton_Hitstatus(3, true);
		messButton_Hitstatus(4, true);
		messButton_Hitstatus(5, true);
		messButton_Hitstatus(6, true);
		messButton_Hitstatus(7, true);
		messButton_Hitstatus(8, true);
	}

	function chengeMessWindow(flag){
		foreButtons.chengeMessWindow(flag);
		backButtons.chengeMessWindow(flag);
	}

	function messWindow_visible(flag){
		foreButtons.visible = flag;
		backButtons.visible = flag;
	}
	//==========================================================================

	function nameDisplay(name){
		foreButtons.nameDisplay(name);
		backButtons.nameDisplay(name);
	}

	function nameErase(){
		foreButtons.nameErase();
		backButtons.nameErase();
	}

	function SystemMessage(i){
		foreButtons.SystemMessage(i);
		backButtons.SystemMessage(i);
	}

	function selectErase(){
		foreButtons3.visible = foreSeen3 = false;
		backButtons3.visible = backSeen3 = false;
		foreButtons4.visible = foreSeen4 = false;
		backButtons4.visible = backSeen4 = false;
	}

	function enabled1(i, flag){
		foreButtons.Buttons[i].enabled = flag;
		backButtons.Buttons[i].enabled = flag;
	}

	function enabled2(i, flag){
		foreButtons2.Buttons[i].enabled = flag;
		backButtons2.Buttons[i].enabled = flag;
	}

	/*function onZoomButtonDisplay(){
		foreButtons.ZoomButtonDisplay();
		backButtons.ZoomButtonDisplay();
	}*/

	/*システムボタン内に断面図カットインボタンの表示を登録*/
	function onSectionButtonDisplay(){
		foreButtons.SectionButtonDisplay();
		backButtons.SectionButtonDisplay();
	}

	function sectionButtonErase(){
		foreButtons.SectionButtonErase();
		backButtons.SectionButtonErase();
	}

	/*システムボタン内に文字カットインボタンの表示を登録*/
	function onCutinStrButtonDisplay(){
		foreButtons.CutinStrButtonDisplay();
		backButtons.CutinStrButtonDisplay();
	}

	function cutinStrButtonErase(){
		foreButtons.CutinStrButtonErase();
		backButtons.CutinStrButtonErase();
	}

	function onCutinButtonDisplay(){
		foreButtons.Button4Display();
		backButtons.Button4Display();
	}

	function CutinDisplay(){
		onCutinButtonDisplay();
	}

	function CutinErase(){
		foreButtons.Button4Erase();
		backButtons.Button4Erase();
	}


	function mode(flag){
		if(tf.label == ''){
			enabled1(0, flag);
			enabled1(1, flag);
			enabled2(0, flag);
			enabled2(1, flag);
		}
		enabled1(4, flag);
		enabled1(6, flag);
		enabled2(3, flag);
		enabled2(4, flag);
	}

	function visible(flag){
		foreButtons.visible = foreSeen = flag;
		backButtons.visible = backSeen = flag;
		foreButtons2.visible = foreSeen2 = flag;
		backButtons2.visible = backSeen2 = flag;
	}

	function select(s1, s2, s3, s4, s5){
		foreButtons3.visible = foreSeen3 = true;
		backButtons3.visible = backSeen3 = true;
		foreButtons3.Display(s1, s2, s3, s4, s5);
		backButtons3.Display(s1, s2, s3, s4, s5);
	}

	function select2(s1, s2, s3, s4, s5, s6){
		foreButtons4.visible = foreSeen4 = true;
		backButtons4.visible = backSeen4 = true;
		foreButtons4.Display(s1, s2, s3, s4, s5, s6);
		backButtons4.Display(s1, s2, s3, s4, s5, s6);
	}

	function onStore(f, elm){
		f.systemButtons = %[];
		with(f.systemButtons){
			.foreVisible = foreSeen;
			.backVisible = backSeen;
			.foreVisible2 = foreSeen2;
			.backVisible2 = backSeen2;
			.foreVisible3 = foreSeen3;
			.backVisible3 = backSeen3;
			.foreVisible4 = foreSeen4;
			.backVisible4 = backSeen4;
		}
	}

	function onRestore(f, clear, elm){
		nameErase();
		if(f.systemButtons === void){
			foreButtons.visible = foreSeen = false;
			backButtons.visible = backSeen = false;
			foreButtons2.visible = foreSeen2 = false;
			backButtons2.visible = backSeen2 = false;
			foreButtons3.visible = foreSeen3 = false;
			backButtons3.visible = backSeen3 = false;
			foreButtons4.visible = foreSeen4 = false;
			backButtons4.visible = backSeen4 = false;
		}
		else {
			with(f.systemButtons){
				foreButtons.visible = foreSeen = .foreVisible;
				backButtons.visible = backSeen = .backVisible;
				foreButtons2.visible = foreSeen2 = .foreVisible2;
				backButtons2.visible = backSeen2 = .backVisible2;
				foreButtons3.visible = foreSeen3 = .foreVisible3;
				backButtons3.visible = backSeen3 = .backVisible3;
				foreButtons4.visible = foreSeen4 = .foreVisible4;
				backButtons4.visible = backSeen4 = .backVisible4;
			}
			if(tf.i) SystemMessage(tf.i);
		}
	}

	function onStableStateChanged(stable){
//		foreButtons.enabled = stable;
//		backButtons.enabled = stable;
	}

	function hidefore(){
		foreButtons.visible = false;
		foreButtons2.visible = false;
		foreButtons3.visible = false;
		foreButtons4.visible = false;
	}

	function hideback(){
		backButtons.visible = false;
		backButtons2.visible = false;
		backButtons3.visible = false;
		backButtons4.visible = false;
	}

	function onMessageHiddenStateChanged(hidden){
		if(hidden)
			hidefore(), hideback();
		else {
			foreButtons.visible = foreSeen;
			backButtons.visible = backSeen;
			foreButtons2.visible = foreSeen2;
			backButtons2.visible = backSeen2;
			foreButtons3.visible = foreSeen3;
			backButtons3.visible = backSeen3;
			foreButtons4.visible = foreSeen4;
			backButtons4.visible = backSeen4;
		}
	}

	function onCopyLayer(toback){
		if(toback){
			backButtons.visible = foreButtons.visible;
			backSeen = foreSeen;
			backButtons2.visible = foreButtons2.visible;
			backSeen2 = foreSeen2;
			backButtons3.visible = foreButtons3.visible;
			backSeen3 = foreSeen3;
			backButtons4.visible = foreButtons4.visible;
			backSeen4 = foreSeen4;
		}
		else {
			foreButtons.visible = backButtons.visible;
			foreSeen = backSeen;
			foreButtons2.visible = backButtons2.visible;
			foreSeen2 = backSeen2;
			foreButtons3.visible = backButtons3.visible;
			foreSeen3 = backSeen3;
			foreButtons4.visible = backButtons4.visible;
			foreSeen4 = backSeen4;
		}
	}

	function onExchangeForeBack(){
		foreButtons <-> backButtons;
		foreSeen <-> backSeen;
		foreButtons2 <-> backButtons2;
		foreSeen2 <-> backSeen2;
		foreButtons3 <-> backButtons3;
		foreSeen3 <-> backSeen3;
		foreButtons4 <-> backButtons4;
		foreSeen4 <-> backSeen4;
	}
}

kag.addPlugin(global.system_object = new SystemButtonPlugin(kag));

//クローズアップモードの処理を実行している部分
kag.onMouseMove = function(x, y){
	y *= Math.max(800/kag.innerWidth, 600/kag.innerHeight);
	if(!sf.optionhold){
		with(system_object){
			if(0<y && y<30 && !kag.historyLayer.visible)
				.foreButtons2.opacity = .backButtons2.opacity = 256;
			else
				.foreButtons2.opacity = .backButtons2.opacity = 0;
		}
	}
/*
	if(f.onZoom==1){
//		f.ix = (x-133) * -3;
//		f.iy = (y-100) * -3;
		if(f.ix > 0) f.ix=0;
		if(f.ix < -0) f.ix=-0;
		if(f.iy > 0) f.iy=0;
		if(f.iy < -0) f.iy=-0;
		kag.tagHandlers.image(%[storage:f.CGfile+'zoom', layer:"7", page:"fore", visible:"true", left:f.ix , top:f.iy]);
		kag.tagHandlers.image(%[storage:f.CGfile+'zoom', layer:"7", page:"back", visible:"true", left:f.ix , top:f.iy]);
	}*/
}incontextof kag;

kag.cancelSkip = function(){
	if(skipMode == 4) skipKeyRepressed = false;
	skipMode = 0;
	actualChSpeed = chSpeed;
	if(!autoMode) system_object.mode(true);
}incontextof kag;

kag.skipToStop = function(){
	onPrimaryClick();
	skipMode = 3;
	actualChSpeed = 1;
	system_object.mode(false);
}incontextof kag;


kag.CutinButton = function(){
	system_object.onCutinButtonDisplay();
}incontextof kag;

/*
kag.ZoomButton = function(){
	system_object.onZoomButtonDisplay();
}incontextof kag;*/

kag.SectionButton = function(){
	system_object.onSectionButtonDisplay();
}incontextof kag;

kag.CutinStrButton = function(){
	system_object.onCutinStrButtonDisplay();
}incontextof kag;

kag.enterAutoMode = function(){
	if(inStable) onPrimaryClick();
	autoMode = true;
	system_object.mode(false);
}incontextof kag;

kag.cancelAutoMode = function(){
	if(autoMode){
		autoMode = false;
		var t = conductor.lastTagName;
		if(t == 'l') showLineBreak(%[canskip: false]);
		else if(t == 'p') showPageBreak();
		else if(t == 'r') showPageBreakAndClear();
		system_object.mode(true);
	}
}incontextof kag;


@endscript
@endif

@macro name=sysbtn
@eval exp="system_object.visible(mp.visible);"
@endmacro

@macro name=select
@eval exp="system_object.select(mp.s1, mp.s2, mp.s3, mp.s4, mp.s5);"
[s][s]
@endmacro

@macro name=select2
@eval exp="system_object.select2(mp.s1, mp.s2, mp.s3, mp.s4, mp.s5, mp.s6);"
[s][s]
@endmacro


;;==============================================================================
;; メッセージウィンドウを表示
@macro name=messWindow_on
@eval exp="system_object.foreButtons.MessageWindowVisibleOn()"
@eval exp="system_object.backButtons.MessageWindowVisibleOn()"
@endmacro
;; メッセージウィンドウを非表示
@macro name=messWindow_off
@eval exp="system_object.foreButtons.MessageWindowVisibleOff()"
@eval exp="system_object.backButtons.MessageWindowVisibleOff()"
@endmacro
;;==============================================================================








@return
