﻿function DistanceMeter(graphics, dMeterPerPixelX, dMeterPerPixelY, parentDiv, pDistancePolyLine)
{
	var me = this;
	
	
	this.iMaxRubberLength = 750;
	this.iDistPoints = 0;
	this.iDistance = 0;
	this.iDistX = 0; // adott zoom szinten a gumivonal kiindulopontja (grid koord)
	this.iDistY = 0;
	this.iLastMoveX = 0;
	this.iLastMoveY = 0;

	this.graphics = graphics;
	this.rubberLine = null;
	this.cursorParent = parentDiv;
	this.cursorList = new Array();
	
	// properties
	this.divDisplay = null;
	this.dMeterPerPixelX = dMeterPerPixelX;
	this.dMeterPerPixelY = dMeterPerPixelY;
	this.pPolyLine = pDistancePolyLine;
	
	this.iMoveToX = 0;
	this.iMoveToY = 0;
	this.bMoving = false;
	
	
	// events
	this.onInit = null; // function() {};
	this.onMove = null; // function (iX, iY) {};
	this.onClose = null; // function() {};
	this.DisplayLength = null; // function(iTotal, iLast) {};

	

	this.Init = function()
	{
		me.iDistPoints = 0;
		me.iDistance= 0;

		if(me.onInit)
			me.onInit();

		me.CreateDistCursor("imgDistCursor", 0, 0, false);
		
//		if(me.DisplayLength)
//			me.DisplayLength(me.pPolyLine.dDistanceMeter,0);
	}

	this.Close = function()
	{
	    me.ClearRubberLine ();
	    me.ClearCursors ();   
		
		if(me.onClose)
			me.onClose();
	}

	this.GetLastDistance = function(iX, iY)
	{
	//	return Math.sqrt(Math.pow((iX-me.iDistX)*me.dMeterPerPixelX, 2)+Math.pow((iY-me.iDistY)*me.dMeterPerPixelY,2) );
	    var x = (iX-me.iDistX)*me.dMeterPerPixelX / 1000;
	    var y = (iY-me.iDistY)*me.dMeterPerPixelY / 1000;
	    var x2 = Math.pow(x, 2);
	    var y2 = Math.pow(y, 2);	    
	    var res = Math.sqrt(x2+y2) * 1000;
	    return res;
	}
	
	this.AddDistPoint = function(iX, iY)
	{
        ++me.iDistPoints;
		if(me.iDistPoints > 1)
		{
			me.graphics.drawLine(me.iDistX, me.iDistY, iX, iY, 2, true);
			me.iDistance += me.GetLastDistance(iX, iY);
		}
		me.iLastMoveX = me.iDistX = iX;
		me.iLastMoveY = me.iDistY = iY;
		
	}
	
	this.CreateDistCursor = function(id, iX, iY, bShow)
	{
		var img = NewTag("img", me.cursorParent);
		me.cursorList[me.cursorList.length] = img;
		img.setAttribute("id", id);
		img.setAttribute("class", "distcursor");
		if(!bShow)
			img.style.visibility = "hidden";
		img.style.position = "absolute";
		SetImage(img, GetAbsUrl("images/mapctrl/cross.gif"), 15, 15);
		img.style.left = (iX-8) + "px";
		img.style.top  = (iY-8) + "px";
	}
	
	this.iCounter = 0;
	
	this.MoveDistCursor = function(iX, iY)
	{
		me.iMoveToX = iX;
		me.iMoveToY = iY;

		if (me.bMoving == false)
		{
			me.bMoving = true;
			wsto_id = window.setTimeout(me.MoveDistCursorMain, 25);
		}
		//me.MoveDistCursorMain();
	}


	// tul hosszu vonalat nem szabad rajzolni a sebessegromlas miatt !!!

	this.GetDistXY = function (iLimitL)
	{
		var Len =  Math.sqrt((me.iLastMoveX - me.iDistX) * (me.iLastMoveX - me.iDistX) +
		                     (me.iLastMoveY - me.iDistY) * (me.iLastMoveY - me.iDistY));

		var vex = (me.iLastMoveX - me.iDistX) / Len;
		var vey = (me.iLastMoveY - me.iDistY) / Len;

		if (Len > iLimitL)
			return [me.iLastMoveX - iLimitL * vex, me.iLastMoveY - iLimitL * vey];
		else
			return [me.iDistX, me.iDistY];
		// ------------------------------------------------------------------
	}
	
	this.MoveDistCursorMain = function()
	{
		iX = me.iMoveToX;
		iY = me.iMoveToY;

		if(iX == me.iLastMoveX && iY == me.iLastMoveY)
		{
			me.bMoving = false;
			return;
		}

		me.iLastMoveX = iX;
		me.iLastMoveY = iY;

		if (me.rubberLine)
			me.graphics.removeShape(me.rubberLine);
		if(me.onMove)
			me.onMove(iX, iY);

		var id="imgDistCursor";
		var img = GetBlock(id);
		if(img)
		{
			img.style.visibility = "visible";
			img.style.left = (iX-8) + "px";
			img.style.top  = (iY-8) + "px";
		}
		
		if(me.iDistPoints > 0)
		{
			me.graphics.penColor = "blue";

			var drawDistXY = me.GetDistXY (me.iMaxRubberLength);
			me.rubberLine = me.graphics.drawLine(drawDistXY[0], drawDistXY[1], me.iLastMoveX, me.iLastMoveY, 2, true);
			var lastDist = me.GetLastDistance(iX, iY);
			if(me.DisplayLength)
				me.DisplayLength(me.iDistance + me.pPolyLine.dDistanceMeter + lastDist, lastDist);
		}
		
		me.bMoving = false;
	}

	// ha zoomol a felhasznalo, akkor mas pontbol kell, hogy
	// kiinduljon a gumivonal
	// ebben a tavolsagmeres verzioban nincsen fix vonal, csak egy gumivonal
	// a kiindulasi pontban itt nincs kep

	this.RefreshDistCursor = function(iNewStartX, iNewStartY, iNewEndX, iNewEndY)
	{
		if (me.rubberLine)
		{
			me.iDistX = iNewStartX;
			me.iDistY = iNewStartY;
			me.iLastMoveX = iNewEndX;
			me.iLastMoveY = iNewEndY;
			me.graphics.removeShape(me.rubberLine);
			me.graphics.penColor = "blue";

			var drawDistXY = me.GetDistXY (me.iMaxRubberLength);
			me.rubberLine = me.graphics.drawLine (drawDistXY[0], drawDistXY[1], me.iLastMoveX, me.iLastMoveY, 2, true);

			var id="imgDistCursor";
			var img = GetBlock(id);
			if(img)
			{
				img.style.visibility = "visible";
				img.style.left = (iNewEndX-8) + "px";
				img.style.top  = (iNewEndY-8) + "px";
			}

		}
	}

	this.SetEndPoint = function (iNewEndX, iNewEndY)
	{
		if (me.rubberLine)
		{
			me.iLastMoveX = iNewEndX;
			me.iLastMoveY = iNewEndY;
			me.graphics.removeShape(me.rubberLine);
			me.graphics.penColor = "blue";

			var drawDistXY = me.GetDistXY (me.iMaxRubberLength);
			me.rubberLine = me.graphics.drawLine(drawDistXY[0], drawDistXY[1], me.iLastMoveX, me.iLastMoveY, 2, true);

			var id="imgDistCursor";
			var img = GetBlock(id);
			if(img)
			{
				img.style.visibility = "visible";
				img.style.left = (iNewEndX-8) + "px";
				img.style.top  = (iNewEndY-8) + "px";
	}
}
	}

	this.SetPixelDensity = function (D)
	{
		me.dMeterPerPixelX = D;
		me.dMeterPerPixelY = D;
	}

	this.ClearRubberLine = function ()
	{
	if (me.rubberLine)
		me.graphics.clear();
	}

	this.ClearCursors = function ()
{
		for(var i=0; i<me.cursorList.length; i++)
		{
			if(me.cursorList[i] != null)
			{
				me.cursorList[i].parentNode.removeChild(me.cursorList[i]);
				me.cursorList[i] = null;
			}
		}
	}
}

