////////////////////
//Touch情報
////////////////////
var NT_TOUCH = function(){this.initialize(arguments[0]);};
NT_TOUCH.prototype={
	sx:0,	// 開始X
	sy:0,	// 開始Y
	ex:0,	// 終了X
	ey:0,	// 終了Y
	mx:0,	// 移動X
	my:0,	// 移動Y
	dx:0,	// 基準点X
	dy:0,	// 基準点Y
	initialize:function(){
		if(arguments[0]!=undefined && arguments[0]!=null){
			this.sx = arguments[0].touch.pageX-arguments[0].rect.left;
			this.sy = arguments[0].touch.pageY-arguments[0].rect.top;
			this.dx = arguments[0].dx;
			this.dy = arguments[0].dy;
		}
		this.ex = 0;
		this.ey = 0;
		this.mx = 0;
		this.my = 0;
		this.callInitialize(arguments[0]);
	},
	// override
	callInitialize:function(){
	},
	setMove:function(){
		this.mx = this.sx - (arguments[0].touch.pageX-arguments[0].rect.left);
		this.my = this.sy - (arguments[0].touch.pageY-arguments[0].rect.top);
		this.ex = arguments[0].touch.pageX-arguments[0].rect.left;
		this.ey = arguments[0].touch.pageY-arguments[0].rect.top;
		this.callSetMove(arguments[0]);
	},
	// override
	callSetMove:function(){
	}
};

////////////////////
//TouchIF
////////////////////
var NT_TOUCHIF = function(){this.initialize(arguments[0]);};
NT_TOUCHIF.prototype={
	touches:null,		// touch情報
	prevtouch:null,	// 前回touch情報(ダブルタップ用)
	touching:false,		// 触れている判定
	gesturing:false,	// ジェスチャー中判定
	scale:null,
	rotation:null,
	initialize:function(){
		this.touches = new Array();
		if(arguments[0]!=undefined && arguments[0]!=null){
			this.callInitialize(arguments[0]);
		}
	},
	// override
	callInitialize:function(){
	},
	touchStartHandle:function(e){
		e.preventDefault();
		this.callTouchStart(e);
	},
	// override
	callTouchStart:function(){
	},
	touchMoveHandle:function(e){
		e.preventDefault();
		// move情報設定
		for(var n=0; n<e.touches.length&&n<this.touches.length; n++){
			this.touches[n].setMove({'touch':e.touches[n],'rect':this.mapElm.getBoundingClientRect()});
		}
		this.callTouchMove(e);
	},
	// override
	callTouchMove:function(){
	},
	touchEndHandle:function(e){
		e.preventDefault();
		this.callTouchEnd(e);
		// touchオブジェクト再生成
		delete this.touches;
		this.touches = new Array();
		this.touching = false;
	},
	// override
	callTouchEnd:function(){
	},
	touchCancelHandle:function(e){
		e.preventDefault();
		this.callTouchCancel(e);
		// touchオブジェクト再生成
		delete this.touches;
		this.touches = new Array();
		this.touching = false;
	},
	// override
	callTouchCancel:function(){
	},
	gestureStartHandle:function(e){
		e.preventDefault();
		this.gesturing = true;
		this.scale = 1;
		this.rotation = 0;
		this.callGestureStart();
	},
	// override
	callGestureStart:function(){
	},
	gestureChangeHandle:function(e){
		e.preventDefault();
		this.scale = e.scale;
		this.rotation = e.rotation;
		this.callGestureChange();
	},
	//override
	callGestureChange:function(){
	},
	gestureEndHandle:function(e){
		e.preventDefault();
		this.scale = e.scale;
		this.rotation = e.rotation;
		this.callGestureEnd();
	},
	//override
	callGestureEnd:function(){
	}
};

////////////////////
//地図Touch情報
////////////////////
NT_TOUCH.MapTouch = function(){this.initialize(arguments[0]);};
NT_TOUCH.MapTouch.prototype = new NT_TOUCH();
// 移動後処理
NT_TOUCH.MapTouch.prototype.afterMove = function(){
	this.sx = this.ex;
	this.sy = this.ey;
};

