//マイ画像クラス
function MyImage(){
	MyImage.prototype.constructor = function(){
		MyImage.prototype.Initialize = function(){
			MyImage.prototype.InitializeProgress = 0;
			MyImage.prototype.imgChange = false;
			//画像オブジェクト設定
			for(var loop in imageSetting.imgFile){
				var tmpObj = [];			
				for(var index in imageSetting.imgFile[loop]){
					tmpObj[index] = imageSetting.imgDir[loop]+imageSetting.imgFile[loop][index];
					imageSetting.imgObj[loop] = tmpObj;
				}
			}
			//時間背景
			MyImage.prototype.backCorrect = 0;
			var backtime = 0;
			MyImage.prototype.back_hiru = backtime++;
			MyImage.prototype.back_yuu = backtime++;
			MyImage.prototype.back_yoru = backtime++;
			MyImage.prototype.back_sinya = backtime++;
			MyImage.prototype.back_flg_asa = true;
			//画像
			MyImage.prototype.img = imageSetting.imgObj;
			//画像個数
			MyImage.prototype.ProgressCount = 0;
			MyImage.prototype.imgCount = 0;
			//画像初期表示位置
			var x_init = 0;
			var y_init = 0;
			//RPG用モンスターイメージコピー
			MyImage.prototype.img["monster1"] = this.img["monster1"];
			MyImage.prototype.img["monster2"] = this.img["monster1"];
			MyImage.prototype.img["monster3"] = this.img["monster1"];
			//画像オブジェクトからキー取得
			var imgKey = Object.keys(this.img);			//ループ用
			MyImage.prototype.imgKeyArry = Object.keys(this.img);	//処理用画像名配列
			//各種処理用オブジェクト生成
			MyImage.prototype.cavasTarget = {}						//描画ターゲット
			MyImage.prototype.imgIndex = {}							//画像インデックス
			MyImage.prototype.imgMagn = {}							//画像マージン
			MyImage.prototype.imgPosXY = {}							//画像表示位置
			for(var loop in imgKey){
				this.imgCount += this.img[imgKey[loop]].length;//画像数カウント
				this.cavasTarget[imgKey[loop]]="image"+(parseInt(loop)==0?"":loop);//描画ターゲット設定
				this.imgIndex[imgKey[loop]] = 0;
				this.imgMagn[imgKey[loop]] = imageSetting.imgMagn[imgKey[loop]];
				this.imgPosXY[imgKey[loop]] = (parseInt(loop)==0?[0,0]:[x_init,y_init]);
			}
			//画像初期表示インデックス
			MyImage.prototype.imgIndexInit = imageSetting.imgIndex;
			MyImage.prototype.GraphStrokeRect = [150,10,100,20];
			MyImage.prototype.GraphFillRect = [151,11,98,19];
		}
	}
	this.constructor();
	
	//キャンバス生成
	MyImage.prototype.CreateCanvas = function(){
		var imageViewSpase = _self.Elm("imageViewSpase",_self.TypeID);
		var imgKey = this.imgKeyArry;
		for(var loop in imgKey){
			var canvasId = this.cavasTarget[imgKey[loop]];
			var elmCnv = _self.CreateElm("canvas",canvasId);
			elmCnv.classList = "image_display";
			imageViewSpase.appendChild(elmCnv);
		}
	}
	
	//グラフ生成
	MyImage.prototype.CreateGraph = function(graphId,MaxFlg=false){
		if((_self.Elm(graphId,_self.TypeID)==undefined)||(_self.Elm(graphId,_self.TypeID)==null)){
			var imageViewSpase = _self.Elm("imageViewSpase",_self.TypeID);
			var elmCnv = _self.CreateElm("canvas",graphId);
			elmCnv.classList = "image_display";
			imageViewSpase.appendChild(elmCnv);
			var canvas = _self.Elm(graphId,_self.TypeID);
			canvas.style.width = _self.width_hi+"px";
			var ctx = canvas.getContext('2d'); 
			ctx.strokeStyle  = 'deeppink';
			ctx.fillStyle = 'hotpink'; 
			var Rect = this.GraphStrokeRect;
			var Rect1 = this.GraphFillRect;
			ctx.strokeRect(Rect[0],Rect[1],Rect[2],Rect[3]);
			if(MaxFlg)ctx.fillRect(Rect1[0],Rect1[1],Rect1[2],Rect1[3]-1);
		}
		if(_self.Elm(graphId,_self.TypeID).style.display == "none"){
			_self.Elm(graphId,_self.TypeID).style.display = "";
		}
	}
	
	//グラフ描画
	MyImage.prototype.ViewGraph = function(graphId,Value,MaxValue){
		var canvas = _self.Elm(graphId,_self.TypeID);
		var ctx = canvas.getContext('2d'); 
		ctx.strokeStyle  = 'deeppink'; 
		ctx.fillStyle   = 'hotpink'; 
		var Rect = this.GraphStrokeRect;
		var Rect1 = this.GraphFillRect;
		ctx.strokeRect(Rect[0],Rect[1],Rect[2],Rect[3]);
		if((Value/MaxValue)<=1)ctx.clearRect(Rect1[0],Rect1[1],Rect1[2],Rect1[3]);
		ctx.fillRect(Rect1[0],Rect1[1],Rect1[2]*(Value/MaxValue),Rect1[3]-1);	
	}
	//グラフ描画
	MyImage.prototype.ViewGraphDec = function(graphId,Value,MaxValue){
		var canvas = _self.Elm(graphId,_self.TypeID);
		var ctx = canvas.getContext('2d'); 
		ctx.strokeStyle  = 'deeppink'; 
		ctx.fillStyle   = 'hotpink'; 
		var Rect = this.GraphStrokeRect;
		var Rect1 = this.GraphFillRect;
		ctx.strokeRect(Rect[0],Rect[1],Rect[2],Rect[3]);
		if((Value/MaxValue)<=1)ctx.fillRect(Rect1[0],Rect1[1],Rect1[2],Rect1[3]);
		ctx.clearRect(Rect1[0],Rect1[1],Rect1[2],Rect1[3]*(1-(Value/MaxValue)));
	}	
	
	//グラフ描画
	MyImage.prototype.ClearGraph = function(graphId){
		_self.Elm(graphId,_self.TypeID).style.display = "none";
	}	
	
	
	//画像読込
	MyImage.prototype.ImageLoad = function(index){
		var idx = parseInt(this.GetIndexPos(index));
		if(index=="back")idx = idx + parseInt(myImg.backCorrect);
		var url = this.GetImgUrl(index,idx);
		//画像設定
		var Data = _self.GetImage(index+idx);
		if(Data==null){
			Data = _self.GetDataStruct();
			Data.Name = index+idx;
			Data.Value = url;
			console.log(Data);
			_self.ImageRead(Data);
			_self.AddImage(Data);
		}
	}
	
	//キャンバス描画済みチェック
	MyImage.prototype.CanvasViewedStatus = function(index){
		var res = false;
		var imgCanvas = this.GetCanvasTarget(index);
		if(_self.ImageData[imgCanvas]!=undefined)res = true;
		return res;
	}
	
	//画像単体表示
	MyImage.prototype.ImageView = function(index,wait=false,clear=false,target=false){
		//画像描画
		var idx = parseInt(this.GetIndexPos(index));
		if(index=="back")idx = idx + parseInt(myImg.backCorrect);		
		this.ImageLoad(index,idx);
		var Data = _self.GetImage(index+idx);
		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);
		if(!wait)_self.ViewCanvas(Data.Name,imgCanvas,x,y,Magn);
		if(wait)_self.ViewCanvasWait(Data.Name,imgCanvas,x,y,Magn);
	}
	
	//全消去
	MyImage.prototype.AllClearImage = function(){
		for(var loop in this.imgKeyArry){
			this.ClearImage(this.imgKeyArry[loop]);
		}		
	}
	
	//描画消去
	MyImage.prototype.ClearImage = function(index="back",rectData = [0,0,_self.width_hi, _self.height_hi]){
		var imgCanvas = this.GetCanvasTarget(index);
		try{
			_self.ImageData[imgCanvas].clearRect(rectData[0],rectData[1],rectData[2],rectData[3]);
		}catch(e){
			console.log("error:ClearImage");
			console.log("index:"+index);
			console.log("rectData");
			console.log(rectData);
			console.log(e);
		}
	}
	
	//読込進捗
	MyImage.prototype.LoadProgeress = function(param){
		//画像描画
		var index = param[0];
		var in_idx= param[1];
		var idx = parseInt(myImg.GetIndexPos(index));
		if(in_idx!=null)idx = in_idx;
		if(index=="back")idx = idx + parseInt(myImg.backCorrect);			
		var data = _self.GetImage(index+idx);
		if(data.flg){
			myImg.InitializeProgress = (++myImg.ProgressCount)/myImg.imgCount;
		}else{
			_self.setTimer(myImg.LoadProgeress,100,[index,idx]);
		}
	}
	
	//初期表示
	MyImage.prototype.InitImageView = function(){
		this.InitializeProgress = 0;
		var timerCount = 0;
		var timevalue = 0;
		myImg.imgCount += this.imgKeyArry.length;
		for(var loop in this.imgKeyArry){
			timevalue = parseInt(loop*50);
			var count = this.GetImageCount(this.imgKeyArry[loop]);
			for(var loop2=0;loop2<count;loop2++){
				_self.setTimer(function(param){
					console.log(param);
					myImg.SetIndexPos(myImg.imgKeyArry[param[0]],param[1]);
					myImg.ImageView(myImg.imgKeyArry[param[0]],true);
					myImg.ImageLoad(myImg.imgKeyArry[param[0]]);
					myImg.LoadProgeress([myImg.imgKeyArry[param[0]],null]);
				},timevalue,[loop,loop2]);
				timevalue += 10;
			}
			timerCount += timevalue;
		}
		_self.setTimer(function(){
			timerCount = 0;
			for(var loop in myImg.imgKeyArry){
				timevalue = parseInt(loop*50)+100;
				var index = myImg.GetIndexInitPos(myImg.imgKeyArry[loop]);
				_self.setTimer(function(param){
					myImg.SetIndexPos(myImg.imgKeyArry[param[0]],param[1]);
					myImg.ImageView(myImg.imgKeyArry[param[0]],true,true);
					myImg.imgChange = false;
					myImg.LoadProgeress([myImg.imgKeyArry[param[0]],null]);
				},timevalue,[loop,index]);
				timerCount += timevalue;
			}		
		},timerCount);
		_self.setTimer(function(){
			myImg.ProgressCheck = function(){
				if(parseInt(myImg.InitializeProgress*100)==100){
					Main.imageload = true;
					this.InitializeProgress = 100;
				}else{
					_self.setTimer(myImg.ProgressCheck,500);
				}
			}
			myImg.ProgressCheck();
		},100);		
	}
	
	//全画像
	MyImage.prototype.AllImageView = function(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 = "";
	}	
	
	//画像オブジェクトデータ取得
	MyImage.prototype.GetImgObjParam = function(key,obj){
		var res = null;
		var keyArray = this.imgKeyArry;
		for(var loop in this.imgKeyArry){
			if(keyArray[loop]==key){
				res = obj[key];
			}
		}
		return res;
	}
	
	//画像数取得
	MyImage.prototype.GetImageCount = function(key){
		var res = null;
		res = this.GetImgObjParam(key,this.img);
		return res.length;		
	}
	
	//画像表示先取得
	MyImage.prototype.GetCanvasTarget = function(key){
		var res = null;
		res = this.GetImgObjParam(key,this.cavasTarget);
		return res;		
	}

	//画像倍率取得
	MyImage.prototype.SetImageMagn = function(key,value){
		if(this.imgKeyArry.indexOf(key)!==-1){
			this.imgMagn[key] = value;
			this.imgChange = true;
		}		var res = null;
	}

	//画像倍率取得
	MyImage.prototype.GetImageMagn = function(key){
		var res = null;
		res = this.GetImgObjParam(key,this.imgMagn);
		return res;		
	}
	
	//画像インデックス設定
	MyImage.prototype.SetIndexPos = function(key,value){
		if(typeof value==='object')value = value.back;
		if(this.imgKeyArry.indexOf(key)!==-1){
			this.imgIndex[key] = value;
			this.imgChange = true;
		}
	}	
	
	//画像インデックス取得
	MyImage.prototype.GetIndexPos = function(key){
		var res = null;
		res = this.GetImgObjParam(key,this.imgIndex);
		return res;		
	}
	
	//画像初期インデックス取得
	MyImage.prototype.GetIndexInitPos = function(key){
		var res = null;
		res = this.GetImgObjParam(key,this.imgIndexInit);
		return res;		
	}			
	
	//画像座標取得
	MyImage.prototype.SetImgPos = function(key,xy_value){
		if(this.imgPosXY[key]){
			this.imgPosXY[key] = xy_value;
			this.imgChange = true;
		}
	}
	
	//画像座標取得
	MyImage.prototype.GetImgPos = function(key,xy_value=-1){
		var res = null;
		res = this.GetImgObjParam(key,this.imgPosXY);
		if(xy_value!=-1)res = res[xy_value];
		return res;
	}
	
	//画像取得
	MyImage.prototype.GetImgUrl = function(key,index){
		var res = null;
		res = this.GetImgObjParam(key,this.img);
		return res[index];
	}
}
myImg = new MyImage();