﻿// Ez az az objektumlista (Markers), amit a kliens
// karbantart. Ezt adja at a szervernek, hogy a cimekhez
// megkapja a pixel koordinatakat.

function Marker (pTopoliszMap, mainType, dX, dY, arrNames, arrValues)
{	
	this.pTopoliszMap = pTopoliszMap;
	this.mainType = mainType;	
	this.dX = dX;
	this.dY = dY;
	this.arrNames = arrNames;
	this.arrValues = arrValues;
	this.bLabelBoxMustBeVisible = false; 
	this.bDetailsMustBeVisible = false; 
	this.iPixX = null;
	this.iPixY = null;		
	this.sID = null;	 // it is NOT the server ID !!
	this.bHandleDetails = true;	
	this.pLabel = null;
	this.pVPLI = null;
	this.bContentLoaded = true;
	this.bMouseOverEnabled = true;
	this.bLongMouseOver = false;
	
	this.DrawSign_Custom = null;		// function MyDrawSign ();
	this.DrawBox_Custom = null;			// function MyDrawBox ();
	this.DrawBoxHeader_Custom = null;	// function MyDrawHeaderBox (divLabelBox);
	this.DrawBoxContent_Custom = null;	// function MyDrawContentBox (divLabelBox);
	this.DrawBoxContentData = null; // function MyDrawContentBox (divLabelBox);
	this.DrawCustomButtons = null;		// draws custom buttons before start/through/stop

	this.m_bSignlessActive = false; // if 'true' : this is a marker without visible pin sign, only has
									// coordinates and dimension (typically the picture is on a layer)									
	
	var me = this;
	
	this.SetPixelCoordinates = function (iX, iY)
	{
		me.iPixX = iX;
		me.iPixY = iY;			
	}

	this.Clone = function()
	{
		var baby = new Marker (me.pTopoliszMap, me.sID, me.dX, me.dY, me.arrNames, me.arrValues);
		return baby;
	}

	this.DeleteLabel = function ()
	{
		if (me.pLabel)
		{
			me.pLabel.Delete ();
			delete me.pLabel;
			me.pLabel = null;
		}
	}
	
	this.GetProperty = function (name)
	{
		if (me.arrNames == null)
			return "???";
		
		var i;
		for (i = 0; i < me.arrNames.length; i++)
		{
			if (me.arrNames[i] == name)			
				return me.arrValues[i];
		}	
		return "???";
	}
	
	this.IsLabelBoxVisible = function ()
	{
		if (!me.pLabel)
			return false;
		else
			return me.pLabel.IsLabelBoxVisible();
	}

	this.SetProperty = function (name, value)
	{
		if (me.arrNames == null)
			me.arrNames = new Array();
		if (me.arrValues == null)
			me.arrValues = new Array();
			
		var i;
		for (i = 0; i < me.arrNames.length; i++)
		{
			if (me.arrNames[i] == name)
			{			
				me.arrValues[i] = value;
				return;
			}
		}	
		me.arrNames.push(name);
		me.arrValues.push(value);
	}
	
	this.ArrNamesAsString = function ()
	{
		var str = "";
		var i;
		for (i = 0; i < me.arrNames.length; i++)
		{
			str = str + me.arrNames[i] + "#";
		}
		return str;
	}

	this.ArrValuesAsString = function ()
	{
		var str = "";
		var i;
		for (i = 0; i < me.arrValues.length; i++)
		{
			str = str + me.arrValues[i] + "#";
		}
		return str;	
	}
	
	this.GetLabelCaption = function (name)
	{
		return me.GetProperty("caption");
	}
	
	this.IsSignless = function ()
	{
		if (me.GetProperty("pin_img_url") == "???")
			return true;
		else	
			return false;
	}			
	
	this.HasWGSCoords = function ()
	{
		var bCoordIsSet = ((me.dX != 0) || (me.dY != 0));
		return bCoordIsSet;
	}
	
	// POI-nál: address.strName a név, address.strAddress a cím
	// címnél: address.strName a cím, address.strAddress = ""
	this.ToAddress = function ()
	{
		var address = new Object ();					
		address.dX = me.dX;
		address.dY = me.dY;		
		address.strName = "";
		address.strAddress = "";
		
		if (me.GetProperty("Név") != "???")
			address.strName = me.GetProperty("Név");
		else if (me.GetProperty("caption") != "???")
			address.strName = me.GetProperty("caption");
			
		if (me.GetProperty("Cím") != "???")
			address.strAddress = me.GetProperty("Cím");
		else if (me.GetProperty("caption") != "???")
			address.strAddress = me.GetProperty("caption");
		
		if (address.strAddress != address.strName)
		{
			var idxFirstSpace = address.strAddress.indexOf(' ');
			
			var alreadyExists = address.strName.indexOf('(');
			
			if ((idxFirstSpace != -1) && (alreadyExists == -1))
			{
				var firstChar = address.strAddress.charAt(0);
				
				if     ((firstChar != '0') && (firstChar != '1') && (firstChar != '2') &&
					(firstChar != '3') && (firstChar != '4') && (firstChar != '5') &&
					(firstChar != '6') && (firstChar != '7') && (firstChar != '8') &&
					(firstChar != '9'))
				{
					if (address.strAddress.charAt(idxFirstSpace-1) == ',') idxFirstSpace--;
					var strCity = address.strAddress.substring (0, idxFirstSpace);
					address.strName = address.strName + " (" + strCity + ")";
				}
				else
				{
					var idxSecondSpace = address.strAddress.indexOf(' ', idxFirstSpace+1);
					if (address.strAddress.charAt(idxSecondSpace-1) == ',') idxSecondSpace--;
					if (idxSecondSpace  != -1)
					{
						var strCity = address.strAddress.substring (idxFirstSpace+1, idxSecondSpace);		
						address.strName = address.strName + " (" + strCity + ")";
					}
				}
			}		
		}
		else
		{
			address.strName = "";
		}
		return address;
	}
	
	this.OnLabelSignClick = function (pEvent)
	{				
		me.pTopoliszMap.OnLabelSignClick (me);
		return false;
	}

	this.OnLabelSignMouseOver = function (pEvent)
	{	
		me.pTopoliszMap.OnLabelSignMouseOver (me);
		return false;	
	}
	
	me.SetProperty("label_css_classname", "label_v2");
	me.SetProperty("labelbox_css_classname", "label_box");
	me.SetProperty("list_css_classname", "poi_list_row");
	me.SetProperty("close_button", true);
	me.SetProperty("fix_label_on_click", false);
	
	var func_label_sign_on_click = function () {}	
	me.SetProperty("func_label_sign_on_click", func_label_sign_on_click);
}

