if (Drupal.jsEnabled) {
  $(document).ready(function() {
    if (!GBrowserIsCompatible()) {
      return;
    }
    
    if ($("#sbx-location-info").length <= 0) {
      return;
    }

    var miles =  SbxGmapZipView_convertProximityCodeToMiles ($("#sbx-proximity-code").html());
    //var zip_code_field = $("#sbx-postal-code");
    //var zip_code = $("#sbx-postal-code").html();
    var lat = $("#sbxPost_lat").html();
    var lng = $("#sbxPost_lng").html();
  
    // ---  For Future Use
    //var state_short   = $("[@name='sbxPost_state_short']").fieldValue ();
    //var country_code  = $("[@name='sbxPost_country_code']").fieldValue ();
    //var city_or_place = $("[@name='sbxPost_city_or_place']").fieldValue ();
    //var postal_code   = $("[@name='sbxPost_postal_code']").fieldValue ();
    // ---
    
    var gmap_canvas = $('<div id="map_canvas" class="mysbxMapCanvas"></div>');
    var gmap_init_flag = false;

    var gmap_toggle_switch = $('<a id="map_toggle" class="action sbx-location-map-toggle" toggle="hide" href="#" style="margin-left:8px">Show on Map</a>');
    var gmap_toggle_flag = false;
    gmap_toggle_switch.insertAfter ($("#sbx-location-info").parent());
    gmap_toggle_switch.bind ('click', function () {
      if (gmap_toggle_flag) {
        gmap_toggle_switch.text ('Show on Map');
        gmap_toggle_switch.attr ('toggle', 'hide');
        gmap_toggle_flag = false;
        hide_gmap ();
      }
      else {
        gmap_toggle_switch.text ('Hide Map');
        gmap_toggle_switch.attr ('toggle', 'show');
        gmap_toggle_flag = true;
        show_gmap ();
      }
      return false;
    });
    
    function show_gmap () {
      if (!gmap_init_flag) {
        //gmap_canvas.insertAfter ($("#sbx-location-info"));
        gmap_canvas.insertAfter ($("#map_toggle"));
        SbxGmapZipView_initGmapView ();
        SbxGmapZipView_setCenterMarkDraggable (false);
        SbxGmapZipView_setProximityMiles (miles);
        SbxGmapZipView_centerMapByLatlng (new GLatLng (lat, lng, true));
        //SbxGmapZipView_centerMapByZipCode (zip_code);
        gmap_init_flag = true;
      }
      gmap_canvas.show ('slow');
    }
    
    function hide_gmap () {
      if (gmap_init_flag) {
        gmap_canvas.hide ('slow');
      }
    }
  });
}
