var map = null;  //  This is set to null so that you can test
                 //  to see if they actually get created
                 //  ( if ( map == null ) { alert ( 'oops!' ); } )
var proj;

        // avoid pink tiles
        OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;
        OpenLayers.Util.onImageLoadErrorColor = "transparent";

function init(){
  var options = {
                projection: new OpenLayers.Projection("EPSG:900913"),
                displayProjection: new OpenLayers.Projection("EPSG:4326"),
                units: "m",
//                maxZoomLevel: 13,
//                minZoomLevel: 3,
//                numZoomLevels: 12,
                maxResolution: "auto",
//                maxResolution: 39135.7584820001,
//                maxResolution: 156543.0339,
//                maxExtent: new OpenLayers.Bounds(-20037508, -20037508,
//                                                 20037508, 20037508.34),
                controls: []                                 
  };
  map = new OpenLayers.Map( $('ngmdb_map'), options);
  //alert(OpenLayers._getScriptLocation());

//            terrain = new OpenLayers.Layer.ArcGIS93Rest( "ESRI Shaded Relief",
//                    "http://server.arcgisonline.com/ArcGIS/rest/services/World_Terrain_Base/MapServer/export", 
//                    {layers: "show:0,2"});

//            layer = new OpenLayers.Layer.ArcGIS93Rest( "Geologic Map of N.A.",
//                    "http://bmproto.esri.com/ArcGIS/rest/services/Geology/NationalGeology/MapServer/export", 
//                    {layers: "show:0,2"},
//                    {isBaseLayer: false, visibility: false});

            reference3 = new OpenLayers.Layer.XYZ( "ESRI",
                    "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/${z}/${y}/${x}",
                    {sphericalMercator: true,
                     wrapDateLine: true,
                     minZoomLevel: 3,
//                     maxZoomLevel: 12,
                     numZoomLevels: 12}
                    );

//            var ms_gmna = new OpenLayers.Layer.MapServer( "Bedrock", 
//                    "http://roguemap.geos.pdx.edu/cgi-bin/mapserv.cgi?", 
//                    {map: '/vol/www/ngmdb/htmaps/map/geomat_ngmdb_pending_wm.map', layers: "GMNA" },
//                    {singleTile: true, isBaseLayer: false, opacity: 50} );

//            var ms_surficial = new OpenLayers.Layer.MapServer( "Surficial Materials",
//                    "http://roguemap.geos.pdx.edu/cgi-bin/mapserv.cgi?", 
//                    {map: '/vol/www/ngmdb/htmaps/map/geomat_ngmdb_pending_wm.map', layers: "Surficial" },
//                    {singleTile: true, isBaseLayer: false, opacity: 50} );

                    
//           layer.setOpacity(0.7);
//            ms_gmna.setOpacity(0.7);
//            ms_surficial.setOpacity(0.7);
//            reference.setOpacity(0.7);
            map.addLayers([reference3]);

            map.addControl(new OpenLayers.Control.MouseDefaults());

//make layerswitcher open by default, uncomment this to allow GMNA to toggle 
            //var ls = new OpenLayers.Control.LayerSwitcher()
            //map.addControl(ls);
            //ls.maximizeControl();
            

//Make a zoom bar WITHOUT other widgets
            var panZoomBar = new OpenLayers.Control.PanZoomBar();
            map.addControl(panZoomBar);
            for (var p = 0; p < 4; p++) {
                panZoomBar.buttons[p].style.display = 'none';        // Don't display panning buttons
            }
            panZoomBar.div.style.marginTop = "-50px";                // Move ZoomBar up 
//end of zoombar definition hack

  map.addControl(new OpenLayers.Control.MousePosition({element:$('ngmdb_map_position'),prefix:"Lat:&nbsp;",separator:"&nbsp;&nbsp;Lon:&nbsp;",numDigits:4}));
  map.addControl(new OpenLayers.Control.Scale($('ngmdb_map_scale')));

  map.setCenter(new OpenLayers.LonLat(-10686671, 4721672), 4);

    proj = new OpenLayers.Projection("EPSG:4326");

    map.events.register("moveend", map, function() {
    cur_extent = this.getExtent().toBBOX();
    cur_extent_array = cur_extent.split(",");
    //alert(cur_extent_array);
    minxy = new OpenLayers.LonLat(cur_extent_array[0], cur_extent_array[1]);
    minxy.transform(map.getProjectionObject(), proj);
    maxxy = new OpenLayers.LonLat(cur_extent_array[2], cur_extent_array[3]);
    maxxy.transform(map.getProjectionObject(), proj);
    //alert(minxy);
    //alert(maxxy);
    $('kaminx').value = minxy.lon;
    $('kaminy').value = minxy.lat;
    $('kamaxx').value = maxxy.lon;
    $('kamaxy').value = maxxy.lat;
    $('kazoom').value = map.getZoom();
    if ($('map_init').value == 1) {
      $('useextents').checked = true;
    } else {
      $('map_init').value = 1;
    }
    changeMap();
   });

/*
  alert("kaminx value = " + $('kaminx').value +
        "\nkaminy value = " + $('kaminy').value +
        "\nkamaxx value = " + $('kamaxx').value +
        "\nkamaxy value = " + $('kamaxy').value +
        "\nkazoom value = " + $('kazoom').value);
*/
  restoreMap();
}

