function ClusterMarker(b,a){this._map=b;this._mapMarkers=[];this._iconBounds=[];this._clusterMarkers=[];this._eventListeners=[];if(typeof(a)==="undefined"){a={};}this.borderPadding=(a.borderPadding)?a.borderPadding:256;this.clusteringEnabled=(a.clusteringEnabled===false)?false:true;if(a.clusterMarkerClick){this.clusterMarkerClick=a.clusterMarkerClick;}this.clusterMarkerIcon=new GIcon();this.clusterMarkerIcon.iconSize=new GSize(40,40);this.clusterMarkerIcon.iconAnchor=new GPoint(6,20);this.clusterMarkerIcon.infoWindowAnchor=new GPoint(6,1);this.clusterMarkerTitle=(a.clusterMarkerTitle)?a.clusterMarkerTitle:"Visualizza %count aziende";if(a.fitMapMaxZoom){this.fitMapMaxZoom=a.fitMapMaxZoom;}this.intersectPadding=(a.intersectPadding)?a.intersectPadding:0;if(a.markers){this.addMarkers(a.markers);}GEvent.bind(this._map,"moveend",this,this._moveEnd);GEvent.bind(this._map,"zoomend",this,this._zoomEnd);GEvent.bind(this._map,"maptypechanged",this,this._mapTypeChanged);}ClusterMarker.prototype.addMarkers=function(b){var a;if(!b[0]){var c=[];for(a in b){c.push(b[a]);}b=c;}for(a=b.length-1;a>=0;a--){b[a]._isVisible=false;b[a]._isActive=false;b[a]._makeVisible=false;}this._mapMarkers=this._mapMarkers.concat(b);};ClusterMarker.prototype._clusterMarker=function(b){function a(l,i,k){return new GMarker(l,{icon:i,title:k});}var g=new GLatLngBounds(),c,d,f=[],j,e=this;for(c=b.length-1;c>=0;c--){j=this._mapMarkers[b[c]];j.index=b[c];g.extend(j.getLatLng());f.push(j);}markerCount=b.length;color="#eeff00";if(markerCount<10){color="#eeff00";}else{if(markerCount<50){color="#eeaa00";}else{color="#ee3300";}}var h=MapIconMaker.createFlatIcon({shape:"roundrect",addStar:false,width:32,height:32,labelSize:0,label:""+b.length,primaryColor:color});d=a(g.getCenter(),h,this.clusterMarkerTitle.replace(/%count/gi,b.length));d.clusterGroupBounds=g;this._eventListeners.push(GEvent.addListener(d,"click",function(){e.clusterMarkerClick({clusterMarker:d,clusteredMarkers:f});}));return d;};ClusterMarker.prototype.clusterMarkerClick=function(a){this._map.setCenter(a.clusterMarker.getLatLng(),this._map.getBoundsZoomLevel(a.clusterMarker.clusterGroupBounds));};ClusterMarker.prototype._filterActiveMapMarkers=function(){var h=this.borderPadding,g=this._map.getZoom(),o=this._map.getCurrentMapType().getProjection(),m,b,k,f,l,c,a=this._map.getBounds(),e,n,d=[],j;if(h){m=o.fromLatLngToPixel(a.getSouthWest(),g);b=new GPoint(m.x-h,m.y+h);k=o.fromPixelToLatLng(b,g);f=o.fromLatLngToPixel(a.getNorthEast(),g);l=new GPoint(f.x+h,f.y-h);c=o.fromPixelToLatLng(l,g);a.extend(k);a.extend(c);}this._activeMarkersChanged=false;if(typeof(this._iconBounds[g])==="undefined"){this._iconBounds[g]=[];this._activeMarkersChanged=true;for(e=this._mapMarkers.length-1;e>=0;e--){n=this._mapMarkers[e];n._isActive=a.containsLatLng(n.getLatLng())?true:false;n._makeVisible=n._isActive;if(n._isActive){d.push(e);}}}else{for(e=this._mapMarkers.length-1;e>=0;e--){n=this._mapMarkers[e];j=n._isActive;n._isActive=a.containsLatLng(n.getLatLng())?true:false;n._makeVisible=n._isActive;if(!this._activeMarkersChanged&&j!==n._isActive){this._activeMarkersChanged=true;}if(n._isActive&&typeof(this._iconBounds[g][e])==="undefined"){d.push(e);}}}return d;};ClusterMarker.prototype._filterIntersectingMapMarkers=function(){var d,c,b,a=this._map.getZoom();for(c=this._mapMarkers.length-1;c>0;c--){if(this._mapMarkers[c]._makeVisible){d=[];for(b=c-1;b>=0;b--){if(this._mapMarkers[b]._makeVisible&&this._iconBounds[a][c].intersects(this._iconBounds[a][b])){d.push(b);}}if(d.length!==0){d.push(c);for(b=d.length-1;b>=0;b--){this._mapMarkers[d[b]]._makeVisible=false;}this._clusterMarkers.push(this._clusterMarker(d));}}}};ClusterMarker.prototype.fitMapToMarkers=function(){var c=this._mapMarkers,d=new GLatLngBounds(),b;for(b=c.length-1;b>=0;b--){d.extend(c[b].getLatLng());}var a=this._map.getBoundsZoomLevel(d);if(this.fitMapMaxZoom&&a>this.fitMapMaxZoom){a=this.fitMapMaxZoom;}this._map.setCenter(d.getCenter(),a);this.refresh();stopLoading();};ClusterMarker.prototype._mapTypeChanged=function(){this.refresh(true);};ClusterMarker.prototype._moveEnd=function(){if(!this._cancelMoveEnd){this.refresh();}else{this._cancelMoveEnd=false;}};ClusterMarker.prototype._preCacheIconBounds=function(b){var m=this._map.getCurrentMapType().getProjection(),g=this._map.getZoom(),f,n,d,a,h,k,e,j,c,l=this.intersectPadding;for(f=b.length-1;f>=0;f--){n=this._mapMarkers[b[f]];d=n.getIcon().iconSize;a=m.fromLatLngToPixel(n.getLatLng(),g);h=n.getIcon().iconAnchor;k=new GPoint(a.x-h.x-l,a.y-h.y+d.height+l);e=new GPoint(a.x-h.x+d.width+l,a.y-h.y-l);j=m.fromPixelToLatLng(k,g);c=m.fromPixelToLatLng(e,g);this._iconBounds[g][b[f]]=new GLatLngBounds(j,c);}};ClusterMarker.prototype.refresh=function(d){var c,b,a=this._filterActiveMapMarkers();if(this._activeMarkersChanged||d){this._removeClusterMarkers();if(this.clusteringEnabled&&this._map.getZoom()<this._map.getCurrentMapType().getMaximumResolution()){if(a.length>0){this._preCacheIconBounds(a);}this._filterIntersectingMapMarkers();}for(c=this._clusterMarkers.length-1;c>=0;c--){this._map.addOverlay(this._clusterMarkers[c]);}for(c=this._mapMarkers.length-1;c>=0;c--){b=this._mapMarkers[c];if(!b._isVisible&&b._makeVisible){this._map.addOverlay(b);b._isVisible=true;}if(b._isVisible&&!b._makeVisible){this._map.removeOverlay(b);b._isVisible=false;}}}};ClusterMarker.prototype._removeClusterMarkers=function(){for(var a=this._clusterMarkers.length-1;a>=0;a--){this._map.removeOverlay(this._clusterMarkers[a]);}for(a=this._eventListeners.length-1;a>=0;a--){GEvent.removeListener(this._eventListeners[a]);}this._clusterMarkers=[];this._eventListeners=[];};ClusterMarker.prototype.removeMarkers=function(){for(var a=this._mapMarkers.length-1;a>=0;a--){if(this._mapMarkers[a]._isVisible){this._map.removeOverlay(this._mapMarkers[a]);}delete this._mapMarkers[a]._isVisible;delete this._mapMarkers[a]._isActive;delete this._mapMarkers[a]._makeVisible;}this._removeClusterMarkers();this._mapMarkers=[];this._iconBounds=[];};ClusterMarker.prototype.triggerClick=function(a){var b=this._mapMarkers[a];if(b._isVisible){GEvent.trigger(b,"click");}else{if(b._isActive){this._map.setCenter(b.getLatLng());this._map.zoomIn();this.triggerClick(a);}else{this._map.setCenter(b.getLatLng());this.triggerClick(a);}}};ClusterMarker.prototype._zoomEnd=function(){this._cancelMoveEnd=true;this.refresh(true);};