////////////////////
// Touch地図IF
////////////////////
NT_TOUCHIF.MapIF = function(){this.initialize(arguments[0]);};
NT_TOUCHIF.MapIF.prototype = new NT_TOUCHIF();
NT_TOUCHIF.MapIF.prototype.mapElm = null;		// 地図エレメント
NT_TOUCHIF.MapIF.prototype.mapObj = null;		// 地図jsオブジェクト
NT_TOUCHIF.MapIF.prototype.zoomGestureMin = 5;	// zoom時の最小gesture閾値(%で指定)
NT_TOUCHIF.MapIF.prototype.doubleTapZoom = false;	// ダブルタップイベントで地図を拡大するかのON/OFF
NT_TOUCHIF.MapIF.prototype.doubleTapMove = false;	// ダブルタップイベントで地図を移動するかのON/OFF（doubleTapZoomもtrueだった場合はdoubleTapZoomを優先）
NT_TOUCHIF.MapIF.prototype.doubleTapTolerance = 20;	// ダブルタップの許容範囲

NT_TOUCHIF.MapIF.prototype.callInitialize = function(){
	this.mapElm = arguments[0].mapElm;
	this.mapObj = arguments[0].mapObj;
	this.zoomGestureMin = arguments[0].zoomGestureMin;
	if(arguments[0].doubleTapZoom != null && arguments[0].doubleTapZoom != undefined){
		this.doubleTapZoom = arguments[0].doubleTapZoom;
	}
	if(arguments[0].doubleTapMove != null && arguments[0].doubleTapMove != undefined){
		this.doubleTapMove = arguments[0].doubleTapMove;
	}
	if(arguments[0].doubleTapTolerance != null && arguments[0].doubleTapTolerance != undefined){
		this.doubleTapTolerance = arguments[0].doubleTapTolerance;
	}
};

NT_TOUCHIF.MapIF.prototype.callTouchStart = function(e){
	// touchオブジェクト生成
	this.touching = true;
	for(var n=this.touches.length; n < e.touches.length; n++){
		// touch位置情報の格納
		this.touches.push(new NT_TOUCH.MapTouch({'touch':e.touches[n],'dx':this.mapObj.getMapPlate().offsetLeft,'dy':this.mapObj.getMapPlate().offsetTop,'rect':this.mapElm.getBoundingClientRect()}));
	}

};

NT_TOUCHIF.MapIF.prototype.callTouchMove = function(e){

	if(e.touches.length == 1){
		// 地図移動
		var stl = this.mapObj.getMapPlate().style;
		stl.left = this.touches[0].dx - this.touches[0].mx + "px";
		stl.top = this.touches[0].dy - this.touches[0].my + "px";
	}
};

NT_TOUCHIF.MapIF.prototype.callTouchEnd = function(e){

	if(this.touches.length == 1 && (this.touches[0].mx != 0 || this.touches[0].my != 0)){
		// 地図移動後処理(未使用)
		this.touches[0].afterMove();
		// 地図の移動(地図画像の読み込み)
		this.mapObj.moveTo(NTGeoUtil.pixel2LonLat(new NTSize(this.touches[0].mx,this.touches[0].my),this.mapObj.getStatus()));
	}

	//ダブルタップ時の操作
	if(this.prevtouch !=null && Math.abs(this.prevtouch.sx-this.touches[0].sx) < this.doubleTapTolerance && Math.abs(this.prevtouch.sy-this.touches[0].sy) < this.doubleTapTolerance){

		if (this.doubleTapZoom || this.doubleTapMove) {


			 if (this.doubleTapZoom) {

				if (this.touches.length == 1 && this.prevtouch != null) {
					//地図拡大処理
					this.mapObj.exeZoom(1);
					this.prevtouch = null;
				}
			}else if (this.doubleTapMove) {
				var x = this.touches[0].sx;
				var y = this.touches[0].sy;
				x -=this.mapElm.offsetWidth/2;
				y -=this.mapElm.offsetHeight/2;
                                this.mapObj.moveTo(NTGeoUtil.pixel2LonLat(new NTSize(x,y),this.mapObj.getStatus()));
			}
		}
	}


	this.prevtouch = {
		sx : this.touches[0].sx,
		sy : this.touches[0].sy,
		ex : this.touches[0].ex,
		ey : this.touches[0].ey,
		mx : this.touches[0].mx,
		my : this.touches[0].my,
		dx : this.touches[0].dx,
		dy : this.touches[0].dy
	};

	//前回touch情報クリア関数
	var clearFunc = (function(self){
		return function(){
			self.prevtouch = null;
		}
	})(this);

	setTimeout(clearFunc, 300);

};

