﻿// JScript File

var actualCoordinates ;
var zoomSize = 5;
var geocoder ;
var map ;
var reasons = [ ] ;
reasons[G_GEO_SUCCESS]            = "Success" ;
reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value." ;
reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address." ;
reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons." ;
reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given" ;
reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded." ;
reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed." ;

var accuracy = [ ] ;
accuracy[0] = 'Unknown' ;
accuracy[1] = 'Country' ;
accuracy[2] = 'Region' ;
accuracy[3] = 'Sub-region' ;
accuracy[4] = 'Town' ;
accuracy[5] = 'Post code' ;
accuracy[6] = 'Street' ;
accuracy[7] = 'Intersection' ;
accuracy[8] = 'Address' ;      

function showAddress(address, alternativeAddress)
{
/*   alert("address = " + address);
   alert("alt address = " + alternativeAddress);*/

 var mapDiv = document.getElementById("GoogleMapGadget");
 
 if (mapDiv != null)
 {  map.clearOverlays() ;
   geocoder.getLocations(address, function (result) 
    { 
     var found = 0;
     
     if (result.Status.code == G_GEO_SUCCESS)
     {
      found = 1;
      for (var i=0; i<result.Placemark.length; i++)
      {
       actualCoordinates = result.Placemark[i].Point.coordinates;
       var marker = new GMarker(new GLatLng(actualCoordinates[1],actualCoordinates[0]),{title:address + " Accuracy: " + accuracy[result.Placemark[i].AddressDetails.Accuracy]});
       map.addOverlay(marker);
      }
      actualCoordinates = result.Placemark[0].Point.coordinates;
      map.setCenter(new GLatLng(actualCoordinates[1],actualCoordinates[0]), zoomSize);
     } else
     {
      var reason="Code "+result.Status.code;
      if (reasons[result.Status.code])
      {
       reason = reasons[result.Status.code]
      } 
      /*alert('Could not find "' + address + '" ' + reason);*/
      
    if (found != 1)
    {
        /*alert("alt 2 address = " + alternativeAddress);*/
        map.clearOverlays() ;
        geocoder.getLocations(alternativeAddress, function (result) 
        { 
         if (result.Status.code == G_GEO_SUCCESS)
         {
          for (var i=0; i<result.Placemark.length; i++)
          {
           actualCoordinates = result.Placemark[i].Point.coordinates;
           var marker = new GMarker(new GLatLng(actualCoordinates[1],actualCoordinates[0]),{title:address + " Accuracy: " + accuracy[result.Placemark[i].AddressDetails.Accuracy]});
           map.addOverlay(marker);
          }
          actualCoordinates = result.Placemark[0].Point.coordinates;
          map.setCenter(new GLatLng(actualCoordinates[1],actualCoordinates[0]), zoomSize);
         } else
         {
          var reason="Code "+result.Status.code;
          if (reasons[result.Status.code])
          {
           reason = reasons[result.Status.code]
          } 
          found = 0;
          /*alert('Could not find "' + address + '" ' + reason);*/
         }
        } ) ;
    }
      
     }
    } ) ;
  }  
}

function googleMapGadgedOnLoad(startAddress, alternativeAddress)
{var mapDiv = document.getElementById("GoogleMapGadget");
 if (mapDiv != null)
 {
   map = new GMap2(mapDiv);
   var start = new GLatLng(35.127771,-89.967041);
   actualCoordinates = start;
   map.disableGoogleBar();
   map.setCenter(start, zoomSize);
   map.disableDoubleClickZoom();
   GEvent.addListener(map, "dblclick", function() {
                window.open("./GoogleMap.aspx","GoogleMap",
                'width=630, height=560, ' +
                'location=no, menubar=no, ' +
                'status=no, toolbar=no, scrollbars=no, resizable=no');
              });   
/*   map.addControl(new GLargeMapControl());
   map.addControl(new GScaleControl()) ;*/
   map.addControl(new TextualZoomControl());
   geocoder = new GClientGeocoder() ;
   
//   alert(startAddress);
   showAddress(startAddress, alternativeAddress);
//   alert(startAddress);
 }
}

// A TextualZoomControl is a GControl that displays textual "Zoom In"
// and "Zoom Out" buttons (as opposed to the iconic buttons used in
// Google Maps).

// We define the function first
function TextualZoomControl() {
}

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
TextualZoomControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
TextualZoomControl.prototype.initialize = function(map) {
  var container = document.createElement("div");

  var zoomInDiv = document.createElement("div");
  this.setButtonStyle_(zoomInDiv);
  container.appendChild(zoomInDiv);
  zoomInDiv.appendChild(document.createTextNode(" + "));
  GEvent.addDomListener(zoomInDiv, "click", function() {
    window.open("./GoogleMap.aspx","GoogleMap",
    'width=630, height=560, ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, scrollbars=no, resizable=no');
  });

  /*var zoomOutDiv = document.createElement("div");
  this.setButtonStyle_(zoomOutDiv);
  container.appendChild(zoomOutDiv);
  zoomOutDiv.appendChild(document.createTextNode(" - "));
  GEvent.addDomListener(zoomOutDiv, "click", function() {
    map.zoomOut();
  });*/

  map.getContainer().appendChild(container);
  return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
TextualZoomControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}

// Sets the proper CSS for the given button element.
TextualZoomControl.prototype.setButtonStyle_ = function(button) {
  button.style.textDecoration = "open";
  button.style.color = "#000088";
  button.style.backgroundColor = "white";
  button.style.font = "10pt Arial";
  button.style.fontWeight = "normal";
  button.style.border = "1px solid black";
  /*button.style.padding = "2px";*/
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "1pc";
  button.style.height = "1pc"
  button.style.cursor = "pointer";
}

