// 計測制御クラス.
function MeasureFigure()
{
	var drawFigure = null;				// 計測クラス.
	var mode = ""									// モード(MeasureLine　MeasureArea).
	this.X = null;								// X.
	this.Y = null;								// Y.
	this.Result = 0;							// 結果.
	this.Mode = -1;								// モード.
	
	// 初期化.
	// id				id.
	this.Initialize = function(id)
	{
		measureFigure.Mode = -1;
		switch(id)
		{
			case common.MODELINE:
				drawFigure = new MeasureLine();
				help.defaultmsg = help.distanceOvermsg;
				measureFigure.Mode = 0;
				break;
			case common.MODEAREA:
				drawFigure = new MeasureArea();
				help.defaultmsg = help.areaOvermsg;
				measureFigure.Mode = 1;
				break;
		}
		mode = id;
		document.getElementById(mode + "Button").src = "MapForm/img/" + mode + "_select.gif";
		document.getElementById("MeasureResult").style.top = "-1000px";
		document.getElementById("MeasureResult").style.left = "-1000px";
	}
	
	// マウスクリックイベント.
	this.onClick = function()
	{
		if (drawFigure == null) return;
		drawFigure.AddPoint();
	}
	
	// マウスムーブイベント.
	this.onMouseMove = function()
	{
		if (drawFigure == null) return;
	
		screen.updateInterval = 1000;
		drawFigure.Draw();
		screen.updateInterval = 0;
		
		measureFigure.X = ((drawFigure != null) ? drawFigure.X : null);
		measureFigure.Y = ((drawFigure != null) ? drawFigure.Y : null);
		measureFigure.Result = ((drawFigure != null) ? drawFigure.Result : 0);
		
		measureFigure.SetCursor();
	}
	
	// 描画完了.
	this.Commit = function()
	{
		if (drawFigure == null) return;
		drawFigure.Commit();
		
		measureFigure.X = ((drawFigure != null) ? drawFigure.X : null);
		measureFigure.Y = ((drawFigure != null) ? drawFigure.Y : null);
		measureFigure.Result = ((drawFigure != null) ? drawFigure.Result : 0);
	}

	// 再描画.	
	this.Redraw = function()
	{
		if (drawFigure == null) return;
		drawFigure.Redraw();
	}
	
	// クリア.
	this.Clear = function()
	{
		if (drawFigure == null) return;
		drawFigure.Clear();
		document.getElementById("MeasureResult").innerHTML = "";
		
		measureFigure.X = null;
		measureFigure.Y = null;
		measureFigure.Result = 0;
	}
	
	// 処理開始.
	// id				モード.
	this.Start = function(id)
	{
		measureFigure.Clear();
		
		document.getElementById(common.MODELINE + "Button").src = "MapForm/img/" + common.MODELINE + ".gif";
		document.getElementById(common.MODEAREA + "Button").src = "MapForm/img/" + common.MODEAREA + ".gif";
		
		if (mode == id)
		{
			measureFigure.End();
			return;
		}
		memoController.End();
		mapImage.MapMode = common.MODEMEASURE;
		measureFigure.Initialize(id);
		measureFigure.SetCursor();
	}
	
	// カーソル設定.
	this.SetCursor = function()
	{
		switch(mode)
		{
			case common.MODELINE:
				document.getElementById("dragImage").style.cursor ="url(MapForm/img/MeasureLength.cur), default";
				break;
			case common.MODEAREA:
				document.getElementById("dragImage").style.cursor ="url(MapForm/img/MeasureArea.cur), default";
				break;
		}
	}
	
	// 計測終了.
	this.End = function()
	{
		measureFigure.Clear();
		drawFigure = null;
		document.getElementById(mode + "Button").src = "MapForm/img/" + mode + ".gif";
		
		mode = "";
		mapImage.MapMode = "";
		
		document.getElementById("dragImage").style.cursor ="url(MapForm/img/BeforePanHand.cur), move";
		
		document.getElementById("MeasureResult").style.top = "-1000px";
		document.getElementById("MeasureResult").style.left = "-1000px";
		help.ShowDefault();
	}
	
	// キープレスENTER.
	this.KeyPressEnter = function()
	{
		if (drawFigure == null) return;
		switch(mode)
		{
			case common.MODELINE:
				if (drawFigure.X.length > 1)
				{
					measureFigure.Commit();
				}
				break;
			case common.MODEAREA:
				if (drawFigure.X.length > 2)
				{
					measureFigure.Commit();
				}
				break;
		}
	}
	
	// キープレスESC.
	this.KeyPressESC = function()
	{
		if (drawFigure == null) return;
		if (drawFigure.X != null && drawFigure.X.length > 0)
		{
			measureFigure.Clear();
		}
		else
		{
			measureFigure.End();
		}
	}

	// キープレスBack Del.
	this.KeyPressBack_Del = function()
	{
		if (drawFigure == null) return;
		drawFigure.DelPoint();
	}
}