NT_TOUCHIF.MapIF.prototype.callGestureEnd = function(e){
	if(this.touches.length == 2 && this.gesturing){
		// 拡大縮小処理

		// 開始位置取得
		var sTop = this.touches[0].sy<=this.touches[1].sy?this.touches[0].sy:this.touches[1].sy;
		var sBottom = this.touches[0].sy<this.touches[1].sy?this.touches[1].sy:this.touches[0].sy;
		var sLeft = this.touches[0].sx<=this.touches[1].sx?this.touches[0].sx:this.touches[1].sx;
		var sRight = this.touches[0].sx<this.touches[1].sx?this.touches[1].sx:this.touches[0].sx;

		// 終了位置取得
		var eTop = this.touches[0].ey<=this.touches[1].ey?this.touches[0].ey:this.touches[1].ey;
		var eBottom = this.touches[0].ey<this.touches[1].ey?this.touches[1].ey:this.touches[0].ey;
		var eLeft = this.touches[0].ex<=this.touches[1].ex?this.touches[0].ex:this.touches[1].ex;
		var eRight = this.touches[0].ex<this.touches[1].ex?this.touches[1].ex:this.touches[0].ex;

		// ズーム中心
		var centerY = eTop + ((eBottom - eTop) / 2);
		var centerX = eLeft + ((eRight - eLeft) / 2);

		// ズームup/down判定
		var ZoomVal =  0;
		if(this.scale==1){
			return;
		}else if(this.scale>1){
			// 拡大
			ZoomVal =  Math.floor(this.scale);
		}else{
			// 縮小

			// 移動値取得
			var YZoomVal = sBottom-sTop>eBottom-sTop?sBottom-eTop-(eBottom-eTop):eBottom-eTop-(sBottom-sTop);
			var XZoomVal =  sRight-sLeft>eRight-eLeft?sRight-sLeft-(eRight-eLeft):eRight-eLeft-(sRight-sLeft);

			// 縮小時の移動距離
			var zmv = YZoomVal>=XZoomVal?YZoomVal:XZoomVal;
			// 地図に対しての比率
			var zr = YZoomVal>=XZoomVal?zmv/this.mapElm.offsetHeight:zmv/this.mapElm.offsetWidth;

			if(zr < (this.zoomGestureMin /100)){
				// gestureが閾値をこえていない場合
				return;
			}
			// 拡大縮小値変換
			ZoomVal = Math.ceil(zr * 4)*-1;
		}

		// ズーム後の中心地点の設定
		this.mapObj.changeCenterPosition(Math.floor(centerX - this.mapElm.offsetWidth/2),Math.floor(centerY - this.mapElm.offsetHeight/2));


		// ズーム位置の設定
		var pos = new NTSize(Math.floor(centerX),Math.floor(centerY));
		this.mapObj.getStatus().setTouchPos(pos);

		// 拡大縮小値のチューニング
		var animate = false;
		var addZoomVal = 0;
		if(ZoomVal > 0){
			addZoomVal = Math.ceil(ZoomVal/4);
		}else if(ZoomVal < 0){
			addZoomVal = ZoomVal;
		}else{
			return false;
		}
		// 拡大縮小実行
		this.mapObj.exeZoom(addZoomVal);
	}

	this.gesturing = false;

};

