﻿// Requires mf_Dom_library for addLoadEvent function 
// Requires prototype library

function loadBasicGoogleMapJSWithLocations(MapDivName, MapPointsArrayName) { 
  if (GBrowserIsCompatible()) {

    //Creates actual Google Map
    var map = new GMap2(document.getElementById(MapDivName));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    //Addition 1:Set MapCenter to (new GLatLng(0,0),0) if custom fitting zoom and lat/long center to fit marker points
    map.setCenter(new GLatLng(0, 0, 0));
    //Addition 1:Line below creates bounds to configure dynamic fit for markers
    var MapBounds = new GLatLngBounds();
  
      //Sets Visual attributes of markers created for the map; image and size
    function createIcon(point) {
        var nIcon = new GIcon();
        
        nIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
        nIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
        
        nIcon.iconSize = new GSize(12, 20);
        nIcon.shadowSize = new GSize(22, 20);
        
        nIcon.iconAnchor = new GPoint(6, 20);
        nIcon.infoWindowAnchor = new GPoint(5, 1);
        return new GMarker(point, {icon:nIcon});
    };
  
  
    // Creates a marker at the given point with the given number label, and sets size and image of marker
    function createMarker(point, text) {
	  //Addition 1:Line Below extends the MapBounds to include that point
      MapBounds.extend(point);
      var marker = createIcon(point);
      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml("<b>" + text + "</b>");
      });
      return marker;
    };
  
      //For each item in the Array MPoints (created in the ASHX file) create a marker 
    //from a breakdown of the array item's value using "|" as the splitter value.
    //Currently the value breaks down into: Latitude (as a double), Longitude(as a double),
    //popup window text(as text)
	MapPointsArrayName.each(function(value, index) {
        var pointParts = value.split("|");
            var point = new GLatLng(pointParts[0],pointParts[1]);
            map.addOverlay(createMarker(point, pointParts[2]));       
    });
    
  //Addition 1:Once all map markers/functions have fired re-center and re-zoom map to fit all markers

     map.setZoom(map.getBoundsZoomLevel(MapBounds));
     map.setCenter(MapBounds.getCenter());
    
    //Point of centered Data
    //var point = new GLatLng([GG_LATITUDE],[GG_LONGITUDE]);
    //map.addOverlay(createMarker(point, '[MF_ADDRESS]'));
    
    // An Event listener that gets map zoom level when zoomed
    //GEvent.addListener(map, "zoomend", function() {
            //alert(map.getZoom());
    //});
  };
};
    
//Fires of Google map load function after Page Load  
//addLoadEvent(loadBasicGoogleMapJSWithLocations('BasicSingleGoogleMapOne', 'BasicGooglePointsArrayOne'));
    