  var geocoder = new GClientGeocoder();
  var myStreetView = null;
  var myStreetViewClient = null;

  function get_window_size() {
    var windowWidth, windowHeight;

    if (self.innerHeight) 
    { // all except Explorer
      if(document.documentElement.clientWidth){
        windowWidth = document.documentElement.clientWidth; 
      } else {
        windowWidth = self.innerWidth;
      }
      windowHeight = self.innerHeight;
    } else if (  document.documentElement 
              && document.documentElement.clientHeight
              ) 
    { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) 
    { // other Explorers
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    } 
    window.windowWidth = windowWidth;
    window.windowHeight = windowHeight;
  }

  function reset_popup_position() {
    var div_pu_menu  = document.getElementById('popup_menu');

    var pu_width = div_pu_menu.style.width;
    var pu_height = div_pu_menu.style.height;

    var idx = pu_width.indexOf("px"); // remove pixel (px) spec
    if ( idx > 0 ) { pu_width = pu_width.substring(0,idx); }

    var idx = pu_height.indexOf("px"); // remove pixel (px) spec
    if ( idx > 0 ) { pu_height = pu_height.substring(0,idx); }

    //alert("Window width  = "+window.windowWidth);
    //alert("pu_width  = "+pu_width);
    //alert("pu_height = "+pu_height);

    //create reference to common "body" across doctypes
    var standardbody =(document.compatMode=="CSS1Compat")
                     ? document.documentElement 
                     : document.body
                     ;

    var page_x_offset;
    var page_y_offset;
    if ( document.height ) {
      page_x_offset = window.pageXOffset;
      page_y_offset = window.pageYOffset;
    } else {
      page_x_offset = standardbody.scrollLeft;
      page_y_offset = standardbody.scrollTop;
    }

    var pos_left = ((window.windowWidth-pu_width)/2) + page_x_offset;
    div_pu_menu.style.left = pos_left + "px";

    var pos_top  = ((window.windowHeight-pu_height)/2) + page_y_offset;
    div_pu_menu.style.top  = pos_top + "px";
  }

  function scrolled() {

    var div_overlay = document.getElementById('overlay');
    if ( div_overlay.style.display == 'block' ) {
      resized();
    }

  }

  function resized() {
    get_window_size();

    var div_overlay = document.getElementById('overlay');
    div_overlay.style.width = window.windowWidth + "px";

    var docHeight;
    //Firefox
    if ( document.height ) docHeight = document.height;
    //MSIE
    else docHeight = parseInt(document.body.scrollHeight);

    div_overlay.style.height = docHeight + "px"; //window.windowHeight;

    reset_popup_position();
  }

  function toggle_popup(display_on) {
    var div_pu_menu = document.getElementById('popup_menu');
    var div_overlay = document.getElementById('overlay');

    if ( display_on == 0 ) {
      // if disabling, then check for street view object and remove to prevent memory leaks
      if ( myStreetView != null) {
        myStreetView.remove();
        myStreetView = null;
      }       
    } 

    if ( display_on < 0 ) { // toggle
      display_now = ( div_overlay.style.display == 'none' ) ? 'block' : 'none';
    } else {
      display_now = ( display_on == 1 ) ? 'block' : 'none';
    }

    if ( display_now == 'block' ) {
      resized();
    }

    div_overlay.style.display = display_now;
    div_pu_menu.style.display = display_now;
    if ( display_now == 'none' ) {
      var dym = document.getElementById("did_you_mean");
      dym.innerHTML = "";
    }
  }

    var reasons=[];
    // ====== Array for decoding the failure codes ======
    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.";

  function get_geolocation(search_location,cb_on_geocode_failure) {
    //alert("Search: " + search_location);
    geocoder.getLocations(search_location, function (result)
      {
        //alert("Result: " + result.Status.code);
        if (result.Status.code == G_GEO_SUCCESS) {
          // ===== If there was more than one result, "ask did you mean" on them all =====
          if (result.Placemark.length > 1) {
            //alert("found: " + result.Placemark.length);
            var dym = document.getElementById("did_you_mean");
            var html = "Did you mean: (" + result.Placemark.length + ")<ol class='dym_list'>";
            // Loop through the results
            for (var i=0; i<result.Placemark.length; i++) {
              var p = result.Placemark[i].Point.coordinates;
              html
                      += "<li>"
                      +"<a href='javascript:geocode_update(" +p[1]+","+p[0]+")'>"
                      + result.Placemark[i].address+"</a></li>";
            }
            html += "</ol>";
            dym.innerHTML = html;
             
            var dymh = document.getElementById("did_you_mean_header");
            dymh.innerHTML = "Multiple Matches Found";

            toggle_popup(1);
          } else {
            // ===== If there was a single marker =====
            document.getElementById("did_you_mean").innerHTML = "";
            var p = result.Placemark[0].Point.coordinates;
            geocode_update(p[1],p[0]);
          }
        }
        // ====== Decode the error status ======
        else {
          if ( cb_on_geocode_failure == undefined ) {
            var reason="Code "+result.Status.code;
            if (reasons[result.Status.code]) {
              reason = reasons[result.Status.code]
            }
            alert('Could not find "'+search_location+ '" ' + reason);
          } else {
            cb_on_geocode_failure(search_location, reason);
          }
        }
      }
    );
  }