function restoreMap() {
  if ($('kazoom').value == "") {
    map.setCenter(new OpenLayers.LonLat(-10686671, 4721672),4);
    cur_extent = map.getExtent().toBBOX();
    cur_extent_array = cur_extent.split(",");
    minxy = new OpenLayers.LonLat(cur_extent_array[0], cur_extent_array[1]);
    minxy.transform(map.getProjectionObject(), proj);
    maxxy = new OpenLayers.LonLat(cur_extent_array[2], cur_extent_array[3]);
    maxxy.transform(map.getProjectionObject(), proj);
    $('kaminx').value = minxy.lon;
    $('kaminy').value = minxy.lat;
    $('kamaxx').value = maxxy.lon;
    $('kamaxy').value = maxxy.lat;
    $('kazoom').value = map.getZoom();
  } else {
    map_wlon = Math.round($('kaminx').value * 10000) / 10000;
    map_slat = Math.round($('kaminy').value * 10000) / 10000;
    map_elon = Math.round($('kamaxx').value * 10000) / 10000;
    map_nlat = Math.round($('kamaxy').value * 10000) / 10000;
    map_zoom = $('kazoom').value;
/*
    alert("extents = " + document.getElementById('extents').value +
      "\nmap_wlon = " + map_wlon +
      "\nmap_slat = " + map_slat +
      "\nmap_elon = " + map_elon +
      "\nmap_nlat = " + map_nlat +
      "\nmap_zoom = " + map_zoom);
*/
//    if (document.getElementById('extents').value == 1) {
      restore_tab();
      setExtents(map_wlon, map_slat, map_elon, map_nlat, map_zoom);
//    }
    updateHits();
  }
}

function changeMap () {
  //alert("In changeMap.\nextents = " + $('extents').value);
  if (document.getElementById('useextents').checked) {
//  if ($('extents').value != 0) {
//    document.getElementById('useextents').checked = true;
//  }
    updateHits();
  }
}

function reinitExtentsLoad () {
  if (! $('useextents').checked) {
    $('map_init').value = 0;
  }
}

function flagExtents() {
  document.getElementById('extents').value = 1;
}

function hilite_tab(ele) {
//    if (ele.id != document.getElementById('cur_id').value) {
      ele.style.borderStyle='outset';
//    }
}

function reg_tab(ele) {
    if (ele.id != document.getElementById('cur_id').value) {
      ele.style.borderStyle='solid';
    } else {
      ele.style.borderStyle='inset';
    }
}

function change_tab(ele, c_lon, c_lat, new_level) {
    //alert ("c_lon = " + c_lon + "\nc_lat = " + c_lat + "\nnew_level = " + new_level);
    cur_id_value = document.getElementById('cur_id').value;
    if (ele.id != cur_id_value) {
      document.getElementById(cur_id_value).style.borderStyle='solid';
      document.getElementById(cur_id_value).style.backgroundColor='#ffdd00';
      ele.style.borderStyle='inset';
      ele.style.backgroundColor='#ffff00';
      document.getElementById('cur_id').value = ele.id;
    }
    flagExtents();
    ctrxy = new OpenLayers.LonLat(c_lon, c_lat);
    ctrxy.transform(proj, map.getProjectionObject());
    //alert(c_lat + " => " + ctrxy.lat + ", " + c_lon + " => " + ctrxy.lon);
    map.setCenter(new OpenLayers.LonLat(ctrxy.lon,ctrxy.lat),new_level);
    if (document.getElementById('useextents').checked) {
      updateHits();
    }
}

function restore_tab() {
    cur_id_value = document.getElementById('cur_id').value;
    if ("cus" != cur_id_value) {
      document.getElementById('cus').style.borderStyle='solid';
      document.getElementById('cus').style.backgroundColor='#ffdd00';
      document.getElementById(cur_id_value).style.borderStyle='inset';
      document.getElementById(cur_id_value).style.backgroundColor='#ffff00';
    }
}

function validate_form () {
//  if (! document.getElementById('useextents').checked) {
//    resetExtents();
//  }
  if (document.getElementById('extenttype').checked) {
    if (! document.getElementById('useextents').checked){
      var msg = "You have requested a point search without selecting a" +
                "\nMap Area.  No point search will be done.\n\nDo you " +
                "wish to continue (OK) or Cancel the search\nso you can " +
                "select a map area?";
      if (confirm(msg))
        return true;
      else
        return false;
    }
  }
  return true;
}

function resetUseExtents () {
  if (document.getElementById('useextents').checked) {
    document.getElementById('useextents').checked = false;
    document.getElementById('extenttype').checked = false;
//    resetExtents();
  }
}

function resetUseExtents2 () {
  if (document.getElementById('extenttype').checked) {
    document.getElementById('useextents').checked = true;
    resetBoundCoords();
  } else {
    updateHits();
  }
}

function setUseExtents () {
  if (document.getElementById('extenttype').checked) {
    document.getElementById('useextents').checked = true;
  }
  updateHits();
}

function resetExtentType () {
  if (! document.getElementById('useextents').checked) {
    document.getElementById('extenttype').checked = false;
  }
  updateHits();
}

function resetBoundCoords () {
  if (document.getElementById('useextents').checked) {
    document.getElementById('bc_ul').value = "";
    document.getElementById('bc_lr').value = "";
  } else {
    document.getElementById('extenttype').checked = false;
  }
  updateHits();
}

function resetExtents() {
document.getElementById('kaminx').value = "";
document.getElementById('kaminy').value = "";
document.getElementById('kamaxx').value = "";
document.getElementById('kamaxy').value = "";
$('kazoom').value = "";
}

function setExtents(minx, miny, maxx, maxy, zlev) {
  s_lon = (minx + maxx) / 2;
  s_lat = (miny + maxy) / 2;
/*
  alert ("s_lon = " + s_lon + "\ns_lat = " + s_lat + "\nzlev = " + zlev);
*/
  s_cntr = new OpenLayers.LonLat(s_lon, s_lat);
  s_cntr.transform(proj, map.getProjectionObject());
  map.setCenter(s_cntr,zlev);
}

