var a;
Clusterer=function(b){this.map=b;this.markers=[];this.clusters=[];this.timeout=null;this.currentZoomLevel=b.getZoom();this.maxVisibleMarkers=Clusterer.defaultMaxVisibleMarkers;this.gridSize=Clusterer.defaultGridSize;this.minMarkersPerCluster=Clusterer.defaultMinMarkersPerCluster;this.maxLinesPerInfoBox=Clusterer.defaultMaxLinesPerInfoBox;this.icon=Clusterer.defaultIcon;GEvent.addListener(b,"zoomend",Clusterer.MakeCaller(Clusterer.Display,this));GEvent.addListener(b,"moveend",Clusterer.MakeCaller(Clusterer.Display,this));
GEvent.addListener(b,"infowindowclose",Clusterer.MakeCaller(Clusterer.PopDown,this))};Clusterer.defaultMaxVisibleMarkers=20;Clusterer.defaultGridSize=5;Clusterer.defaultMinMarkersPerCluster=2;Clusterer.defaultMaxLinesPerInfoBox=11;Clusterer.defaultIcon=new GIcon;Clusterer.defaultIcon.image="/images/icons/cluster.gif";Clusterer.defaultIcon.shadow="/images/icons/gmap/defaultshadow.png";Clusterer.defaultIcon.iconSize=new GSize(32,32);Clusterer.defaultIcon.shadowSize=new GSize(46,32);
Clusterer.defaultIcon.iconAnchor=new GPoint(16,32);Clusterer.defaultIcon.infoWindowAnchor=new GPoint(16,32);Clusterer.defaultIcon.infoShadowAnchor=new GPoint(16,32);a=Clusterer.prototype;a.SetIcon=function(b){this.icon=b};a.SetMaxVisibleMarkers=function(b){this.maxVisibleMarkers=b};a.SetMinMarkersPerCluster=function(b){this.minMarkersPerCluster=b};a.SetMaxLinesPerInfoBox=function(b){this.maxLinesPerInfoBox=b};
a.AddMarker=function(b,c){b.setMap!=null&&b.setMap(this.map);b.title=c;b.onMap=false;this.markers.push(b);this.DisplayLater()};
a.RemoveMarker=function(b){for(var c=0;c<this.markers.length;++c)if(this.markers[c]==b){b.onMap&&this.map.removeOverlay(b);for(var f=0;f<this.clusters.length;++f){var d=this.clusters[f];if(d!=null){for(var e=0;e<d.markers.length;++e)if(d.markers[e]==b){d.markers[e]=null;--d.markerCount;break}if(d.markerCount==0){this.ClearCluster(d);this.clusters[f]=null}else d==this.poppedUpCluster&&Clusterer.RePop(this)}}this.markers[c]=null;break}this.DisplayLater()};
a.DisplayLater=function(){this.timeout!=null&&clearTimeout(this.timeout);this.timeout=setTimeout(Clusterer.MakeCaller(Clusterer.Display,this),50)};
Clusterer.Display=function(b){var c,f,d,e;clearTimeout(b.timeout);var g=b.map.getZoom();if(g!=b.currentZoomLevel){for(c=0;c<b.clusters.length;++c)if(b.clusters[c]!=null){b.ClearCluster(b.clusters[c]);b.clusters[c]=null}b.clusters.length=0;b.currentZoomLevel=g}g=b.map.getBounds();var h=g.getSouthWest();e=g.getNorthEast();c=e.lng()-h.lng();d=e.lat()-h.lat();if(c<0)c+=360;if(c<300&&d<150){c*=0.1;d*=0.1;g=new GLatLngBounds(new GLatLng(h.lat()-d,h.lng()-c),new GLatLng(e.lat()+d,e.lng()+c))}h=[];e=[];for(c=
0;c<b.markers.length;++c){d=b.markers[c];if(d!=null)g.contains(d.getPoint())?h.push(d):e.push(d)}for(c=0;c<e.length;++c){d=e[c];if(d.onMap){b.map.removeOverlay(d);d.onMap=false}}for(c=0;c<b.clusters.length;++c){e=b.clusters[c];if(e!=null&&!g.contains(e.marker.getPoint())&&e.onMap){b.map.removeOverlay(e.marker);e.onMap=false}}if(h.length>b.maxVisibleMarkers){c=g.toSpan().lat()/b.gridSize;d=c/Math.cos((g.getNorthEast().lat()+g.getSouthWest().lat())/2*Math.PI/180);e=g.toSpan().lng();f=g.getSouthWest().lng()+
e;for(var i=g.getSouthWest().lat();i<=g.getNorthEast().lat();i+=c)for(var j=g.getSouthWest().lng();j<=f;j+=d){e={};e.clusterer=b;e.bounds=new GLatLngBounds(new GLatLng(i,j),new GLatLng(i+c,j+d));e.markers=[];e.markerCount=0;e.onMap=false;e.marker=null;b.clusters.push(e)}for(c=0;c<h.length;++c){d=h[c];if(d!=null&&!d.inCluster)for(f=0;f<b.clusters.length;++f){e=b.clusters[f];if(e!=null&&e.bounds.contains(d.getPoint())){e.markers.push(d);++e.markerCount;d.inCluster=true}}}for(c=0;c<b.clusters.length;++c)if(b.clusters[c]!=
null&&b.clusters[c].markerCount<b.minMarkersPerCluster){b.ClearCluster(b.clusters[c]);b.clusters[c]=null}for(c=b.clusters.length-1;c>=0;--c)if(b.clusters[c]!=null)break;else--b.clusters.length;for(c=0;c<b.clusters.length;++c){e=b.clusters[c];if(e!=null)for(f=0;f<e.markers.length;++f){d=e.markers[f];if(d!=null&&d.onMap){b.map.removeOverlay(d);d.onMap=false}}}for(c=0;c<b.clusters.length;++c){e=b.clusters[c];if(e!=null&&e.marker==null){for(f=j=i=0;f<e.markers.length;++f){d=e.markers[f];if(d!=null){i+=
+d.getPoint().lng();j+=+d.getPoint().lat()}}d=new GLatLng(j/e.markerCount,i/e.markerCount);opts={icon:b.icon,labelText:"+",labelOffset:new GSize(-16,-16),labelClass:"fn-marker"};d=new LabeledMarker(d,opts);e.marker=d;GEvent.addListener(d,"click",Clusterer.MakeCaller(Clusterer.PopUp,e))}}}for(c=0;c<h.length;++c){d=h[c];if(d!=null&&!d.onMap&&!d.inCluster){b.map.addOverlay(d);d.addedToMap!=null&&d.addedToMap();d.onMap=true}}for(c=0;c<b.clusters.length;++c){e=b.clusters[c];if(e!=null&&!e.onMap&&g.contains(e.marker.getPoint())){b.map.addOverlay(e.marker);
e.onMap=true}}Clusterer.RePop(b)};
Clusterer.PopUp=function(b){for(var c=b.clusterer,f="<div>",d=0,e=0;e<b.markers.length;++e){var g=b.markers[e];if(g!=null){++d;f+=g.getIcon().smallImage!=null?'<img src="'+g.getIcon().smallImage+'">':'<img src="'+g.getIcon().image+'" width="'+g.getIcon().iconSize.width/2+'" height="'+g.getIcon().iconSize.height/2+'">';f+="&nbsp;"+g.title+"<br>";if(d==c.maxLinesPerInfoBox-1&&b.markerCount>c.maxLinesPerInfoBox){f+="<p>...and "+(b.markerCount-d)+" more. Zoom In to see more.</p>";break}}}f+="</div>";
c.map.closeInfoWindow();b.marker.openInfoWindowHtml(f);c.poppedUpCluster=b};Clusterer.RePop=function(b){b.poppedUpCluster!=null&&Clusterer.PopUp(b.poppedUpCluster)};Clusterer.PopDown=function(b){b.poppedUpCluster=null};
Clusterer.prototype.ClearCluster=function(b){var c;for(c=0;c<b.markers.length;++c)if(b.markers[c]!=null){b.markers[c].inCluster=false;b.markers[c]=null}b.markers.length=0;b.markerCount=0;b==this.poppedUpCluster&&this.map.closeInfoWindow();if(b.onMap){this.map.removeOverlay(b.marker);b.onMap=false}};Clusterer.MakeCaller=function(b,c){return function(){b(c)}};a=GMarker.prototype;a.setMap=function(b){this.map=b};a.addedToMap=function(){this.map=null};a.origOpenInfoWindow=GMarker.prototype.openInfoWindow;
a.openInfoWindow=function(b,c){return this.map!=null?this.map.openInfoWindow(this.getPoint(),b,c):this.origOpenInfoWindow(b,c)};a.origOpenInfoWindowHtml=GMarker.prototype.openInfoWindowHtml;a.openInfoWindowHtml=function(b,c){return this.map!=null?this.map.openInfoWindowHtml(this.getPoint(),b,c):this.origOpenInfoWindowHtml(b,c)};a.origOpenInfoWindowTabs=GMarker.prototype.openInfoWindowTabs;
a.openInfoWindowTabs=function(b,c){return this.map!=null?this.map.openInfoWindowTabs(this.getPoint(),b,c):this.origOpenInfoWindowTabs(b,c)};a.origOpenInfoWindowTabsHtml=GMarker.prototype.openInfoWindowTabsHtml;a.openInfoWindowTabsHtml=function(b,c){return this.map!=null?this.map.openInfoWindowTabsHtml(this.getPoint(),b,c):this.origOpenInfoWindowTabsHtml(b,c)};a.origShowMapBlowup=GMarker.prototype.showMapBlowup;
a.showMapBlowup=function(b){return this.map!=null?this.map.showMapBlowup(this.getPoint(),b):this.origShowMapBlowup(b)};
