function Map(options){var defaultOptions={htmlContainer:'map_container',centerLat:'-15.397',centerLng:'10.644',initialZoom:2,minZoom:1,maxZoom:9,bigControls:false,visitMapper:[],markerClick:function(){}};$.extend(defaultOptions,options);this.htmlContainer=defaultOptions.htmlContainer;this.centerLat=defaultOptions.centerLat;this.centerLng=defaultOptions.centerLng;this.initialZoom=defaultOptions.initialZoom;this.minZoom=defaultOptions.minZoom;this.maxZoom=defaultOptions.maxZoom;this.bigControls=defaultOptions.bigControls;this.onClickMarker=defaultOptions.markerClick;this.visitMapper=new VisitImageMapper(defaultOptions.visitMapper);this.bounds=new google.maps.LatLngBounds();this.overlays=[];this.plot=true;this.initialize();}
Map.UNCLUSTERED_MAX_SIZE=200;Map.BALLOON_MARKER_IMAGE_PUBLIC='/images/marker_public.png';Map.BALLOON_MARKER_IMAGE_PRIVATE='/images/marker_private.png';Map.BALLOON_MARKER_SHADOW='http://labs.google.com/ridefinder/images/mm_20_shadow.png';Map.DEFAULT_MARKER_OPTIONS={iconImage:Map.BALLOON_MARKER_IMAGE_PUBLIC,iconWidth:12,iconHeight:20,iconAnchorX:6,iconAnchorY:20,shadowImage:Map.BALLOON_MARKER_SHADOW,shadowWidth:22,shadowHeight:20,shadowAnchorX:6,shadowAnchorY:20};Map.DEFAULT_CURRENT_CITY_MARKER_OPTIONS={iconImage:'http://maps.google.com/mapfiles/ms/micons/man.png',iconWidth:32,iconHeight:32,iconAnchorX:16,iconAnchorY:32,shadowImage:'http://maps.google.com/mapfiles/ms/micons/man.shadow.png',shadowWidth:59,shadowHeight:32,shadowAnchorX:16,shadowAnchorY:32};Map._CLUSTERER_CONFIG={gridSize:40,maxZoom:4,styles:[{url:'/images/cluster_small.png',height:45,width:45},{url:'/images/cluster_medium.png',height:55,width:55},{url:'/images/cluster_big.png',height:65,width:65}]};Map.prototype.initialize=function(){var $this=this;var latlng=new google.maps.LatLng(this.centerLat,this.centerLng);var mapOptions={zoom:$this.initialZoom,navigationControl:true,navigationControlOptions:{position:google.maps.ControlPosition.TOP_RIGHT},center:latlng,mapTypeId:google.maps.MapTypeId.ROADMAP,mapTypeControl:false};this.googleMap=new google.maps.Map(document.getElementById(this.htmlContainer),mapOptions);this._setupZoomLimits();};Map.prototype._setupZoomLimits=function(){var $this=this;google.maps.event.addListener(this.googleMap,'zoom_changed',function(){if($this.googleMap.getZoom()<$this.minZoom){$this.googleMap.setZoom($this.minZoom);}else if($this.googleMap.getZoom()>$this.maxZoom){$this.googleMap.setZoom($this.maxZoom);}});}
Map.prototype.move=function(lat,lng,zoom){if(this.plot){this.googleMap.setCenter(new google.maps.LatLng(lat,lng));if(zoom!==undefined){this.googleMap.setZoom(zoom);}}
return this;};Map.prototype.drawVisits=function(points,options){if(this.plot){this.clearMap();this._drawVisitsMarkers(points);this._afterDrawing(options);}};Map.prototype.drawStops=function(points,options){if(this.plot){this.clearMap();this._drawStopMarkers(points);this._afterDrawing(options);}};Map.prototype.clearMap=function(){this.clearOverlays();this.bounds=new google.maps.LatLngBounds();this._clearMarkers();};Map.prototype._afterDrawing=function(options){var defaultOptions={zoom:true,center:true};$.extend(defaultOptions,options);if(defaultOptions.zoom){this._zoomToBounds();}
if(defaultOptions.center){this._centerToBounds();}};Map.prototype._centerToBounds=function(){this.googleMap.setCenter(this.bounds.getCenter());};Map.prototype._zoomToBounds=function(){this.googleMap.fitBounds(this.bounds);};Map.prototype._drawStopMarkers=function(points){for(var x=0;x<points.length;x++){this._drawStopMarker(points[x]);}};Map.prototype._drawStopMarker=function(point){var $this=this;var defaultMarkerOptions=point.currentCity?Map.DEFAULT_CURRENT_CITY_MARKER_OPTIONS:Map.DEFAULT_MARKER_OPTIONS;if(point.markerOptions){$.extend(defaultMarkerOptions,point.markerOptions);}
point.markerOptions=defaultMarkerOptions;if(point.private){point.markerOptions.imageIcon=Map.BALLOON_MARKER_IMAGE_PRIVATE;}
var marker=this._createMarker(point);google.maps.event.addListener(marker,'click',function(){$this.onClickMarker(point);});return marker;};Map.prototype._clearMarkers=function(){if(this.markerClusterer){this.markerClusterer.clearMarkers();}
if(this.markerManager){this.markerManager.clearMarkers();}};Map.prototype._drawVisitsMarkers=function(points){var circleMarkers=[];for(var x=0;x<points.length;x++){circleMarkers.push(this._createVisitsMarker(points[x]));}
if(circleMarkers.length){this._createManager(circleMarkers);}};Map.prototype._createVisitsMarker=function(point){var $this=this;var position=new google.maps.LatLng(point.latitude,point.longitude);var visits=point.visits+" visit"+(point.visits>1?"s":"");var marker=new VisitsMarker(position,point.visits,this.visitMapper,this.googleMap,{tooltipTitle:point.title+" // "+visits});this.overlays.push(marker);google.maps.event.addListener(marker,'click',function(){$this.onClickMarker(point);});this.bounds.extend(position);return marker;};Map.prototype._createManager=function(markers){var $this=this;if(markers.length>Map.UNCLUSTERED_MAX_SIZE){this._initializeMarkerClusterer(markers);}else{this._initializeMarkerManager();google.maps.event.addListener(this.markerManager,'loaded',function(){$this.markerManager.addMarkers(markers,0);$this.markerManager.refresh();});}};Map.prototype.clearOverlays=function(){for(i in this.overlays){this.overlays[i].setMap(null);}};Map.prototype._initializeMarkerManager=function(){if(!this.markerManager){this.markerManager=new MarkerManager(this.googleMap);}};Map.prototype._initializeMarkerClusterer=function(markers){this.markerClusterer=new MarkerClusterer(this.googleMap,markers,Map._CLUSTERER_CONFIG);};Map.prototype.setZoom=function(zoom){this.googleMap.setZoom(zoom);};Map.prototype._createMarker=function(point){var $this=this;var latLng=new google.maps.LatLng(point.latitude,point.longitude);var icon=new google.maps.MarkerImage(point.markerOptions.iconImage,new google.maps.Size(point.markerOptions.iconWidth,point.markerOptions.iconHeight),new google.maps.Point(0,0),new google.maps.Point(point.markerOptions.iconAnchorX,point.markerOptions.iconAnchorY),new google.maps.Size(point.markerOptions.iconWidth,point.markerOptions.iconHeight));var shadowIcon=new google.maps.MarkerImage(point.markerOptions.shadowImage,new google.maps.Size(point.markerOptions.shadowWidth,point.markerOptions.shadowHeight),new google.maps.Point(0,0),new google.maps.Point(point.markerOptions.shadowAnchorX,point.markerOptions.shadowAnchorY));var marker=new google.maps.Marker({position:latLng,icon:icon,shadow:shadowIcon,title:point.title,map:$this.googleMap});this.overlays.push(marker);this.bounds.extend(latLng);return marker;};Map.prototype.drawArc=function(lat1,lng1,lat2,lng2){var $this=this;this.clearOverlays();var markerOrigin=this._drawStopMarker({latitude:lat1,longitude:lng1});var markerDestiny=this._drawStopMarker({latitude:lat2,longitude:lng2});this.googleMap.fitBounds(this.bounds);var polyline=new google.maps.Polyline({path:[markerOrigin.getPosition(),markerDestiny.getPosition()],strokeColor:"#ff0000",strokeWeight:2,strokeOpacity:1,geodesic:true,map:$this.googleMap});this.overlays.push(polyline);};Map.prototype.enablePlot=function(){this.plot=true;};Map.prototype.disablePlot=function(){this.plot=false;};Map.prototype.isPlotEnabled=function(){return this.plot;};Map.prototype.getMinZoom=function(){return this.minZoom;};Map.prototype.fitToCountry=function(countryCode){var $this=this;var geocoder=new google.maps.Geocoder();var bounds=new google.maps.LatLngBounds();geocoder.geocode({'address':countryCode+' Country'},function(results,status){if(status==google.maps.GeocoderStatus.OK){var bounds=new google.maps.LatLngBounds();for(var i in results){bounds.union(results[i].geometry.viewport);}
$this.googleMap.fitBounds(bounds);}});};Map.prototype.moveToDefault=function(){this.move(this.centerLat,this.centerLng);};Map.prototype.zoomToDefault=function(){this.setZoom(this.initialZoom);}
Map.prototype.drawAddresses=function(addresses,markersImagesContent){this.addressMax=addresses.length;this.addressCount=0;var geocoder=new GeoCoder();var $this=this;$this.clearMap();for(i=0;i<addresses.length;i++){var options={'address':addresses[i]}
geocoder.getResults(options,function(results){var markerOptions={};if(markersImagesContent===undefined){markerOptions=Map.DEFAULT_MARKER_OPTIONS;}else{markerOptions={iconImage:markersImagesContent.eq($this.addressCount).attr('src'),iconWidth:17,iconHeight:30,iconAnchorX:8,iconAnchorY:30};}
var point={title:results[0].geometry.formatted_address,latitude:results[0].geometry.location.lat(),longitude:results[0].geometry.location.lng(),currentCity:false,markerOptions:markerOptions};$this._drawStopMarker(point);$this.addressCount++;if($this.addressMax==$this.addressCount){$this._afterDrawing();}});}}