
function KGMap(map,sGetDataUrl,sInstanceName,sElementId){this._map=map;this._sInstanceName=sInstanceName;this._sGetDataUrl=sGetDataUrl;this._sSaveDataUrl=false;this._sSaveLocationUrl=false;this._sShadowUrl=false;this._sElementId=sElementId;this._sParts='';this._sCustom='';this._oMarkers=new Object();}
KGMap.prototype.updateLocations=function(){this.onStartLoadMap();var zoom=this._map.getZoom();var center=this._map.getCenter();var bounds=this._map.getBounds();var southWest=bounds.getSouthWest();var northEast=bounds.getNorthEast();var span=bounds.toSpan();var $this=this;var sUrl=this._sGetDataUrl;sUrl=sUrl.replace('{zoom}',zoom);sUrl=sUrl.replace('{instance}',this._sInstanceName);sUrl=sUrl.replace('{lat_min}',southWest.lat());sUrl=sUrl.replace('{lat_max}',southWest.lat()+span.lat());sUrl=sUrl.replace('{lng_min}',southWest.lng());sUrl=sUrl.replace('{lng_max}',southWest.lng()+span.lng());sUrl=sUrl.replace('{lat_center}',center.lat());sUrl=sUrl.replace('{lng_center}',center.lng());sUrl=sUrl.replace('{parts}',this._sParts);sUrl=sUrl.replace('{custom}',this._sCustom);sUrl=sUrl.replace('{ts}',(new Date()).getTime());this.loading(1);$.getJSON(sUrl,function(data){$this._oMarkers=null;$this._oMarkers=new Object();$this._map.clearOverlays();var l=data.length;for(var i=0;i<l;++i){var o=data[i];var fLat=parseFloat(o.lat);var fLng=parseFloat(o.lng);var point=new GLatLng(fLat,fLng);var marker=$this.createMarker(point,o.data,o.icon.url,{w:o.icon.w,h:o.icon.h,sw:o.icon.sw,sh:o.icon.sh,sd:o.icon.sd});$this._map.addOverlay(marker);if(o.usernames===undefined)
continue;var ll=o.usernames.length;for(var ii=0;ii<ll;++ii)
$this._oMarkers[o.usernames[ii]]=marker;}
$this.onLoadMap();$this.loading(0);});}
KGMap.prototype.loading=function(b){if(!this._sElementId)
return;bx_map_loading(this._sElementId,b);}
KGMap.prototype.createMarker=function(point,html,image,options){var marker;if(image){var icon=new GIcon();icon.image=image;if(options.sw>0)
icon.shadow=(this._sShadowUrl&&options.sd&&1==parseInt(options.sd)?this._sShadowUrl:image.replace('.png','_shadow.png'));icon.iconSize=new GSize(options.w,options.h);if(options.sw>0)
icon.mapshadowSize=new GSize(options.sw,options.sh);icon.iconAnchor=new GPoint(options.w/2,options.h/2);icon.infoWindowAnchor=new GPoint(options.w/2,0);marker=new GMarker(point,icon);}else{marker=new GMarker(point);}
if(html&&html.length){GEvent.addListener(marker,"click",function(){marker.openInfoWindowHtml(html,{maxWidth:300});});}
return marker;}
KGMap.prototype.magnify=function(fLat,fLng,iZoom){this._map.setCenter(new GLatLng(fLat,fLng),iZoom);}
KGMap.prototype.setShadowUrl=function(sUrl){this._sShadowUrl=sUrl;}
KGMap.prototype.setSaveLocationUrl=function(sUrl){this._sSaveLocationUrl=sUrl;}
KGMap.prototype.setSaveDataUrl=function(sUrl){this._sSaveDataUrl=sUrl;if(false==sUrl||''==sUrl){GEvent.clearListeners(this._map,"click");GEvent.clearListeners(this._map,"zoomend");GEvent.clearListeners(this._map,"maptypechanged");}else{var $this=this;var hh=function(marker,point){var sMapType='normal';switch($this._map.getCurrentMapType())
{case G_SATELLITE_MAP:sMapType='satellite';break;case G_HYBRID_MAP:sMapType='hybrid';break;};$this.saveData('null','null',$this._map.getZoom(),sMapType);};var h=function(marker,point){if(!point)return;$this.saveData(point.lat(),point.lng(),'null','null');$this._map.clearOverlays();$this._map.addOverlay($this.createMarker(point));};GEvent.addListener(this._map,"click",h);GEvent.addListener(this._map,"zoomend",hh);GEvent.addListener(this._map,"maptypechanged",hh);}}
KGMap.prototype.saveData=function(fLat,fLng,iZoom,sMapType){var $this=this;var sUrl=this._sSaveDataUrl;sUrl=sUrl.replace('{zoom}',iZoom);sUrl=sUrl.replace('{map_type}',sMapType);sUrl=sUrl.replace('{lat}',fLat);sUrl=sUrl.replace('{lng}',fLng);sUrl=sUrl.replace('{instance}',this._sInstanceName);sUrl=sUrl.replace('{parts}',this._sParts);sUrl=sUrl.replace('{ts}',(new Date()).getTime());if('null'==fLat||'null'==fLng){sUrl=sUrl.replace('{address}','null');sUrl=sUrl.replace('{country}','null');this.loading(1);$.get(sUrl,function(responseText){$this.loading(0);});}else{var geocoder=new GClientGeocoder();var $this=this;this.loading(1);geocoder.getLocations(new GLatLng(fLat,fLng),function(response){var sAddress='null';var sCountry='null';if(response&&response.Status.code==200){for(var i in response.Placemark){var place=response.Placemark[i];var accuracy=parseInt(place.AddressDetails.Accuracy);if(accuracy<4){sAddress=place.address;sCountry=place.AddressDetails.Country.CountryNameCode;break;}}}
sUrl=sUrl.replace('{address}',sAddress);sUrl=sUrl.replace('{country}',sCountry);$.get(sUrl,function(responseText){$this.onUpdateAddress(sAddress,sCountry);$this.loading(0);});});}}
KGMap.prototype.saveLocation=function(){if(!this._sSaveLocationUrl||''==this._sSaveLocationUrl)
return false;var sUrl=this._sSaveLocationUrl;var sMapType='normal';switch(this._map.getCurrentMapType()){case G_SATELLITE_MAP:sMapType='satellite';break;case G_HYBRID_MAP:sMapType='hybrid';break;};sUrl=sUrl.replace('{zoom}',this._map.getZoom());sUrl=sUrl.replace('{map_type}',sMapType);sUrl=sUrl.replace('{lat}',this._map.getCenter().lat());sUrl=sUrl.replace('{lng}',this._map.getCenter().lng());sUrl=sUrl.replace('{instance}',this._sInstanceName);sUrl=sUrl.replace('{parts}',this._sParts);sUrl=sUrl.replace('{ts}',(new Date()).getTime());this.loading(1);var $this=this;$.get(sUrl,function(responseText){$this.loading(0);if('ok'==responseText)
alert('Location has been saved');else
alert('Location saving failed');});return false;}
KGMap.prototype.onStartLoadMap=function(){this._isMapLoaded=0;if(window.glKGMapLocationsMapOnStartLoadCallback===undefined)
return;glKGMapLocationsMapOnStartLoadCallback(this);}
KGMap.prototype.onLoadMap=function(){this._isMapLoaded=1;if(window.glKGMapLocationsMapOnLoadCallback===undefined)
return;glKGMapLocationsMapOnLoadCallback(this);}
KGMap.prototype.onUpdateAddress=function(sAddress,sCountry){$('#bx_map_curr_loc').html(sAddress);}
KGMap.prototype.setParts=function(s){this._sParts=s;}
KGMap.prototype.setCustom=function(s){this._sCustom=s;}
function bx_map_loading(sId,b){bx_loading(sId,b);}