//マイ画像クラス
class MyImage{
	constructor(imageData=null){
		this.InitializeProgress = 0;
		this.imgChange = false;
		//画像
		this.img = {
			back:["./img/kesu.png","./img/BG14a_80.jpg","./img/tanumachi004_800600.jpg"],
			body:["./img/kesu.png","./img/body.png","./img/body2.png","./img/body3.png","./img/body4.png","./img/body5.png","./img/body6.png","./img/body7.png","./img/body8.png","./img/body9.png","./img/body2_1.png"],
			sitagi:["./img/kesu.png","./img/sitagi.png","./img/sitagi2.png","./img/sitagi3.png","./img/sitagi2_2.png"],
			kao_iro:["./img/kesu.png","./img/kao_aka.png"],
			taitu:["./img/kesu.png","./img/taitu.png","./img/taitu2.png","./img/taitu_kutusita.png"],
			kutu:["./img/kesu.png","./img/kutu.png","./img/kutu2.png"],
			wear:["./img/kesu.png",
			"./img/seihuku1.png","./img/seihuku2.png","./img/seihuku3.png","./img/seihuku4.png",//1-4
			"./img/seihuku1_2.png","./img/seihuku1_3.png","./img/seihuku3_2.png","./img/seihuku3_3.png",//5-8
			"./img/shart_shorts.png","./img/tankutop_short1.png","./img/tankutop_short2.png","./img/tankutop_only.png"],//9-12
			kao:["./img/kesu.png","./img/kao.png","./img/kao2.png","./img/kao3.png","./img/kao4.png"],
			oshikko:["./img/kesu.png","./img/oshikko1.png","./img/oshikko2.png","./img/oshikko3.png","./img/oshikko4.png"],
		}
		if(imageData!=null)this.img = imageData;
		//画像個数
		this.ProgressCount = 0;
		this.imgCount = 0;
		this.imgCount += this.img.back.length;
		this.imgCount += this.img.body.length;
		this.imgCount += this.img.sitagi.length;
		this.imgCount += this.img.kao_iro.length;
		this.imgCount += this.img.taitu.length;
		this.imgCount += this.img.kutu.length;
		this.imgCount += this.img.wear.length;
		this.imgCount += this.img.kao.length;
		this.imgCount += this.img.oshikko.length;
		//ターゲット
		this.cavasTarget = {
			back:"image",
			body:"image1",
			sitagi:"image2",
			kao_iro:"image3",
			taitu:"image4",
			kutu:"image5",
			wear:"image6",
			kao:"image7",
			oshikko:"image8",
		}
		//画像
		this.imgIndexInit = {
			back:1,
			body:2,
			sitagi:2,			
			kao_iro:0,
			taitu:1,
			kutu:1,
			wear:1,
			kao:1,
			oshikko:0,
		}		
		//画像
		this.imgIndex = {
			back:0,
			body:0,
			sitagi:0,
			kao_iro:0,
			taitu:0,
			kutu:0,
			wear:0,
			kao:0,
			oshikko:0,
		}
		//画像
		this.imgMagn = {
			back:1,
			body:0.4,
			sitagi:0.4,
			kao_iro:0.4,
			taitu:0.4,
			kutu:0.4,
			wear:0.4,
			kao:0.4,
			oshikko:0.4,
		}		
		//画像
		var x_init = 0;
		this.imgPosXY = {
			back:[0,0],
			body:[x_init,0],
			sitagi:[x_init,0],
			kao_iro:[x_init,0],
			taitu:[x_init,0],
			kutu:[x_init,0],
			wear:[x_init,0],
			kao:[x_init,0],
			oshikko:[x_init,0],
		}		
		//画像情報
		this.imgKeyArry = [
			"back",
			"body",
			"sitagi",
			"kao_iro",
			"taitu",
			"kutu",
			"wear",
			"kao",
			"oshikko",
		];
	}
	//画像読込
	ImageLoad(index){
		var idx = this.GetIndexPos(index);
		var url = this.GetImgUrl(index,idx);
		//画像設定
		var Data = _self.GetDataStruct();
		Data.Name = index;
		Data.Value = url;
		_self.ImageRead(Data);
		_self.AddImage(Data);
	}
	
	//キャンバス描画済みチェック
	CanvasViewedStatus(index){
		var res = false;
		var imgCanvas = this.GetCanvasTarget(index);
		if(_self.ImageData[imgCanvas]!=undefined)res = true;
		return res;
	}
	
