function MarkerManager(a,c){var b=this;b.map_=a;b.mapZoom_=a.getZoom();b.projection_=a.getCurrentMapType().getProjection();c=c||{};b.tileSize_=MarkerManager.DEFAULT_TILE_SIZE_;var d=MarkerManager.DEFAULT_MAX_ZOOM_;if(c.maxZoom!=undefined)d=c.maxZoom;b.maxZoom_=d;b.trackMarkers_=c.trackMarkers;var e;e=typeof c.borderPadding=="number"?c.borderPadding:MarkerManager.DEFAULT_BORDER_PADDING_;b.swPadding_=new GSize(-e,e);b.nePadding_=new GSize(e,-e);b.borderPadding_=e;b.gridWidth_=[];b.grid_=[];b.grid_[d]=
[];b.numMarkers_=[];b.numMarkers_[d]=0;GEvent.bind(a,"moveend",b,b.onMapMoveEnd_);b.removeOverlay_=function(f){a.removeOverlay(f);b.shownMarkers_--};b.addOverlay_=function(f){a.addOverlay(f);b.shownMarkers_++};b.resetManager_();b.shownMarkers_=0;b.shownBounds_=b.getMapGridBounds_()}MarkerManager.DEFAULT_TILE_SIZE_=1024;MarkerManager.DEFAULT_MAX_ZOOM_=17;MarkerManager.DEFAULT_BORDER_PADDING_=100;MarkerManager.MERCATOR_ZOOM_LEVEL_ZERO_RANGE=256;
MarkerManager.prototype.resetManager_=function(){for(var a=MarkerManager.MERCATOR_ZOOM_LEVEL_ZERO_RANGE,c=0;c<=this.maxZoom_;++c){this.grid_[c]=[];this.numMarkers_[c]=0;this.gridWidth_[c]=Math.ceil(a/this.tileSize_);a<<=1}};MarkerManager.prototype.clearMarkers=function(){this.processAll_(this.shownBounds_,this.removeOverlay_);this.resetManager_()};
MarkerManager.prototype.getTilePoint_=function(a,c,b){a=this.projection_.fromLatLngToPixel(a,c);return new GPoint(Math.floor((a.x+b.width)/this.tileSize_),Math.floor((a.y+b.height)/this.tileSize_))};MarkerManager.prototype.addMarkerBatch_=function(a,c,b){var d=a.getPoint();this.trackMarkers_&&GEvent.bind(a,"changed",this,this.onMarkerMoved_);d=this.getTilePoint_(d,b,GSize.ZERO);for(b=b;b>=c;b--){this.getGridCellCreate_(d.x,d.y,b).push(a);d.x>>=1;d.y>>=1}};
MarkerManager.prototype.isGridPointVisible_=function(a){var c=this.shownBounds_.minY<=a.y&&a.y<=this.shownBounds_.maxY,b=this.shownBounds_.minX,d=b<=a.x&&a.x<=this.shownBounds_.maxX;if(!d&&b<0){d=this.gridWidth_[this.shownBounds_.z];d=b+d<=a.x&&a.x<=d-1}return c&&d};
MarkerManager.prototype.onMarkerMoved_=function(a,c,b){var d=this.maxZoom_,e=false;c=this.getTilePoint_(c,d,GSize.ZERO);for(b=this.getTilePoint_(b,d,GSize.ZERO);d>=0&&(c.x!=b.x||c.y!=b.y);){var f=this.getGridCellNoCreate_(c.x,c.y,d);f&&this.removeFromArray(f,a)&&this.getGridCellCreate_(b.x,b.y,d).push(a);if(d==this.mapZoom_)if(this.isGridPointVisible_(c)){if(!this.isGridPointVisible_(b)){this.removeOverlay_(a);e=true}}else if(this.isGridPointVisible_(b)){this.addOverlay_(a);e=true}c.x>>=1;c.y>>=1;
b.x>>=1;b.y>>=1;--d}e&&this.notifyListeners_()};MarkerManager.prototype.removeMarker=function(a){for(var c=this.maxZoom_,b=false,d=this.getTilePoint_(a.getPoint(),c,GSize.ZERO);c>=0;){var e=this.getGridCellNoCreate_(d.x,d.y,c);e&&this.removeFromArray(e,a);if(c==this.mapZoom_)if(this.isGridPointVisible_(d)){this.removeOverlay_(a);b=true}d.x>>=1;d.y>>=1;--c}b&&this.notifyListeners_()};
MarkerManager.prototype.addMarkers=function(a,c,b){b=this.getOptMaxZoom_(b);for(var d=a.length-1;d>=0;d--)this.addMarkerBatch_(a[d],c,b);this.numMarkers_[c]+=a.length};MarkerManager.prototype.getOptMaxZoom_=function(a){return a!=undefined?a:this.maxZoom_};MarkerManager.prototype.getMarkerCount=function(a){for(var c=0,b=0;b<=a;b++)c+=this.numMarkers_[b];return c};
MarkerManager.prototype.addMarker=function(a,c,b){b=this.getOptMaxZoom_(b);this.addMarkerBatch_(a,c,b);if(this.isGridPointVisible_(this.getTilePoint_(a.getPoint(),this.mapZoom_,GSize.ZERO))&&c<=this.shownBounds_.z&&this.shownBounds_.z<=b){this.addOverlay_(a);this.notifyListeners_()}this.numMarkers_[c]++};GBounds.prototype.containsPoint=function(a){return this.minX<=a.x&&this.maxX>=a.x&&this.minY<=a.y&&this.maxY>=a.y};
MarkerManager.prototype.getGridCellCreate_=function(a,c,b){var d=this.grid_[b];if(a<0)a+=this.gridWidth_[b];b=d[a];if(!b){b=d[a]=[];return b[c]=[]}a=b[c];if(!a)return b[c]=[];return a};MarkerManager.prototype.getGridCellNoCreate_=function(a,c,b){var d=this.grid_[b];if(a<0)a+=this.gridWidth_[b];return(a=d[a])?a[c]:undefined};
MarkerManager.prototype.getGridBounds_=function(a,c,b,d){c=Math.min(c,this.maxZoom_);var e=a.getSouthWest();a=a.getNorthEast();b=this.getTilePoint_(e,c,b);d=this.getTilePoint_(a,c,d);var f=this.gridWidth_[c];if(a.lng()<e.lng()||d.x<b.x)b.x-=f;if(d.x-b.x+1>=f){b.x=0;d.x=f-1}e=new GBounds([b,d]);e.z=c;return e};MarkerManager.prototype.getMapGridBounds_=function(){return this.getGridBounds_(this.map_.getBounds(),this.mapZoom_,this.swPadding_,this.nePadding_)};
MarkerManager.prototype.onMapMoveEnd_=function(){this.objectSetTimeout_(this,this.updateMarkers_,0)};MarkerManager.prototype.objectSetTimeout_=function(a,c,b){return window.setTimeout(function(){c.call(a)},b)};MarkerManager.prototype.refresh=function(){this.shownMarkers_>0&&this.processAll_(this.shownBounds_,this.removeOverlay_);this.processAll_(this.shownBounds_,this.addOverlay_);this.notifyListeners_()};
MarkerManager.prototype.updateMarkers_=function(){this.mapZoom_=this.map_.getZoom();var a=this.getMapGridBounds_();if(!(a.equals(this.shownBounds_)&&a.z==this.shownBounds_.z)){if(a.z!=this.shownBounds_.z){this.processAll_(this.shownBounds_,this.removeOverlay_);this.processAll_(a,this.addOverlay_)}else{this.rectangleDiff_(this.shownBounds_,a,this.removeCellMarkers_);this.rectangleDiff_(a,this.shownBounds_,this.addCellMarkers_)}this.shownBounds_=a;this.notifyListeners_()}};
MarkerManager.prototype.notifyListeners_=function(){GEvent.trigger(this,"changed",this.shownBounds_,this.shownMarkers_)};MarkerManager.prototype.processAll_=function(a,c){for(var b=a.minX;b<=a.maxX;b++)for(var d=a.minY;d<=a.maxY;d++)this.processCellMarkers_(b,d,a.z,c)};MarkerManager.prototype.processCellMarkers_=function(a,c,b,d){if(a=this.getGridCellNoCreate_(a,c,b))for(c=a.length-1;c>=0;c--)d(a[c])};MarkerManager.prototype.removeCellMarkers_=function(a,c,b){this.processCellMarkers_(a,c,b,this.removeOverlay_)};
MarkerManager.prototype.addCellMarkers_=function(a,c,b){this.processCellMarkers_(a,c,b,this.addOverlay_)};MarkerManager.prototype.rectangleDiff_=function(a,c,b){var d=this;d.rectangleDiffCoords(a,c,function(e,f){b.apply(d,[e,f,a.z])})};
MarkerManager.prototype.rectangleDiffCoords=function(a,c,b){var d=a.minX,e=a.minY,f=a.maxX;a=a.maxY;var j=c.minX,i=c.minY,k=c.maxX;c=c.maxY;for(var h=d;h<=f;h++){for(var g=e;g<=a&&g<i;g++)b(h,g);for(g=Math.max(c+1,e);g<=a;g++)b(h,g)}for(g=Math.max(e,i);g<=Math.min(a,c);g++){for(h=Math.min(f+1,j)-1;h>=d;h--)b(h,g);for(h=Math.max(d,k+1);h<=f;h++)b(h,g)}};MarkerManager.prototype.removeFromArray=function(a,c,b){for(var d=0,e=0;e<a.length;++e)if(a[e]===c||b&&a[e]==c){a.splice(e--,1);d++}return d};
