// Copyright (c) Position s.r.o.
// All rights reserved.

var ngNearestVer=1,ngNearestSubVer=0,ngNearestAPIVersion=ngNearestVer+"."+ngNearestSubVer,ngNearestByID=[],ngNearestLastID=0,errNearestNotFound=-1,errNearestNotAvailable=-2;function nr_ProcessUrl(a){this.rpc.sendRequest(a)}
function nr_NearestToPos(a,b,c,d){if(!(a==""||b=="")){this.Reset();this.SearchType="P";this.NearestParams="&C=P&L="+ng_URLEncode(a)+"&POS="+b;if(typeof c!="undefined"&&c!="")this.NearestParams+="&COUNTRY="+ng_URLEncode(c);if(typeof d!="undefined")this.NearestParams+="&"+d;this.DoSearch()}}
function nr_NearestTo(a,b,c,d,e){if(!(a==""||b==""||c=="")){this.Reset();this.SearchType=a;this.NearestParams="&C="+a+"&L="+ng_URLEncode(b)+"&POS="+ng_URLEncode(c);if(typeof d!="undefined"&&d!="")this.NearestParams+="&COUNTRY="+ng_URLEncode(d);if(typeof e!="undefined")this.NearestParams+="&"+e;this.DoSearch()}}
function nr_LoadLayers(a){if(!this.OnLoadLayers||this.OnLoadLayers(this)){this.CmdID++;if(this.CmdID>99)this.CmdID=0;var b="NE="+this.NearestID+"&C=L&CI="+this.CmdID;if(this.APIKey!="")b+="&KEY="+this.APIKey;if(typeof a!="undefined"&&a!="")b+="&COUNTRY="+ng_URLEncode(a);this.ProcessUrl(ng_AddURLParam(this.NearestURL,b))}}function nr_LayersLoaded(){this.OnLayersLoaded&&this.OnLayersLoaded(this)}
function nr_URLStateParams(){var a="";if(this.NearestParams!=""){a+="NE="+this.NearestID+"&P="+this.Page+"&CI="+this.CmdID;if(this.DistEps>0)a+="&EP="+this.DistEps;if(this.PageSize>0)a+="&PS="+this.PageSize;if(this.APIKey!="")a+="&KEY="+this.APIKey;a+=this.NearestParams}return a}function nr_DoSearch(){if(this.NearestParams!="")if(!this.OnSearch||this.OnSearch(this)){this.CmdID++;if(this.CmdID>99)this.CmdID=0;this.ProcessUrl(ng_AddURLParam(this.NearestURL,this.URLStateParams()))}}
function nr_CancelSearch(){this.CmdID++;if(this.CmdID>99)this.CmdID=0}function nr_SearchFinished(){this.OnSearchResults&&this.OnSearchResults(this)}function nr_Error(a){this.OnSearchError&&this.OnSearchError(this,a)}function nr_FirstPage(){if(this.Page>0){this.Page=0;this.DoSearch()}}function nr_NextPage(){if(this.NextVisible){this.Page++;this.DoSearch()}}function nr_PrevPage(){if(this.Page>0){this.Page--;this.DoSearch()}}
function nr_FindItemByID(a){for(var b=Results.length,c=0;c<b;c++)if(Results[c]&&Results[c].id==a)return c;return-1}function nr_DefaultItemPopupMenu(a,b){return a.CreatePopupHTML(b.descr)}
function nr_DisplayItem(a){if(!this.Map||a<0||a>=this.Results.length)return false;var b=null;if(this.OnDisplayItem)b=this.OnDisplayItem(this,a);if(!b){var c;b=this.Results[a];if(typeof b.pos=="undefined"||b.pos=="")return null;c=this.DisplayIcon.length;if(c>0){c=(this.Page*this.PageSize+a)%c;a=this.DisplayIcon[c];c=this.DisplayIconHotSpot[c]}else{a=this.LocateIcon;c=this.LocateIconHotSpot}b=this.Map.AddPopupIcon(this.LocateIconId+"_"+b.id,a,b.pos,b.descr,c);b.onpopup=this.OnItemPopupMenu}return b}
function nr_DisplayItemByID(a){if(this.Map)return this.DisplayItem(this.FindItemByID(a));return false}
function nr_LocateItem(a,b,c){if(!this.Map||a<0||a>=this.Results.length)return false;if(this.OnLocateItem&&!this.OnLocateItem(this,a))return false;var d=this.Results[a],e=this.LocateIconId+"_"+d.id;if(!this.Map.PopupIconExists(e)){e=this.LocateIconId+"_Search";this.Map.RemovePopupIconByID(e);var f=null;if(this.OnDisplayItem)f=this.OnDisplayItem(this,a);if(!f){if(typeof d.pos=="undefined"||d.pos=="")return false;f=this.Map.AddPopupIcon(e,this.LocateIcon,d.pos,d.descr,this.LocateIconHotSpot);f.onpopup=
this.OnItemPopupMenu}}if(typeof b=="undefined")b=d.scale;return this.Map.LocatePopupIcon(e,b,c)}function nr_LocateItemByID(a,b){if(this.Map)return this.LocateItem(this.FindItemByID(a),undefined,b);return false}function nr_LocateItems(a){this.Map&&this.Map.LocatePopupIcons(this.LocateIconId+"_*",a)}function nr_RemoveItem(a){if(!this.Map||a<0||a>=this.Results.length)return false;if(a=this.Results[a]){this.Map.RemovePopupIconByID(this.LocateIconId+"_"+a.id);return true}return false}
function nr_RemoveItemByID(a){if(this.Map)return this.RemoveItem(this.FindItemByID(a));return false}function nr_DisplayAllItems(){for(var a=this.Results.length,b=0;b<a;b++)this.DisplayItem(b)}function nr_ClearDisplayedItems(){this.Map&&this.Map.RemovePopupIconsByID(this.LocateIconId+"_")}function getNearestByID(a){if(ngNearestByID)return ngNearestByID[a];return null}
function nr_Reset(){this.NearestParams="";this.Results=[];this.SearchType="";this.PageTo=this.PageFrom=this.Page=this.DistEps=0;this.PrevVisible=this.NextVisible=false;this.ClearDisplayedItems()}
function ngNearest(a){ngNearestLastID++;this.NearestID="ngNear"+ngNearestLastID;this.rpc=new ngRPC(this.NearestID);ngNearestByID[this.NearestID]=this;this.APIKey=typeof ngMapAPIKey=="undefined"?"":ngMapAPIKey;this.Map=typeof a!="undefined"?a:null;this.NearestURL=ngMapAPIURL+"/nearest.php";this.NearestParams="";this.LocateIcon=ngMapAPIURL+"/locate.png";this.LocateIconHotSpot=new ScreenPt(15,15);this.LocateIconId="Near"+ngNearestLastID;ng_PreloadImage(this.LocateIcon);this.DisplayIcon=[];this.DisplayIconHotSpot=
[];for(a=0;a<10;a++){this.DisplayIcon[a]=ngMapAPIURL+"/n"+(a+1)+".png";this.DisplayIconHotSpot[a]=new ScreenPt(9,9);ng_PreloadImage(this.DisplayIcon[a])}this.Results=[];this.Layers=[];this.CmdID=0;this.SearchType="";this.PageSize=this.PageTo=this.PageFrom=this.Page=this.DistEps=0;this.PrevVisible=this.NextVisible=false;this.ProcessUrl=nr_ProcessUrl;this.DoSearch=nr_DoSearch;this.URLStateParams=nr_URLStateParams;this.SearchFinished=nr_SearchFinished;this.LayersLoaded=nr_LayersLoaded;this.Error=nr_Error;
this.DefaultItemPopupMenu=nr_DefaultItemPopupMenu;this.Reset=nr_Reset;this.NearestToPos=nr_NearestToPos;this.NearestTo=nr_NearestTo;this.LoadLayers=nr_LoadLayers;this.FirstPage=nr_FirstPage;this.NextPage=nr_NextPage;this.PrevPage=nr_PrevPage;this.FindItemByID=nr_FindItemByID;this.DisplayItem=nr_DisplayItem;this.DisplayItemByID=nr_DisplayItemByID;this.LocateItem=nr_LocateItem;this.LocateItemByID=nr_LocateItemByID;this.LocateItems=nr_LocateItems;this.RemoveItem=nr_RemoveItem;this.RemoveItemByID=nr_RemoveItemByID;
this.DisplayAllItems=nr_DisplayAllItems;this.ClearDisplayedItems=nr_ClearDisplayedItems;this.OnSearchError=this.OnSearchCancel=this.OnSearchResults=this.OnSearch=this.OnLayersLoaded=this.OnLoadLayers=null;this.OnItemPopupMenu=this.DefaultItemPopupMenu;this.OnLocateItem=this.OnDisplayItem=null};