	//画像単体表示
	ImageView(index,wait=false,clear=false,target=false){
		//画像描画
		this.ImageLoad(index);
		var Data = _self.GetImage(index);
		var imgCanvas = this.GetCanvasTarget(index);			
		if(target)imgCanvas = target;
		_self.Elm(imgCanvas,_self.TypeID).style.position = "fixed";
		_self.Elm(imgCanvas,_self.TypeID).style.top = "0px";
		_self.Elm(imgCanvas,_self.TypeID).style.left = "0px";
		var x = this.GetImgPos(index,0);
		var y = this.GetImgPos(index,1);
		var Magn = this.GetImageMagn(index);
		var width = 400;
		var height = 500;		
		if(!wait)_self.ViewCanvas(Data.Name,imgCanvas,x,y,Magn);
		if(wait)_self.ViewCanvasWait(Data.Name,imgCanvas,x,y,Magn);
	}
	
	//描画消去
	ClearImage(index="back"){
		var width = 400;
		var height = 500;
		var imgCanvas = this.GetCanvasTarget(index);		
		_self.ImageData[imgCanvas].clearRect(0, 0, width, height);
	}
	
	//読込進捗
	LoadProgeress(index){
		var data = _self.GetImage(index);
		//if(data.flg){
			myImg.InitializeProgress = (++myImg.ProgressCount)/myImg.imgCount;
		//}else{
		//	_self.setTimer(myImg.LoadProgeress,100,index);
		//}
	}
	
	//初期表示
	InitImageView(){
		this.InitializeProgress = 0;
		var timerCount = 0;
		var timevalue = 0;
		myImg.imgCount += this.imgKeyArry.length;
		for(var loop in this.imgKeyArry){
			timevalue = parseInt(500+loop*10);
			var count = this.GetImageCount(this.imgKeyArry[loop]);
			for(var loop2=0;loop2<count;loop2++){
				this.SetIndexPos(this.imgKeyArry[loop],loop2);
				_self.setTimer(function(param){
					//myImg.ImageView(myImg.imgKeyArry[param],true);
					myImg.ImageLoad(myImg.imgKeyArry[param]);
					myImg.LoadProgeress(myImg.imgKeyArry[param]);
				},timevalue,loop);
			}
			timerCount += timevalue;
		}
		_self.setTimer(function(){
			timerCount = 0;
			for(var loop in myImg.imgKeyArry){
				timevalue = parseInt(500+loop*100);
				var index = myImg.GetIndexInitPos(myImg.imgKeyArry[loop]);
				myImg.SetIndexPos(myImg.imgKeyArry[loop],index);
				_self.setTimer(function(param){
					myImg.ImageView(myImg.imgKeyArry[param],true,true);
					myImg.imgChange = false;
					myImg.LoadProgeress(myImg.imgKeyArry[param]);
				},timevalue,loop);
				timerCount += timevalue;
			}		
		},timerCount);
		_self.setTimer(function(){
			myImg.ProgressCheck = function(){
				if(parseInt(myImg.InitializeProgress*100)==100){
					Main.imageload = true;
					this.InitializeProgress = 100;
					//myImg.AllImageView(true,true,"image8");
				}else{
					_self.setTimer(myImg.ProgressCheck,500);
				}
			}
			myImg.ProgressCheck();
		},100);		
	}
	
	//全画像
	AllImageView(wait=false,clear=true,target=false){
		if(target)_self.Elm(target,_self.TypeID).style.display = "";
		for(var loop in this.imgKeyArry){
			this.ImageView(this.imgKeyArry[loop],wait,clear,target);
			this.imgChange = false;
		}
		if(target)_self.Elm(target,_self.TypeID).style.display = "";
	}	
	
	//画像数取得
	GetImageCount(key){
		var res = null;
		switch(key){
			case "oshikko":
				res = this.img.oshikko.length;
				break;
			case "kao":
				res = this.img.kao.length;
				break;
			case "wear":
				res = this.img.wear.length;
				break;
			case "kutu":
				res = this.img.kutu.length;
				break;
			case "taitu":
				res = this.img.taitu.length;
				break;
			case "kao_iro":
				res = this.img.kao_iro.length;
				break;
			case "sitagi":
				res = this.img.sitagi.length;
				break;			
			case "body":
				res = this.img.body.length;
				break;			
			case "back":
				res = this.img.back.length;
				break;			
		}
		return res;		
	}
	
	//画像表示先取得
	GetCanvasTarget(key){
		var res = null;
		switch(key){
			case "oshikko":
				res = this.cavasTarget.oshikko;
				break;
			case "kao":
				res = this.cavasTarget.kao;
				break;
			case "wear":
				res = this.cavasTarget.wear;
				break;
			case "kutu":
				res = this.cavasTarget.kutu;
				break;
			case "taitu":
				res = this.cavasTarget.taitu;
				break;
			case "kao_iro":
				res = this.cavasTarget.kao_iro;
				break;
			case "sitagi":
				res = this.cavasTarget.sitagi;
				break;			
			case "body":
				res = this.cavasTarget.body;
				break;			
			case "back":
				res = this.cavasTarget.back;
				break;			
		}
		return res;		
	}

