﻿// First, we have to load the APIs.
google.load('maps' , '2');
google.load('search' , '1');

// Global variables we will set in OnLoad
var map;
var searcher;
var geoInfo;

function SetMap(params) {

  // Get the content div and clear it's current contents.
  var contentDiv = document.getElementById(params.mapContainer);
  contentDiv.innerHTML = ''; 
  
  var searchBox = document.getElementById(params.searchBoxContainer);
  searchBox.innerHTML = ''; 
  
  var mapContainer = document.createElement('div'); // build the map div
  mapContainer.style.height = params.mapContainerHeight;
  mapContainer.style.width = params.mapContainerWidth; 

  var controlContainer = document.createElement('div'); // build the control div

  // Now we have to add these divs to the content div in the document body
  contentDiv.appendChild(mapContainer);
  searchBox.appendChild(controlContainer);

  // We're ready to build our map...
  map = new google.maps.Map2(mapContainer);

  // Add map controls if needed 
  if (params.mapAddControls)
    map.addControl(new google.maps.SmallMapControl()); // Add a small map control
  

  // We'll wait to the end to actually initialize the map
  var searchControl = new google.search.SearchControl();

  // Initialize a LocalSearch instance
  searcher = new google.search.LocalSearch(); // create the object
  searcher.setCenterPoint(map); // bind the searcher to the map

  // Create a SearcherOptions object to ensure we can see all results
  var options = new google.search.SearcherOptions(); // create the object
  options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
  
  if(params.resultsContainer != undefined)
    options.setRoot(document.getElementById(params.resultsContainer));

  // Add the searcher to the SearchControl
  searchControl.addSearcher(searcher , options);

 // And second, we need is a search complete callback!
  searchControl.setSearchCompleteCallback(searcher , function() {
    map.clearOverlays();

    var shelterIcon = new GIcon();
    shelterIcon.image = '/i/sl/image.png';
    shelterIcon.shadow = '/i/sl/shadow.png';
    shelterIcon.iconSize = new GSize(49,65);
    shelterIcon.shadowSize = new GSize(82,65);
    shelterIcon.iconAnchor = new GPoint(25,65);
    shelterIcon.infoWindowAnchor = new GPoint(25,0);
    shelterIcon.printImage = '/i/sl/printImage.gif';
    shelterIcon.mozPrintImage = '/i/sl/mozPrintImage.gif';
    shelterIcon.printShadow = '/i/sl/printShadow.gif';
    shelterIcon.transparent = '/i/sl/transparent.png';
    shelterIcon.imageMap = [31,1,34,2,36,3,38,4,39,5,40,6,41,7,42,8,43,9,44,10,45,11,45,12,46,13,46,14,46,15,47,16,47,17,47,18,47,19,48,20,48,21,48,22,48,23,48,24,48,25,48,26,48,27,48,28,47,29,47,30,47,31,47,32,46,33,46,34,46,35,45,36,45,37,44,38,43,39,42,40,41,41,40,42,39,43,38,44,36,45,34,46,34,47,33,48,33,49,32,50,32,51,31,52,31,53,30,54,30,55,29,56,28,57,28,58,27,59,27,60,26,61,26,62,25,63,25,64,24,64,24,63,23,62,23,61,22,60,22,59,21,58,21,57,20,56,20,55,19,54,19,53,18,52,18,51,17,50,16,49,16,48,15,47,14,46,13,45,11,44,9,43,8,42,8,41,7,40,6,39,5,38,4,37,4,36,3,35,2,34,2,33,2,32,1,31,1,30,1,29,0,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,1,19,1,18,1,17,1,16,2,15,2,14,2,13,3,12,3,11,4,10,5,9,6,8,7,7,8,6,9,5,10,4,12,3,14,2,17,1];
    
    // Set up our GMarkerOptions object
    markerOptions = {icon: shelterIcon};
    
    var results = searcher.results; // Grab the results array
    $('#NoResults').hide();
    if(results.length == 0)
        $('#NoResults').show();
      
    
    // We loop through to get the points
    for (var i = 0; i < results.length; i++) {
      var result = results[i]; // Get the specific result
      var markerLatLng = new google.maps.LatLng(parseFloat(result.lat),
                                                parseFloat(result.lng));
      var marker = new google.maps.Marker(markerLatLng, markerOptions ); // Create the marker

      // Bind information for the infoWindow aka the map marker popup
      marker.bindInfoWindow(result.html.cloneNode(true));
      result.marker = marker; // bind the marker to the result
      map.addOverlay(marker); // add the marker to the map
    }

    // Store where the map should be centered
    var center = searcher.resultViewport.center;
    
    // Calculate what the zoom level should be
    var ne = new google.maps.LatLng(searcher.resultViewport.ne.lat,
                                    searcher.resultViewport.ne.lng);
    var sw = new google.maps.LatLng(searcher.resultViewport.sw.lat,
                                    searcher.resultViewport.sw.lng);
    var bounds = new google.maps.LatLngBounds(sw, ne);
    var zoom = map.getBoundsZoomLevel(bounds, new google.maps.Size(params.mapContainerHeight, params.mapContainerWidth));

    // Set the new center of the map
    // parseFloat converts the lat/lng from a string to a float, which is what
    // the LatLng constructor takes.
    map.setCenter(new google.maps.LatLng(parseFloat(center.lat),
                                         parseFloat(center.lng)),
                                         zoom);
  });

  // Draw the control
  searchControl.draw(controlContainer);
 
  // LoadGeo Data
  loadGeoData()

  map.setCenter(new google.maps.LatLng(geoInfo.latitude, geoInfo.longitude), 11);

 // Execute an initial search
  searchControl.execute('shelters');

  return map;
}

function loadGeoData()
{
      $.ajax({
        type: "POST",
        url: "/services/GeoIp.asmx/GetGeoInformation",
        data: "{}",
        async: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            geoInfo = eval("(" + msg.d + ")");
        }
     });
}

function UpdateMap(zipcode)
{
    searcher.execute(zipcode + ' shelters');
}