function Markers ()
{
	this.arrMarkers = new Array ();
	this.m_iLastID = 0;

	var me = this;
	
	this.Clone = function()
	{
		var baby = new Markers();
		for(var i=0; i<me.arrMarkers.length; i++)
		{
			if(me.arrMarkers[i] != null)
			{
				baby.arrMarkers[baby.arrMarkers.length] = me.arrMarkers[i];
			}
		}
		return baby;
	}

	this.Find = function (id)
	{
		var i;
		for (i = 0; i < me.arrMarkers.length; i++)
		{
			if (me.arrMarkers[i] != null)
			{
				if (id == me.arrMarkers[i].sID)
					return me.arrMarkers[i];
			}
		}
		return null;
	}

	this.FindIdx = function (id)
	{
		var i;
		for (i = 0; i < me.arrMarkers.length; i++)
		{
			if (me.arrMarkers[i] != null)
			{
				if (id == me.arrMarkers[i].sID)
					return i;
			}
		}
		return -1;
	}
            
    this.Add = function (marker)
    {		
		marker.sID = me.m_iLastID;
		me.m_iLastID++;
		me.arrMarkers[me.arrMarkers.length] = marker;
		return marker;
    }
      	
	this.Delete = function (id)
	{
		var idx = me.FindIdx (id);
		if (idx != -1)
		{
			me.arrMarkers[idx].DeleteLabel();
			delete me.arrMarkers[idx];
			me.arrMarkers[idx] = null;
		}
	}
	
	this.DeleteAll = function ()
	{
		var i;
		for (i = 0; i < me.arrMarkers.length; i++)
		{
			if (me.arrMarkers[i] != null)
				me.arrMarkers[i].DeleteLabel();
		}
			
		delete me.arrMarkers;
		me.arrMarkers = new Array ();
	}

	this.DeleteAllWithType = function (type)
	{
		var arrCleaned = new Array();
		
		var i;
		for (i = 0; i < me.arrMarkers.length; i++)
		{
			if (me.arrMarkers[i] != null)
			{
				if (me.arrMarkers[i].mainType == type)
				{
					me.arrMarkers[i].DeleteLabel();
					delete me.arrMarkers[i];
					me.arrMarkers[i] = null;	
				}				
				else
				{
					arrCleaned.push(me.arrMarkers[i]);
				}			
			}
		}
		
		delete me.arrMarkers;
		me.arrMarkers = arrCleaned;
	}
	
	this.GetUrl = function()
	{
		var strIDs = "";
		var strXs = "";
		var strYs = "";
		for (i = 0; i < me.arrMarkers.length; i++)
		{
			if (me.arrMarkers[i] != null)
			{
				if(strIDs != "")
				{
					strIDs += "|";
					strXs += "|";
					strYs += "|";
				}
				
				strIDs += me.arrMarkers[i].sID;
				strXs += me.arrMarkers[i].dX;
				strYs += me.arrMarkers[i].dY;
			}
		}
		var strUrl = "arrMarkerIDs=" + strIDs + "&arrMarkerX=" + strXs + "&arrMarkerY=" + strYs;
		return strUrl;
	}
	
	/// filterFunc: bool func(marker)
	this.GetUrlParams = function (filterFunc)
	{
	    var strIDs = "";
	    var strXs = "";
	    var strYs = "";
	    var strTexts = "";
	    for (i = 0; i < me.arrMarkers.length; i++)
	    {
	        var pMarker = me.arrMarkers[i];
	        if (pMarker != null)
	        {
	            if (filterFunc != null)
	            {
	                if (!filterFunc(pMarker))
	                    continue;
	            }
	            if (strIDs != "")
	            {
	                strIDs += "|";
	                strXs += "|";
	                strYs += "|";
	                strTexts += "|";
	            }

	            strIDs += pMarker.sID;
	            strXs += pMarker.dX;
	            strYs += pMarker.dY;
	            var strNumber = pMarker.GetProperty("pinnumber");
	            if (strNumber != "???" && strNumber != "0")
	            {
	                strTexts += strNumber;
	            }

	        }
	    }
	    return { "arrMarkerIDs": strIDs, "arrMarkerX": strXs, "arrMarkerY": strYs, "arrMarkerText": strTexts };
	}

	this.GetMouseOutDistance = function ()
	{
		return 10;
	}
	
    this.GetPinNearXY = function (x,y) //x,y : grid koord
    {
		var method = /*"firstnear"*/ "combi";
		
		if (method == "firstnear")
		{
			var i;                
			// list order is equal the drawing order. The top most element is the last
			for (i = me.arrMarkers.length-1; i >= 0; i--)
			{
				var pPin = me.arrMarkers[i];
				if (pPin != null)
				{
					if (pPin.m_bSignlessActive)
					{
						if ((Math.abs((pPin.iPixX - x)) < 10) && (Math.abs((pPin.iPixY - y)) < 10))
						{						
							return pPin;
						}
					}
				}
			}                
			return null;
		}
		else if  (method == "nearest")
		{
			var i;      
			var mindistsqr = -1;
			var pRetPin = null;
			
			for (i = 0; i <  me.arrMarkers.length; i++)
			{
				var pPin = me.arrMarkers[i];
				if (pPin != null)
				{
					if (pPin.m_bSignlessActive)
					{
						if ((Math.abs((pPin.iPixX - x)) < 15) && (Math.abs((pPin.iPixY - y)) < 15))
						{				
							var distsqr = (pPin.iPixX - x) * (pPin.iPixX - x) + (pPin.iPixY - y) * (pPin.iPixY - y);														
							if ((distsqr < mindistsqr) || (mindistsqr == -1))
							{
								mindistsqr = distsqr;
								pRetPin = pPin;
							}								
						}
					}
				}
			}   
			             																									
			return pRetPin;		
		}
		else if (method == "combi") // the nearest, but if a very near is found the func returns 
									// the pure "nearest" can not consider drawing order
		{
			var i;      
			var mindistsqr = -1;
			var pRetPin = null;
			
			for (i = me.arrMarkers.length-1; i >= 0; i--)
			{
				var pPin = me.arrMarkers[i];
				if (pPin != null)
				{
					if (pPin.m_bSignlessActive)
					{
						if ((Math.abs((pPin.iPixX - x)) < 15) && (Math.abs((pPin.iPixY - y)) < 15))
						{				
							var distsqr = (pPin.iPixX - x) * (pPin.iPixX - x) + (pPin.iPixY - y) * (pPin.iPixY - y);														
							if ((distsqr < mindistsqr) || (mindistsqr == -1))
							{
								mindistsqr = distsqr;
								pRetPin = pPin;
							}	
							
							
							if (distsqr <= 25)							
							{
								return pPin;				
							}
						}
					}
				}
			}   
			             																									
			return pRetPin;				
		}
    }	
}