// 距離計測.
function MeasureLine()
{
	this.LinePath = "";
	var drawFlag = true;
	this.X = [];
	this.Y = [];
	this.Result = 0;
	
	// ポイント追加.
	this.AddPoint=function()
	{
		if (!drawFlag)
		{
			this.Clear();
		}
		
		if (this.LinePath != "")
		{
			this.LinePath += " ";
		}
		this.LinePath +=  currentDocument.ActiveMouseX + "," + (currentDocument.ActiveMouseY - parseInt(document.getElementById("map").style.top));
		
		this.X.push(mapImage.MapX);
		this.Y.push(mapImage.MapY);
		
		document.getElementById("MeasureResult").style.visibility = "visible";
	}

	// 計測確定処理.
	this.Commit = function()
	{
		drawFlag = false;
		
		document.getElementById("MeasureResult").style.top =  parseInt(mapImage.GetMapPosYToPixcelPosY(this.Y[this.X.length-1])) -25 + parseInt(document.getElementById("map").style.top);
		document.getElementById("MeasureResult").style.left =  parseInt(mapImage.GetMapPosXToPixcelPosX(this.X[this.X.length-1]));
		if(document.all)
		{
			// IE版.
			html = "<v:polyline style='position:absolute;top: 0; left: 0; width: 1280px; height: 997px;' ";
			html += " points='" + this.LinePath + "'";
			html += " strokecolor='blue' strokeweight='2' filled = 'f'/>";
			document.getElementById("MeasureFigureArea").innerHTML = html;
		}
		else
		{
			// Fox版.
			var canvas = document.getElementById('MeasureFigureCanvas').getContext('2d');
			canvas.clearRect(0,0,800,800);
			canvas.strokeStyle = "rgba(0,0,255,1)";
			canvas.lineWidth = 3;
			canvas.beginPath();
			
			for(var i = 0; i < this.X.length; i++)
			{
				posX = parseInt(mapImage.GetMapPosXToPixcelPosX(this.X[i]));
				posY = parseInt(mapImage.GetMapPosYToPixcelPosY(this.Y[i]));
			
				if (i == 0)
				{
					canvas.moveTo(posX,posY);
				}
				else
				{
					canvas.lineTo(posX,posY);
				}
			}
			canvas.stroke();
			
			document.getElementById("MeasureResult").style.top =  parseInt(mapImage.GetMapPosYToPixcelPosY(this.Y[this.X.length-1])) -25 + parseInt(document.getElementById("map").style.top);
			document.getElementById("MeasureResult").style.left =  parseInt(mapImage.GetMapPosXToPixcelPosX(this.X[this.X.length-1]));
		}
		
		this.Result = Math.round(this.measure(this.X, this.Y));
		document.getElementById("MeasureResult").innerHTML = this.Result + " ｍ";
	}
	
	// 計測削除.
	this.Clear = function()
	{
		drawFlag = true;
		
		if(document.all)
		{
			// IE版.
			document.getElementById("MeasureFigureArea").innerHTML = "";
		}
		else
		{
			// Fox版.
			var canvas = document.getElementById('MeasureFigureCanvas').getContext('2d');
			canvas.clearRect(0,0,800,800);
		}
		
		document.getElementById("MeasureResult").style.visibility = "hidden";
		this.LinePath = "";
		this.X = [];
		this.Y = [];
		this.Result = 0;
	}
	
	// 再描画.
	this.Redraw = function()
	{
		if(document.all)
		{
			// IE版.
			if (this.LinePath != "")
			{
				this.LinePath = "";
				for(var i = 0; i < this.X.length; i++)
				{
					if (this.LinePath != "")
					{
						this.LinePath += " ";
					}
					this.LinePath +=  parseInt(mapImage.GetMapPosXToPixcelPosX(this.X[i])) + "," + parseInt(mapImage.GetMapPosYToPixcelPosY(this.Y[i]));
				}
				
				html = "<v:polyline style='position:absolute;top:0; left: 0; width: 1280px; height: 997px;' ";
				html += " points='" + this.LinePath + "'";
				if (drawFlag)
				{
					html += " filled = 'f'>";
					html += "<v:stroke Color='red' DashStyle='Dot' Weight='3' />";
					html += " </v:polyline>";
				}
				else
				{
					html += " strokecolor='blue' strokeweight='2' filled = 'f'/>";
				}
				document.getElementById("MeasureFigureArea").innerHTML = html;
				
				document.getElementById("MeasureResult").style.top =  parseInt(mapImage.GetMapPosYToPixcelPosY(this.Y[this.X.length-1])) -25 + parseInt(document.getElementById("map").style.top);
				document.getElementById("MeasureResult").style.left =  parseInt(mapImage.GetMapPosXToPixcelPosX(this.X[this.X.length-1]));
				
			}
		}
		else
		{
			// Fox版.
			var canvas = document.getElementById('MeasureFigureCanvas').getContext('2d');
			canvas.clearRect(0,0,800,800);
			
			canvas.beginPath();
			
			for(var i = 0; i < this.X.length; i++)
			{
				posX = parseInt(mapImage.GetMapPosXToPixcelPosX(this.X[i]));
				posY = parseInt(mapImage.GetMapPosYToPixcelPosY(this.Y[i]));
			
				if (i == 0)
				{
					canvas.moveTo(posX,posY);
				}
				else
				{
					canvas.lineTo(posX,posY);
				}
			}
			
			if (drawFlag)
			{
				canvas.lineWidth = 2;
				canvas.strokeStyle = "rgba(255,0,0,1)";
			}
			else
			{
				canvas.lineWidth = 2;
				canvas.strokeStyle = "rgba(0,0,255,1)";
			}
			
			canvas.stroke();
			
			document.getElementById("MeasureResult").style.top =  parseInt(mapImage.GetMapPosYToPixcelPosY(this.Y[this.X.length-1])) -25 + parseInt(document.getElementById("map").style.top);
			document.getElementById("MeasureResult").style.left =  parseInt(mapImage.GetMapPosXToPixcelPosX(this.X[this.X.length-1]));
		}
	}
	
	// 描画.
	this.Draw = function()
	{
		if (!drawFlag) return;
		
		if (this.LinePath.length > 0)
		{
			document.getElementById("MeasureResult").style.top =  currentDocument.ActiveMouseY - 25;
			document.getElementById("MeasureResult").style.left =  currentDocument.ActiveMouseX;
		}
		
		if(document.all)
		{
			// IE版.
			html = "<v:polyline style='position:absolute;top: 0; left: 0; width: 1280px; height: 997px;' ";
			html += " points='" + this.LinePath + " " + currentDocument.ActiveMouseX + "," + (currentDocument.ActiveMouseY - parseInt(document.getElementById("map").style.top)) + "'";
			html += " filled = 'f'>";
			html += "<v:stroke Color='red' DashStyle='Dot' Weight='3' />";
			html += " </v:polyline>";
			document.getElementById("MeasureFigureArea").innerHTML = html;
		}
		else
		{
			// Fox版.
			var canvas = document.getElementById('MeasureFigureCanvas').getContext('2d');
			canvas.clearRect(0,0,800,800);
			canvas.strokeStyle = "rgba(255,0,0,1)";
			canvas.lineWidth = 2;
			canvas.beginPath();
			
			for(var i = 0; i < this.X.length; i++)
			{
				posX = parseInt(mapImage.GetMapPosXToPixcelPosX(this.X[i]));
				posY = parseInt(mapImage.GetMapPosYToPixcelPosY(this.Y[i]));
			
				if (i == 0)
				{
					canvas.moveTo(posX,posY);
				}
				else
				{
					canvas.lineTo(posX,posY);
				}
			}
			canvas.lineTo(currentDocument.ActiveMouseX,(currentDocument.ActiveMouseY - parseInt(document.getElementById("map").style.top)));
			canvas.stroke();
		}
		
		var workX = [];
		var workY = [];
		
		for (var i = 0; i < this.X.length; i++) 
		{
			workX.push(this.X[i]);
			workY.push(this.Y[i]);
		}
		
		workX.push(mapImage.MapX);
		workY.push(mapImage.MapY);
		
//		this.Result = Math.round(this.measure(workX, workY));
		document.getElementById("MeasureResult").innerHTML = Math.round(this.measure(workX, workY)) + " ｍ";
	}
	
	// 面積計算.
	// x				x座標.
	// y				y座標.
	this.measure = function(x, y)
	{
		var distance = 0;
		for (var i = 1; i < x.length; i++) {
			// 辺の距離計算

			one = Math.sqrt((x[i-1] - x[i]) * (x[i-1] - x[i])
					 + (y[i-1] - y[i]) * (y[i-1] - y[i]));
			distance += one;
		}
		return distance;
	}
	
	// 一つ前に描画した点を削除する.
	this.DelPoint = function()
	{
		if (!drawFlag)
		{
			return;
		}
		
		if (this.X.length > 1)
		{
			this.X.pop();
			this.Y.pop();
			this.Redraw();
			this.Draw();
		}
		else
		{
			this.Clear();
		}
	}
	
}