	//画像倍率取得
	GetImageMagn(key){
		var res = null;
		switch(key){
			case "oshikko":
				res = this.imgMagn.oshikko;
				break;
			case "kao":
				res = this.imgMagn.kao;
				break;
			case "wear":
				res = this.imgMagn.wear;
				break;
			case "kutu":
				res = this.imgMagn.kutu;
				break;
			case "taitu":
				res = this.imgMagn.taitu;
				break;
			case "kao_iro":
				res = this.imgMagn.kao_iro;
				break;
			case "sitagi":
				res = this.imgMagn.sitagi;
				break;			
			case "body":
				res = this.imgMagn.body;
				break;			
			case "back":
				res = this.imgMagn.back;
				break;			
		}
		return res;		
	}	
	
	//画像インデックス設定
	SetIndexPos(key,value){
		var res = null;
		switch(key){
			case "oshikko":
				res = this.imgIndex.oshikko = value;
				break;
			case "kao":
				res = this.imgIndex.kao = value;
				break;
			case "wear":
				res = this.imgIndex.wear = value;
				break;
			case "kutu":
				res = this.imgIndex.kutu = value;
				break;
			case "taitu":
				res = this.imgIndex.taitu = value;
				break;
			case "kao_iro":
				res = this.imgIndex.kao_iro = value;
				break;
			case "sitagi":
				res = this.imgIndex.sitagi = value;
				break;			
			case "body":
				res = this.imgIndex.body = value;
				break;			
			case "back":
				res = this.imgIndex.back = value;
				break;			
		}
		this.imgChange = true;
		return res;		
	}	
	
	//画像インデックス取得
	GetIndexPos(key){
		var res = null;
		switch(key){
			case "oshikko":
				res = this.imgIndex.oshikko;
				break;
			case "kao":
				res = this.imgIndex.kao;
				break;
			case "wear":
				res = this.imgIndex.wear;
				break;
			case "kutu":
				res = this.imgIndex.kutu;
				break;
			case "taitu":
				res = this.imgIndex.taitu;
				break;
			case "kao_iro":
				res = this.imgIndex.kao_iro;
				break;
			case "sitagi":
				res = this.imgIndex.sitagi;
				break;			
			case "body":
				res = this.imgIndex.body;
				break;			
			case "back":
				res = this.imgIndex.back;
				break;			
		}
		return res;		
	}
	
	//画像初期インデックス取得
	GetIndexInitPos(key){
		var res = null;
		switch(key){
			case "oshikko":
				res = this.imgIndexInit.oshikko;
				break;
			case "kao":
				res = this.imgIndexInit.kao;
				break;
			case "wear":
				res = this.imgIndexInit.wear;
				break;
			case "kutu":
				res = this.imgIndexInit.kutu;
				break;
			case "taitu":
				res = this.imgIndexInit.taitu;
				break;
			case "kao_iro":
				res = this.imgIndexInit.kao_iro;
				break;
			case "sitagi":
				res = this.imgIndexInit.sitagi;
				break;			
			case "body":
				res = this.imgIndexInit.body;
				break;			
			case "back":
				res = this.imgIndexInit.back;
				break;			
		}
		return res;		
	}		
	
	//画像座標取得
	GetImgPos(key,xy_value){
		var res = null;
		switch(key){
			case "oshikko":
				res = this.imgPosXY.oshikko[xy_value];
				break;
			case "kao":
				res = this.imgPosXY.kao[xy_value];
				break;
			case "wear":
				res = this.imgPosXY.wear[xy_value];
				break;
			case "kutu":
				res = this.imgPosXY.kutu[xy_value];
				break;
			case "taitu":
				res = this.imgPosXY.taitu[xy_value];
				break;
			case "kao_iro":
				res = this.imgPosXY.kao_iro[xy_value];
				break;
			case "sitagi":
				res = this.imgPosXY.sitagi[xy_value];
				break;			
			case "body":
				res = this.imgPosXY.body[xy_value];
				break;			
			case "back":
				res = this.imgPosXY.back[xy_value];
				break;			
		}
		return res;		
	}
	
	//画像取得
	GetImgUrl(key,index){
		var res = null;
		switch(key){
			case "oshikko":
				res = this.img.oshikko[index];
				break;
			case "kao":
				res = this.img.kao[index];
				break;
			case "wear":
				res = this.img.wear[index];
				break;
			case "kutu":
				res = this.img.kutu[index];
				break;
			case "taitu":
				res = this.img.taitu[index];
				break;
			case "kao_iro":
				res = this.img.kao_iro[index];
				break;
			case "sitagi":
				res = this.img.sitagi[index];
				break;			
			case "body":
				res = this.img.body[index];
				break;			
			case "back":
				res = this.img.back[index];
				break;			
		}
		
		return res;
	}
}
myImg = new MyImage();