//画像処理クラス
function ImageProcess(){
	//コンストラクタ
	this.constructor = function(){
		this.ImageData = [];
		this.SetData = [];
		this.Count = 0;
		this.ProcFlg = false;
		//背景設定用
		this.height_room = 600;
		this.width_room = 800;
		//表示範囲
		this.height_hi = 500;
		this.width_hi = 400;		
		this.height_low = 400;
		this.width_low = 300;		
	}
	
	//画像読み込み
	this.ImageRead = function(Data){
		var imagePath = null;
		var serchKey = 0;
		var surceFlg = false;
		//初回読込済みの場合
		if(Data.Buff!=null){
			serchKey = Data.Value.strsub(1,Data.Value.length-1);
			imagePath = Data.Buff.src;
			surceFlg = imagePath.indexOf(serchKey)!==-1?true:false;
		}
		//読込済みチェック
		if((!Data.flg)||(!surceFlg)){
			Data.Buff = new Image();
			Data.Buff.onload = function(){
				Data.flg = true;
			}
			Data.Buff.src = Data.Value;
		}
	}
	
	//幅設定
	this.setWidthHi = function(value){
		this.width_hi = value;
	}

	//縦設定
	this.setHeightHi = function(value){
		this.height_hi = value;
	}
	
	//幅設定
	this.setWidthLow = function(value){
		this.width_low = value;		
	}

	//縦設定
	this.setHeightLow = function(value){
		this.height_low = value;
	}
	//表示
	this.ViewCanvas = function(Key,target,x,y,size=1,clear=true){
		var Data = this.GetImage(Key);
		if(this.ImageData[target]==undefined){
			this.ImageData[target] = this.GetCtx(target);
		}
		var imageData = Data.Buff;
		if(clear)this.ImageData[target].clearRect(x, y, Data.Buff.width*size, Data.Buff.height*size);
		this.ImageData[target].drawImage(imageData,x,y,Data.Buff.width*size,Data.Buff.height*size);
	}
	//表示
	this.ViewCanvasWait = function(Key,target,x,y,size,clear=true){
		var Data = this.GetImage(Key);
		if(Data.flg&&(!this.ProcFlg)){
			this.ProcFlg = true;
			if(this.ImageData[target]==undefined){
				this.ImageData[target] = this.GetCtx(target);
			}
			var imageData = Data.Buff;
			if(clear)this.ImageData[target].clearRect(x, y, Data.Buff.width*size, Data.Buff.height*size);
			this.ImageData[target].drawImage(imageData,x,y,Data.Buff.width*size,Data.Buff.height*size);
			imageData = null;
			this.ProcFlg = false;
		}else{
			this.SetData[Key] = {
				"Key":Key,
				"target":target,
				"x":x,
				"y":y,
				"size":size,
			}
			this.SetData[Key].Key = Key;			
			this.SetData[Key].target = target;			
			this.SetData[Key].x = x;			
			this.SetData[Key].y = y;			
			this.setTimer(function(param){
				var ImgObj = param[1];
				Key = ImgObj.SetData[param[0]].Key;
				target = ImgObj.SetData[param[0]].target;
				x = ImgObj.SetData[param[0]].x;
				y = ImgObj.SetData[param[0]].y;
				size = ImgObj.SetData[param[0]].size;
				ImgObj.ViewCanvasWait(param[0],target,x,y,size,true);
			},100,[Key,this]);
		}
	}
	
	//コンテキスト
	this.GetCtx = function(key){
		//背景用
		var height_room = this.height_room;
		var width_room = this.width_room;
		var height = this.height_hi;
		var width = this.width_hi;		
		
		//キャンバス取得
		var image = document.getElementById(key);
		if ( ! image || ! image.getContext ) { return false; }
		
		//横幅最高値
		var widthMax = this.width_hi;

		//2次元画像指定&画像サイズ指定
		var ctx = image.getContext('2d');

		//キャンバス設定
		//if(window.innerHeight<=height){
		if(window.innerHeight<=height){
			image.height = window.innerHeight;
		}else{
			image.height = height;
		}
		if(window.innerWidth>=widthMax){
			image.width = widthMax;		
		}else{
			image.width = window.innerWidth;
		}
		image.globalAlpha = 1;
		image.globalCompositeOperation = "source-in";
		
		return ctx;
	}
	
}