// 面積計測.
function MeasureArea()
{
	this.LinePath = "";
	var drawFlag = true;
	this.X = [];
	this.Y = [];
	this.Result = 0;
	
	// ポイント追加.
	this.AddPoint=function()
	{
		if (!drawFlag)
		{
			this.Clear();
		}
		
		if (this.LinePath == "")
		{
			this.LinePath = "m" + currentDocument.ActiveMouseX  + "," + (currentDocument.ActiveMouseY - parseInt(document.getElementById("map").style.top))+ "l";
		}
		else
		{
			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:shape style='position:absolute;top: 0; left: 0; width: 1280px; height: 997px;' stroked='t' filled='f' strokecolor='blue' strokeweight='2' fillcolor='#ffcccc' coordsize='1280px,997px' coordorigin='0,0'>";
			html += " <v:path v='";
			html +=  this.LinePath;
			html += " x e'/>";
			html += " <v:fill id='fset' color='blue' opacity='30%' on='t' />";
			html += "</v:shape>";
			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.fillStyle = "rgba(0,0,255,0.3)";
			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.closePath();
			canvas.fill();
			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)
		{
			document.getElementById("MeasureFigureArea").innerHTML = "";
		}
		else
		{
			var canvas = document.getElementById('MeasureFigureCanvas').getContext('2d');
			canvas.clearRect(0,0,800,800);
		}
		
		document.getElementById("MeasureResult").style.visibility = "hidden";
		this.LinePath = "";
		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 (i == 0)
					{
						this.LinePath = "m" + parseInt(mapImage.GetMapPosXToPixcelPosX(this.X[i]))  + "," + parseInt(mapImage.GetMapPosYToPixcelPosY(this.Y[i])) + "l";
					}
					else
					{
						this.LinePath += " " + parseInt(mapImage.GetMapPosXToPixcelPosX(this.X[i]))  + "," + parseInt(mapImage.GetMapPosYToPixcelPosY(this.Y[i]));
					}
				}
				
				if (drawFlag)
				{
					// 描画中.
					html = "<v:shape style='position:absolute;top: 0; left: 0; width: 1280px; height: 997px;'  fillcolor='#ffcccc' coordsize='1280px,997px' coordorigin='0,0'>";
					html += " <v:path v='";
					html +=  this.LinePath;
					html += " x e'/>";
					html += " <v:fill id='fset' color='red' opacity='30%' on='t' />";
					html += "<v:stroke Color='red' DashStyle='Dot' Weight='3' />";
					html += "</v:shape>";
				}
				else
				{
					// 描画完了.
					html = "<v:shape style='position:absolute;top: 0; left: 0; width: 1280px; height: 997px;' stroked='t' filled='f' strokecolor='blue' strokeweight='2' fillcolor='#ffcccc' coordsize='1280px,997px' coordorigin='0,0'>";
					html += " <v:path v='";
					html +=  this.LinePath;
					html += " x e'/>";
					html += " <v:fill id='fset' color='blue' opacity='30%' on='t' />";
					html += "</v:shape>";
				}
				
				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.strokeStyle = "rgba(255,0,0,1)";
			canvas.fillStyle = "rgba(255,0,0,0.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);
				}
			}
			
			if (drawFlag)
			{
				canvas.lineWidth = 2;
				canvas.strokeStyle = "rgba(255,0,0,1)";
				canvas.fillStyle = "rgba(255,0,0,0.3)";
			}
			else
			{
				canvas.lineWidth = 2;
				canvas.strokeStyle = "rgba(0,0,255,1)";
				canvas.fillStyle = "rgba(0,0,255,0.3)";
			}
			
			canvas.closePath();
			canvas.fill();
			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:shape style='position:absolute;top: 0; left: 0; width: 1280px; height: 997px;'  fillcolor='#ffcccc' coordsize='1280px,997px' coordorigin='0,0'>";
			html += " <v:path v='";
			html +=  this.LinePath + " " + currentDocument.ActiveMouseX  + "," + (currentDocument.ActiveMouseY-parseInt(document.getElementById("map").style.top));
			html += " x e'/>";
			html += " <v:fill id='fset' color='red' opacity='30%' on='t' />";
			html += "<v:stroke Color='red' DashStyle='Dot' Weight='3' />";
			html += "</v:shape>";
			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.fillStyle = "rgba(255,0,0,0.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.lineTo(currentDocument.ActiveMouseX,(currentDocument.ActiveMouseY - parseInt(document.getElementById("map").style.top)));
			canvas.closePath();
			canvas.fill();
			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 result = 0;
		
		for (var i = 0; i < x.length - 1; i++) 
		{
			result += (x[i] * y[i+1] - y[i] * x[i+1]);
		}
		result += (x[x.length -1] * y[0] - y[y.length -1] * x[0]);
		
		result = Math.abs(result / 2);
		
		return result;
	}
	
	// 一つ前に描画した点を削除する.
	this.DelPoint = function()
	{
		if (!drawFlag)
		{
			return;
		}
		
		if (this.X.length > 1)
		{
			this.X.pop();
			this.Y.pop();
			this.Redraw();
			this.Draw();
		}
		else
		{
			this.Clear();
		}
	}
}
