function loadGoogleMaps(elmID, centerPoint, mapDistance, miniMap) {
     try{
          var zoomLevel = computeZoomLevel(mapDistance);

          if(centerPoint){
               if (GBrowserIsCompatible()) {

                    mapInstance = new GMap2(document.getElementById(elmID));
                    
                    if(!miniMap){
                         mapInstance.addControl(new GSmallZoomControl());
                    }
                    
                    addCenterMarker(centerPoint.latitude, centerPoint.longitude);
                    mapInstance.setCenter(new GLatLng(centerPoint.latitude, centerPoint.longitude), zoomLevel);
               }
          }
     }catch(e){
          return;
     }
}

function loadProviders(pageNumber){
     mapMarkers = new Array();
 
     //Clear all the exisiting overlays
     mapInstance.clearOverlays();
     
     //readd the center marker
     addCenterMarker(mapCenter.latitude, mapCenter.longitude);

     for(var i=0; i < mapProviders.length; i++){
          var provider = mapProviders[i];
          
          /* need to clear markers prior to inserting them to ensure they are empty 
          and not being reused by a secondary process such as sorting or reordering them */
          provider.markers = new Array();
          
          /* Only load the providers that appear on that page
             Note that pages are numbered from 1 onward, where
             our array indexes start at 0. So we subtract one
             from the pageNumber param to make up this difference
          */
          if(provider.onPage == (pageNumber-1)){
               for(var c=0; c < mapLocations.length; c++){
                    var location = mapLocations[c];
                   
                    if(location.providerIDs.hasElement(provider.providerID)){
                    
                         /* Only add locations that have coords */
                         if(parseInt(location.latitude, 10) != 0 && parseInt(location.longitude, 10) != 0){
                              var markerID = "";
                              
                              if(location.providerIDs.length > 1){
                                   markerID = createMarker(location.latitude,
                                                           location.longitude, 
                                                           location.providerIDs.length ,
                                                           'dup',
                                                           '',
                                                           '',
                                                           '');
                                   //Since the marker for this provider is now a num, we update the array
                                   provider.markerColor = location.providerIDs.length;
                              } else {
                                   markerID = createMarker(location.latitude,
                                                location.longitude,
                                                provider.markerColor,
                                                provider.providerID,
                                                provider.providerName,
                                                location.City,
                                                location.State);
                              }
                              
                              provider.markers.push(markerID);
                         }
                    }
               }
          }
     }

}



function addCenterMarker(lat, lng){
     // Create a custom icon for wellclicks
     var wcIcon = new GIcon(G_DEFAULT_ICON);

     if (mapType == "matches")
          wcIcon.image = "images/gmap_mapcenter.png";
     else
          wcIcon.image = "images/gmap_mapcenter_provider.png";
     wcIcon.shadow = "images/gmap_shadow.png";
     wcIcon.iconSize = new GSize(27, 41);
     wcIcon.shadowSize = new GSize(1, 1);
     wcIcon.iconAnchor = new GPoint(3, 35);
     
     // Set a title
     var gTitle = (mapType && mapType == "matches") ? "Your" : "Provider's";
     gTitle += " approximate location";
     // Set up our GMarkerOptions object
     markerOptions = { icon:wcIcon, title:gTitle };
     
     // Position the marker
     var point = new GLatLng(lat,lng);
     
     // Create the marker
     var marker = new GMarker(point, markerOptions);
     
     // Add the marker to the top.map
     mapInstance.addOverlay(marker, markerOptions);
}

function computeZoomLevel(distance) {     
     // Set Google Maps Zoom Level
     // 12 is about 5-7 miles radius
     // 9 is about 50-60 miles radius
     
     switch(parseInt(distance, 10)){
          case 5:
               return 11;
               break;
          case 10:
               return 10;
               break;
          case 25:
               return 9;
               break;
          case 50: default:
               return 8;
               break;
     }
}
/*
if(!GMap2.recenter){
     GMap2.prototype.recenter = function recenter(newCoords){
          newCenter = new GLatLng(newCoords.latitude, newCoords.longitude)
          this.panTo(newCenter)
     }
}
*/

function highlightMarker(providerID) {
     for(var m = 0; m < mapMarkers.length; m++){
          var marker = mapMarkers[m];
          marker.hide();
     }
     for(var p=0; p < mapProviders.length; p++){
          var provider = mapProviders[p];
          
          if(provider.providerID == parseInt(providerID,10)){
               var x = provider.markers.toString();

               var myArr = x.split(',');
               for (var i=0; i<myArr.length; i+=1) {
                    if(!myArr[i].empty()){
                         if(typeof(test)==="undefined")
                              mapMarkers[myArr[i]].show();
                    }
               }              
               return;
          }
     }
}

function unhighlightMarker(providerID) {
     for(var m = 0; m < mapMarkers.length; m++){
          var marker = mapMarkers[m];
          marker.show();
     }
}

function createMarker(lat, lng, color, id, name, city, state) {
     // Create a custom icon for wellclicks
     var wcIcon = new GIcon(G_DEFAULT_ICON);

     wcIcon.image = "images/gmap_marker_"+color+".png";
     wcIcon.shadow = "images/gmap_shadow.png";
     wcIcon.iconSize = new GSize(29, 29);
     wcIcon.shadowSize = new GSize(1, 1);
     wcIcon.iconAnchor = new GPoint(12, 12);
     
     // Set a title
     var gTitle = (id == "dup") ? color+" providers share this location." : name+", "+city+" "+state;
     
     // Set up our GMarkerOptions object
     markerOptions = { icon:wcIcon, title:gTitle };
     
     // Position the marker
     var point = new GLatLng(lat,lng);
     
     // Create the marker
     var marker = new GMarker(point, markerOptions);
     
     // Add to Marker Array
     mapMarkers[mapMarkers.length] = marker;
     
     // Add the marker to the top.map
     mapInstance.addOverlay(marker, markerOptions);
     
     // return our markerID 
     return (mapMarkers.length - 1);
}

function showAddress(id) {
     $("#locations_address div").css("display", "none");
     $("#locations"+id).css("display", "block");

     for(var i = 0; i < mapLocations.length; i++){
          var location = mapLocations[i];
          
          if(location.locationID == id){
               mapCenter = {"latitude": location.latitude,
                            "longitude":location.longitude};
          }
     }
}

function reorderMarkers(maxPage){
     for(var i = 1; i < maxPage+1; i++){
          var position = 0;
          
          //Loop over each page
          $("#page"+ i).children(".profile").each(function(){
               var providerID = parseInt($(this).attr("alt"), 10);
               
               //And update the map Providers onPage 
               for(var c = 0; c < mapProviders.length; c++){
                    var provider = mapProviders[c];
                    
                    // if the id's match
                    if(provider.providerID == providerID){
                         provider.onPage = (i-1);
                    }
               }
               
               position++;
          });
     }
}