//----------------------------------------------------------------------------------

function DistanceDisplay(tmTopoliszMap)
{
	this.tmTopoliszMap = tmTopoliszMap;
	var parentDiv = tmTopoliszMap.pGridMain.mainGridDiv;
	this.parentDiv = parentDiv;
	this.mainDiv = NewTag("div", parentDiv);
	this.mainDiv.className = "distpanel";
	this.mainDiv.style.visibility = "hidden";
	this.distTr = null;
	this.exitTr = null;

	var table, tr, td;
	
	table = NewTag("table", this.mainDiv);
	table.cellPadding = "0";
	table.cellSpacing = "0";
	table = NewTag("tbody", table);

	tr = NewTag("tr", table);
	this.distTr = tr;

	td = NewTag("td", tr);
	td.className = "distcaption";
	NewTextTag(g_Babel.GetWord('MeasureDistance', "Távolságmérés")+":", td);
	td.style.paddingRight = "5px";
	td.style.paddingLeft = "5px";


	td = NewTag("td", tr);
	td.className = "distvalue";
	td.style.whiteSpace = "nowrap";
	this.displayLastDist = td;
	td.style.paddingRight = "5px";
	NewTextTag("0 km", this.displayLastDist);

	tr = NewTag("tr", table);

	td = NewTag("td", tr);
	td.className = "distcaption";
	NewTextTag(g_Babel.GetWord('MeasureTotal', "Összesen")+":", td);
	td.style.paddingRight = "5px";
	td.style.paddingLeft = "5px";


	td = NewTag("td", tr);
	td.className = "distvalue";
	td.style.whiteSpace = "nowrap";
	this.displaySumDist = td;
	td.style.paddingRight = "5px";
	NewTextTag("0 km", this.displaySumDist);

	tr = NewTag("tr", table);
	this.exitTr = tr;


	td = NewTag("td", tr);
	td.className = "distcaption";
	NewTextTag(g_Babel.GetWord('ExitDistanceMeasuring', "Kilépéshez Esc"), td);
	td.style.paddingRight = "5px";
	td.style.paddingLeft = "5px";


	td = NewTag("td", tr);
	td.className = "distcaption";
	td.style.whiteSpace = "nowrap";



	var ExitImage = NewTag("img", td);
	SetImage (ExitImage, GetAbsUrl("design/ICON_measure_on.gif"), 17,17, "Distance");
	ExitImage.style.top = 0 + "px";
	ExitImage.style.left = 0 + "px";
	td.style.paddingBottom = "5px";




	var me = this;

	this.Move = function(iX, iY) 
	{
	    var mvw = me.tmTopoliszMap.GetMapViewWidth ();
	    var mvh = me.tmTopoliszMap.GetMapViewHeight ();
	    var mvvx = me.tmTopoliszMap.pGridMain.GetMapViewVectorX ();
	    var mvvy = me.tmTopoliszMap.pGridMain.GetMapViewVectorY ();
	    
	    var mvt = mvvy;
	    var mvl = mvvx;
	    var mvb = mvt + mvh;
	    var mvr = mvl + mvw;
	    	    	    	    
	    var mdw = GetLayerWidth (me.mainDiv);
	    var mdh = GetLayerHeight (me.mainDiv);
	    
	    // egyik utkozik
	    var L;
	    if (iX + 11 + mdw < mvr)
	        L = iX + 11;
	    else
	        L = mvr - mdw;
	    
	    // masik utkozik
	    var H;
	    if (iY + 11 + mdh < mvb)
	        H = iY + 11;
	    else
	        H = mvb - mdh;
	        
	    // mindketto utkozik
	    
	    if ((iX + 11 + mdw >= mvr) && (iY + 11 + mdh >= mvb))
	    {
	        L = iX - 11 - mdw;
	        H = iY - 11 - mdh;
	    }
	    
		me.mainDiv.style.left = L + "px";
		me.mainDiv.style.top = H + "px";
		me.mainDiv.style.visibility = "visible";
	}

	this.DisplayLength = function(iTotal, iLast)
	{
        me.displaySumDist.lastChild.nodeValue = GetLengthText(iTotal);
        me.displayLastDist.lastChild.nodeValue = GetLengthText(iLast);
	}

	this.Close = function()
	{
		if(me.mainDiv)
			me.mainDiv.parentNode.removeChild(me.mainDiv);
	}
}

function GetLengthText(iMeter)
{
	var str;
	iMeter = parseInt(iMeter, 10);
	if(iMeter < 1000)
		str = iMeter + " m";
	else
		str = (iMeter/1000) + " km";
	return str;
}


