//メイン処理
var title_str = 'ゲームフレーム';
var ver_str = "ver.0.0.1-230902";
var ver_str_option = "";
ver_str = ver_str+ver_str_option;
var dateDay = "20"+ver_str.split("-")[1];
var proc_buff = ["",dateDay.slice(0,4),dateDay.substr(4,2),dateDay.substr(6,2)];
var time_obj = new Date(proc_buff[1],(parseInt(proc_buff[2])-1),proc_buff[3],23,59);
time_obj.setMonth(time_obj.getMonth() + 1);
var timelimit = "timelimit,"+time_obj.getFullYear()+","+(time_obj.getMonth()+1)+","+time_obj.getDate();
var myRpg = null;
var imageSetting = null;
var myImg = null;
var myBT = null;
var myBE = null;
var myProc = null;
var myST = null;
var myMap = null;
var myCustom = null;
var mySpCode;
var MapFlg = false;
var glObj = null;
var storage_func = null;
Main = new StartProcess();
Main.title_str = title_str;//サブタイトルがある場合はここに改行コードを入れて追加
document.oncontextmenu = function () {return false;}
//処理開始
Main.ProcStart = function(){
	if(window.parent.caches!=undefined)window.parent.caches.delete("call");
	var _this = null;
	_this = new BaseJs();
	_self = _this;
	_self.doc = document;
	//タイトル
	_self.doc.title = this.title_str;	
	/*初期設定処理もともとの場所*/
	/*var buffWidth = window.innerWidth<=800?window.innerWidth:800;
	var buffheight = window.innerHeight<=600?window.innerHeight:600;*/
	var buffWidth = window.innerWidth;
	var buffheight = window.innerHeight;	
	var bodyElm = _self.doc.body;
	var zoom_value = 0;
	var zoom_value_x = 1;
	var zoom_value_y = 1;
	if((buffWidth-10)>=_self.width_hi)zoom_value_x = (buffWidth-10)/_self.width_hi;
	if((buffheight-10)>=_self.height_hi)zoom_value_y = (buffheight-10)/_self.height_hi;
	/*if(buffWidth <= 800)zoom_value = zoom_value_x;
	if((buffheight <= 600)&&((zoom_value==0)||(window.innerHeight<=(buffheight*zoom_value))))zoom_value = zoom_value_y;
	_self.zoom_value = zoom_value;*/
	if(window.innerHeight>=window.innerWidth){
		zoom_value = zoom_value_x;
	}else{
		zoom_value = zoom_value_y;
	}
	_self.zoom_value = zoom_value;
	bodyElm.style.transformOrigin='top left';
	bodyElm.style.transform='scale(' + zoom_value + ')';
	bodyElm.style.width=Math.round(1000/zoom_value)/10 + '%';
	bodyElm.style.height=Math.round(1000/zoom_value)/10 + '%';
	var width = Main.DisplayWidth(_self.width_hi);
	var height = Main.DisplayHeight(_self.height_hi);
	_this.setWidthHi(width);
	_this.setHeightHi(height);
	//暗幕
	Main.Loading(_this,width+"px",height+"px",false);
	//メイン処理
	Main.MainProc = function(){
		document.oncontextmenu = function () {return false;}
		//データ名設定
		//画像表示用カスタムクラス読み込み
		Main.oneload = false;
		Main.WaitLoad = function(){
			var timeValue = 100;
			if(myRpg==null){
				Main.ScriptLoad("./js/MyRPGSystem.js?202409292330");
				_this.setTimer(Main.WaitLoad,timeValue);
				return;
			}else if(imageSetting==null){
				Main.ScriptLoad("./js/ImageSetting.js?202409292330");
				_this.setTimer(Main.WaitLoad,timeValue);
				return;
			}else if(myBT==null){
				Main.ScriptLoad("./js/MyBoxTouch.js?202409292330");
				_this.setTimer(Main.WaitLoad,timeValue);
				return;
			}
			else if(myImg==null){
				Main.ScriptLoad("./js/MyImage.js?202409292330");	
				_this.setTimer(Main.WaitLoad,timeValue);
				return;
			}
			else if(myST==null){
				Main.ScriptLoad("./js/MySetting.js?202409292330");	
				_this.setTimer(Main.WaitLoad,timeValue);
				return;
			}			
			else if(myBE==null){
				Main.ScriptLoad("./js/MyBaseEvent.js?202409292330");
				_this.setTimer(Main.WaitLoad,timeValue);
				return;
			}
			else if(myProc==null){
				Main.ScriptLoad("./js/MyProcessing.js?202409292330");
				_this.setTimer(Main.WaitLoad,timeValue);
				return;
			}else if(myCustom==null){
				Main.ScriptLoad("./custom/custom.js?202409292330");
				_this.setTimer(Main.WaitLoad,timeValue);
				return;
			}else if(myMap==null&&MapFlg){
				Main.ScriptLoad("./js/MapTool/maplib.js?202112111601");
				Main.ScriptLoad("./js/MapTool/littleambitionJs.js?202409292330");
				Main.ScriptLoad("./js/MapTool/storage.js?202409292330");
				Main.ScriptLoad("./js/MapTool/Ruins/imageDataxml.js?202409292330");
				Main.ScriptLoad("./js/MapTool/map.js?202409292330");				
				_this.setTimer(Main.WaitLoad,timeValue);
				return;
			}else if(glObj==null&&MapFlg){
				_this.setTimer(Main.WaitLoad,timeValue*5);
				return;
			}
			//初期設定処理--->
			_self.debug = false;
			//起動時機能解放コード取得
			mySpCode = new DataSave;
			mySpCode.constructor();
			mySpCode.SetDataName(title_str+"_SpCode");
			mySpCode.Load();		
			var sData = mySpCode.AppSaveData;
			_self.Spcode = 0;
			mySpCode.CheckCode = function(saveData=null){
				var res = false;
				var codeConvert = {
					free:["jisO@sfg",false],
					code100:["atrf@h6ge",false],
					code300:["tyioa#er",false],
					code500:["loywu+65",false],
					codeProduct:["rdqrt7%e",false],
					codeWebOnly:["web@aksd-223",false],
					codeDotNetOnly:["dotnet@llk887q",false],
				};
				if(sData!=null){
					var code = sData["SpCodeValue"];
					_self.Spcode = 0;
					if(codeConvert.free[0]==code){_self.Spcode += 1;codeConvert.free[1]=true;res = true;}//1
					if(codeConvert.code100[0]==code){_self.Spcode += 2;codeConvert.code100[1]=true;res = true;}//2
					if(codeConvert.code300[0]==code){_self.Spcode += 4;codeConvert.code300[1]=true;res = true;}//4
					if(codeConvert.codeProduct[0]==code){_self.Spcode += 8;codeConvert.codeProduct[1]=true;res = true;}//8
					if(codeConvert.code500[0]==code){_self.Spcode = 31;codeConvert.code500[1]=true;res = true;}//16
					if(codeConvert.codeWebOnly[0]==code){_self.Spcode = 32;codeConvert.codeWebOnly[1]=true;res = true;}//32
					if(codeConvert.codeDotNetOnly[0]==code){_self.Spcode = 64;codeConvert.codeDotNetOnly[1]=true;res = true;}//32
					_self.codeData = codeConvert;
				}else{
					_self.codeData = codeConvert;
					mySpCode.SetDataName(title_str+"_SpCode");
					mySpCode.SetSaveData("SpCodeValue","");
					mySpCode.Save();		
				}
				return res;
			}
			mySpCode.CheckCode();
			//製品フラグ
			var webSw = location.origin.indexOf("http");
			var input_option = location.search.substr(1,location.search.length);
			var dNetSw = (input_option.indexOf("DotNet")!=-1);
			_self.Windows_flg = (input_option.indexOf("windows")!=-1);
			_self.product_flg = (input_option.indexOf("true")==-1)||(_self.Spcode&4);
			_self.WebProduct_flg = (_self.Spcode&32);
			_self.dNetProduct_flg = (_self.Spcode&64);
			_self.sample_flg = (input_option.indexOf("true")!=-1);
			_self.sample2_flg = (input_option.indexOf("true2")!=-1)||(_self.Spcode==1);
			_self.special_flg = (input_option.indexOf("special")!=-1)||(_self.Spcode&16);
			_self.clear_flg = (input_option.indexOf("clear")!=-1);
			_self.Output_flg = (input_option.indexOf("Output")!=-1);
			_self.test_flg = (input_option.indexOf("test")!=-1);
			_self.timelimit = ((input_option.indexOf("timelimit")!=-1)?true:false)||(_self.Spcode&2);
			console.log("製品モード:"+_self.product_flg);
			//製品時のコード設定
			if(_self.product_flg){
				if(_self.Spcode>=4 && _self.Spcode<=15){
					mySpCode.SetDataName(title_str+"_SpCode");
					mySpCode.SetSaveData("SpCodeValue",_self.codeData.codeProduct[0]);
					mySpCode.Save();
				}else if(input_option.indexOf("true")==-1 && _self.Spcode<=15){
					mySpCode.SetDataName(title_str+"_SpCode");
					mySpCode.SetSaveData("SpCodeValue",_self.codeData.codeProduct[0]);
					mySpCode.Save();
				}
			}
			if((!_self.sample_flg)&&_self.sample2_flg)_self.product_flg = true;	
			//設定コードから解除
			if(_self.Spcode>=4){
				_self.sample_flg = false;
				_self.sample2_flg = false;
				_self.timelimit = false;
			}
			if(_self.Spcode&2){
				_self.sample_flg = false;
				_self.sample2_flg = false;
				_self.product_flg = true;
			}
			//ネット専用
			if(_self.WebProduct_flg&&webSw){
				_self.sample_flg = false;
				_self.sample2_flg = false;
				_self.product_flg = true;
			}
			//.Net専用
			if(_self.dNetProduct_flg&&dNetSw){
				_self.sample_flg = false;
				_self.sample2_flg = false;
				_self.product_flg = true;
			}
			//データ初期化
			if(_self.clear_flg){
				localStorage.removeItem(title_str+"_SpCode");
				localStorage.removeItem(title_str);
				_self.Elm("operation",_self.TypeID).innerHTML = '<center><p style="color:white">データを初期化致しました。<br>アプリを再起動してください。</p></center>';
				return;
			}
			//データ抽出
			if(_self.Output_flg){
				var getData = [];
				document.oncontextmenu = function () {return false;}
				getData[getData.length] = localStorage.getItem(title_str+"_SpCode");
				getData[getData.length] = localStorage.getItem(title_str);
				getData[getData.length] = localStorage.getItem("Test_MapData");
				getData[getData.length] = localStorage.getItem("Test_MapPos");
				var viewData = JSON.stringify(getData);
				// Encrypt
				var ciphertext = CryptoJS.AES.encrypt(viewData, 'kazuya2009').toString();
				_self.Elm("operation",_self.TypeID).innerHTML = '<center><p style="color:white">クリップボードにデータを抽出しました。<br>テキストエディタ等に張り付けてください。<br>貼り付け後、アプリを再起動してください。</p></center>';
				// 文字をすべて選択
				if(navigator.clipboard){
					navigator.clipboard.writeText(ciphertext);
				}
				return;
			}
			//<---初期設定処理
			//画像データ設定
			myST.Img = imageSetting.Img;
			//音楽/効果音読込
			myProc.music();
			myProc.SESound();
			myProc.Voice();
			//キャンバス生成
			myImg.CreateCanvas();
			myImg.SetImgPos("title",[0,0]);
			//マップ設定用
			if(MapFlg){
				var move_canvas = null;
				for(var loop in myImg.imgKeyArry){
					var elm = myImg.imgKeyArry;
					if(elm[loop]=="body"){
						move_canvas = "image"+(parseInt(loop)==0?"":loop);
					}
				}
				//maplib設定用
				var Option = {
					"parent":"imageViewSpase",
					"move_canvas":move_canvas,
				};

				//マップ初期化
				myMap.Initialize(Option);
			}
			//初期読み込み処理
			_this.InitProgeress = function(_this){
				//画像読み込み
				_this.Elm(".Progress").innerText = parseInt(myImg.InitializeProgress*100)+"%";
				if(parseInt(myImg.InitializeProgress*100)<100){
					_this.setTimer(_this.InitProgeress,100,_this);
				}else{
					/*_this.setTimer(function(_this){
						if((_this.Elm(".Progress").innerText!="")||(!_self.StartOn)){
							_this.Elm(".Progress").innerText = "まもなく起動します...";
						}else{
							
						}
						_self.WarningView();
					},500,_this);*/
					//期間限定か否かチェック
					_this.setTimer(function(){
						//現在時刻を取得
						var now_obj = new Date();	
						var now_time = now_obj.getTime();					
						//期限設定
						if(_self.timelimit){
							var split_data = timelimit.split(",");
							//入力された日付から期間を取得
							var after_obj = new Date(split_data[1],(parseInt(split_data[2])-1),split_data[3],23,59);
							var now_time = now_obj.getTime();
							var after_time = after_obj.getTime();
							if(after_time>=now_time){
								_this.Elm(".Progress").innerHTML = "この製品はプレイ期間限定版モードです。<br>"+split_data[1]+"年"+split_data[2]+"月"+split_data[3]+"日までプレイが出来ます。";
								_self.product_flg = true;
							}else{
								sample_flg = "true";
								_this.Elm(".Progress").innerHTML = "プレイ期間が過ぎています。<br>限定体験版で動作させます。";
								_self.sample2_flg = true;
							}
						}else if(_self.sample_flg){
							_this.Elm(".Progress").innerHTML = "この製品は体験版モードです。";
						}else if(_self.sample2_flg){
							_this.Elm(".Progress").innerHTML = "この製品は限定体験版モードです。";
						}
						myImg.SetIndexPos('back',0);
						myImg.SetIndexPos('title',1);
						myImg.AllImageView(true);
						_this.setTimer(function(){
						var elm = _this.Elm("anmaku",_this.TypeID);
							elm.style.opacity = "0.5";
						},200);
					},550);
					myImg.SetIndexPos('body',0);
				}
			}
			_this.InitProgeress(_this);
			myImg.InitImageView();
		}	
		Main.WaitLoad();
		//枠描画
		_this.ViewInitializeBox = function(){
			var elm = _this.Elm("anmaku",_this.TypeID);
			if(elm.innerHTML!=""){
				_this.setTimer(_this.ViewInitializeBox,1000);
				return;
			}
			var touch = _self.Elm("body");
			if(!_self.CheckTouchEvent()){
				touch[0].onmousedown = myST.BodyTouch;
			}else{
				touch[0].ontouchdown = myST.BodyTouch;
			}
			//枠を生成
			var parent = _self.Elm("operation",_self.TypeID);
			parent.style.width = _self.width_hi;
			parent.style.height = _self.height_hi;
			var imgElm = _self.Elm(".image");
			//画像消去
			myImg.AllClearImage();
			//イベント設定
			myProc.move(true);
			myProc.event(myST.GetEvent);
			var data = myProc.loadData(true);
			var dataCount = myProc.LoadIndexMax;
			//保存データ枠生成
			for(var loop=0;loop<myProc.LoadIndexMax;loop++){
				var boxId = myBT.saveLoadBox(parent,loop);
				var elm = _self.Elm(boxId,_self.TypeID);
				elm.style.textAlign = "center";
				if(data["saveFlg"+loop]==false){
					dataCount--;
					elm.innerHTML = "<br>";
					elm.innerHTML += "データなし";
					_this.DataLoadFunc = function(input_id){
						var stratPos = String("saveLoad").length;
						var endPos = input_id.length - stratPos;
						var idx = parseInt(input_id.substr(stratPos,endPos));
						myProc.LoadIndex = idx;
						myBT.yesNoBox(parent,"データNo"+parseInt(idx+1)+"でゲームを始めますか？",function(){
							//枠描画
							myBT.setBoxTouch(parent);
							//処理設定
							_this.Processing();							
						});
					}
					elm.onmousedown = function(){
						if(!_self.CheckTouchEvent()){
							_this.DataLoadFunc(this.id);
						}
					}
					elm.ontouchdown = function(){
						_this.DataLoadFunc(this.id);
					}
				}else{
					var mvPosData = myProc.movedata[0];
					var mvIdx = data["moveIndex"+loop];
					elm.innerHTML += '<div id="load'+loop+'"></div>';
					elm.innerHTML += '<div id="del'+loop+'">消去</div>';
					var loadelm = _self.Elm("load"+loop,_self.TypeID);
					loadelm.innerHTML += 'データ'+parseInt(loop+1)+'<br>';
					var funcIdx = mvPosData.length-1;
					loadelm.innerHTML += "場所："+mvPosData[funcIdx](mvIdx)+"<br>";
					//loadelm.innerHTML += "LV："+data["RpgData"+loop].LV+",EXE："+data["RpgData"+loop].EXE+",";
					loadelm.innerHTML += "幼なじみ奴隷"+data["Day"+loop]+"日目<br>";
					loadelm.onmousedown = function(){
						var stratPos = String("load").length;
						var endPos = this.id.length - stratPos;
						var idx = parseInt(this.id.substr(stratPos,endPos));
						myProc.LoadIndex = idx;
						myBT.yesNoBox(parent,"データNo"+parseInt(idx+1)+"の続きを始めますか？",function(){
							//枠描画
							myBT.setBoxTouch(parent);
							//処理設定
							_this.Processing();
						});	
					}
					loadelm.ontouchdown = function(){
						elm.onmousedown();
					}					
					var delelm = _self.Elm("del"+loop,_self.TypeID);
					delelm.onmousedown = function(){
						var stratPos = String("del").length;
						var endPos = this.id.length - stratPos;
						var idx = parseInt(this.id.substr(stratPos,endPos));
						myProc.LoadIndex = idx;
						myBT.yesNoBox(parent,"データNo"+parseInt(idx+1)+"を消しますか？",function(){
							//枠描画
							myBT.setBoxTouch(parent);
							//処理設定
							myProc.deleteData();
							parent.innerHTML = "";
							_this.ViewInitializeBox();
						});						
					}
					delelm.ontouchdown = function(){
						delelm.onmousedown();
					}
				}
			}
			/*ロードデータあり/なしの場合はいきなりゲームスタート*/
			if(!dataCount){
				if(dataCount==0)myProc.LoadIndex = 4;
				myBT.setBoxTouch(parent);
				_this.Processing();
			}
		}
		//各種処理
		_this.Processing = function(firstFlg=true){
			//カスタム初期化処理
			myCustom.Initialize();

			_self.Elm("anmaku",_self.TypeID).style.opacity = 0;
			_self.ViewBox("anmaku");
			//セーブ枠を消す
			for(var loop=0;loop<myProc.LoadIndexMax;loop++){
				_self.HiddenBox("saveLoad"+loop);
			}
			
			//初回のみ通す
			if(firstFlg){
				_self.AddCommand_org = _self.AddCommand;
				_self.AddCommand = function(addElmId,Key,str,display=null){
					_self.AddCommand_org(addElmId,Key,str,display);
					_self.Elm(addElmId,_self.TypeID).style.lineHeight = "0.85em";
				}
			}
			
			/*画像位置初期変更*/
			var imgYpos = 0;
			//横位置画面幅毎変更
			var imgXpos = 0;
			var Charbody_x = 0;
			var Charbody_y = -50;
			if(_self.width_hi!=400)imgXpos = -70;			
			myImg.SetImgPos("back",[imgXpos,-50]);
			myImg.SetImgPos("Other",[imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("body",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("body_bk_hair",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("body_face_bk",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("body_fr_hair",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("body_face_fk",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("kutusita",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("kutu",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("burager",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("siru",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("manko",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("panty",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("inner",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("shatu",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("sukart",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("seihuku",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("penis",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("shokushu",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("seieki",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("sitem",[Charbody_x+imgXpos,Charbody_y+imgYpos]);		
			myImg.SetImgPos("monster1",[imgXpos,imgYpos]);		
			myImg.SetImgPos("monster2",[imgXpos,imgYpos]);		
			myImg.SetImgPos("monster3",[imgXpos,imgYpos]);		
			myImg.SetImgPos("MagicEffect",[imgXpos,imgYpos]);		
			myImg.SetImgPos("BattleEffect",[imgXpos,imgYpos]);		
			myImg.SetImgPos("Other2Back",[imgXpos,imgYpos]);		
			myImg.SetImgPos("Other2",[imgXpos,imgYpos]);
			/*-------------------------*/						
			//各種コマンド処理宣言
			myBT.DayTime();
			myBT.OrgasmFunc();
			myProc.Initialize();
			//RPGキャラデータはここで
			if(myRpg.FunctionFlg){
				myRpg.SetCharactors();			
				myRpg.SetMagicItemData();
				myRpg.StoreFunction();
				myRpg.StatusFunction();
				myRpg.SleepFunction();
				myRpg.ItemFunction();
				myRpg.Attack();
				myRpg.Guard();
				myRpg.BattleItem();
				myRpg.Escape();
				myRpg.GuildFunction();
			}
			//MAP使用時
			if(MapFlg){
				//ダンジョンMAP
				myMap.SetImageCorrect();
				//エリア変更
				myMap.NextArea();
				myMap.BeforeArea();
				myMap.ViewMapAreaPos();
			}
			
			//データロード
			myProc.loadData();
			myBT.chgDayTime();
			if(MapFlg)myMap.SetTreasure();
			
			//経験値、キャラクターデータチェック用
			var flg = true;
			if(flg){
				var msg;
				var Charctor = JSON.parse(JSON.stringify(myRpg.Charctor));
				console.log(JSON.stringify(myRpg.CharArray[0]));
				myRpg.Charctor.LV = 1;
				myRpg.Charctor.EXE = myRpg.GetLevelUpEXE(false,30);
				msg = myRpg.LevelUpProc(msg);
				myRpg.Charctor = Charctor;
			}
			
			//シナリオデータログ出力
			myProc.OutPutSinario();			
			console.log(_self.width_hi);
			_self.setTimer(function(){
				var exc = myBE.serchExecuteEvent();
				if(exc[myBE.ID]!=0)myBT.OpeningFlg = false;
				myBE.excuteEvent(exc);
				if((!myBT.OpeningFlg)&&(!exc))myProc.moveBefore(myProc.moveIndex);
				if((!myBT.OpeningFlg))_self.HiddenBox("anmaku");
				if((!myBT.OpeningFlg))myBT.MenuView();
				//RPG用
				if(MapFlg){
					var tmp = myRpg.Charctor;
					if(tmp.MAPINDEX!==undefined)gl_map_index　= tmp.MAPINDEX;
					if(tmp.ADVPOS!==undefined)gl_now_adv_pos = tmp.ADVPOS;
					if(tmp.MAPX!==undefined)gl_now_pos_x = tmp.MAPX;
					if(tmp.MAPY!==undefined)gl_now_pos_y = tmp.MAPY;
				}
				myST.START_FLG = false;
			},500);
			myProc.music(myProc.musIndex,0);
		}
		_this.ViewInitializeBox();
	}	
	Main.MainProc();
}
Main.ClassLoad();
Main.Start(Main.ProcStart);
