/***************************************************************************************************************
Revisions:
Rev: 405
Function: realTimeInit
Changes: Fixed up the PlatformGML GML layer. GML layer only has options hash, not a parameters and options hash.
         Also added hash option(displayInLayerSwitcher: false) to not display GML layer in the layer switcher.
          
***************************************************************************************************************/

function loadMap()
{
  //debugger;
  if( document.URL.match("index.php") )
  {
    realTimeInit();
  }
  else if( document.URL.match("indexArchived.php") )
  {
    archiveInit();
  }
  else
  {
    realTimeInit();
  }
}
/*################################################################################################
 There are 3 openLayers.Layer options I've added to the options hash:
  LAYERURL: The is the link to the data provider. I use this in the tree control to make the check box text clickable.
  GROUP: This is used by the layer tree to group the layers together in a parent/leaf arrangement. All like GROUPS end up under the same parent.
  QUERYABLE: Specifies if a given layer is clickable for a GetFeatureInfo request. Will popup an Info icon next to the active query layer.
  legend: Specifies the image file to use as the legend in the layer tree control.
################################################################################################*/

//Thanks to Jessie Cleary for the transparent PNG issue/resolution for IE6
var browser = navigator.appName;
var version = navigator.appVersion;
var version1 = version.substring(22, 25);
var ImageType = 'image/png';
var TileCacheMod = '';  //Modifier for our TileCache layers. For ie6 we use a specific layer to avoid the png transparency issue.
if (browser == "Microsoft Internet Explorer" && version1 == "6.0")
{
  ImageType = 'image/png1';
  TileCacheMod = 'ie6';
}

var map;
var layerSwitcherExt;
function realTimeInit()
{

  var url;
  var icon;
  var lat=33.8;
  var lon=-77.9;       
  var zoom = 4;
  var OFFSET = '2';

  var strTileCacheURL = 'http://129.252.37.86/tilecache/tilecache.cgi?';
  var strMapservURL   = 'http://129.252.37.86/cgi-bin/mapserv?';
  var strMapFile      = '/home/rcoos/mapserver/RCOOS.map';
  OpenLayers.ProxyHost="http://carolinasrcoos.org/cgi-bin/proxy.php?url=";
  //OpenLayers.ProxyHost="http://rcoos.org/carolinasrcoosrev2/cgi-bin/proxy.php?url=";
  
  OpenLayers.Util.onImageLoadErrorColor = "transparent";
  
  layerSwitcherExt = new OpenLayers.Control.LayerSwitcherExt("LayerSwitcher", {},LayerStateChange );
  var option= {
                //maxExtent: new OpenLayers.Bounds(-91.5,22,-71.5,36.5),
                maxExtent: new OpenLayers.Bounds(-82, 31, -74, 37),          
                restrictedExtent: new OpenLayers.Bounds(-82, 31, -74, 37),
                scales: [4000000, 2000000, 1000000, 500000],
                projection:"EPSG:4326",
                tileSize: new OpenLayers.Size(512,512),
                units: "degrees",           
                controls: [new OpenLayers.Control.MouseDefaults(),
                           new OpenLayers.Control.MousePosition({numdigits:2}),                
                           new OpenLayers.Control.PanZoomBar(),
                           new OpenLayers.Control.Permalink('permalink'),                         
                           layerSwitcherExt,
                           new OpenLayers.Control.ScaleLine()
                           ]
              };

  map = new OpenLayers.Map( 'map' ,option );
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // Base map layers
                                                                            
  var TileCacheBaseMap = new OpenLayers.Layer.WMS( 'Base Map',
                                      strTileCacheURL,
                                      {
                                         LAYERS : 'basemap'
                                      },
                                      {
                                        displayInLayerSwitcher: false,
                                        GROUP : 'BaseMap'
                                      }
                                     );                                    
  // Chart 

/*
  var Chart_11009 = new OpenLayers.Layer.WMS( "NOAA Chart Offshore", 
                                               strTileCacheURL, 
                                               {
                                                 LAYERS: 'NOAACHART11009'
                                               },
                                               {
                                                attribution:'Provided by NOAA. Chart 11009',
                                                LAYERURL: 'http://carolinasrcoos.org/DataLinks.php#NOAACharts',
                                                GROUP : 'BaseMap'
                                               }
                                            );
*/
 // Chart 
  var Chart_11009 = new OpenLayers.Layer.WMS( "NOAA Chart Offshore", 
                                               strMapservURL, 
                                               {
                                                 MAP : strMapFile, 
                                                 TRANSPARENT : true, 
                                                 LAYERS : 'HatterasToFL',
                                                 FORMAT: ImageType
                                               },
                                               {
                                                attribution:'Provided by NOAA. Chart 11009',
                                                LAYERURL: 'http://carolinasrcoos.org/DataLinks.php#NOAACharts',
                                                GROUP : 'Map Overlay'
                                               }
                                            );

  Chart_11009.setVisibility(false);


  var MajorCities = new OpenLayers.Layer.WMS( "Coastal Cities", 
                                           strMapservURL,
                                           {
                                             MAP : strMapFile, 
                                             TRANSPARENT : true, 
                                             LAYERS : 'MajorCities',
                                             FORMAT: ImageType
                                           },
                                           {
                                              GROUP : 'Map Overlay'            
                                           }
                                        );
  MajorCities.setVisibility( false );                 
                                        
                                        
  var LatLongGrid = new OpenLayers.Layer.WMS( "Latitude/Longitude Grid", 
                                           strMapservURL,
                                           {
                                             MAP : strMapFile, 
                                             TRANSPARENT : true, 
                                             LAYERS : 'latlonggrid',
                                             FORMAT: ImageType
                                           },
                                           {
                                              singleTile: true,
                                              ratio: 1,
                                              GROUP : 'Map Overlay'            
                                           }
                                        );
  LatLongGrid.setVisibility( false );                 
                                        
/*  var CitiesTowns = new OpenLayers.Layer.WMS( "Cities & Towns", 
                                           strMapservURL,
                                           {
                                             MAP : strMapFile, 
                                             TRANSPARENT : true, 
                                             LAYERS : 'CitiesTowns'
                                           },
                                           {
                                              GROUP : 'Map Overlay'            
                                           }
                                        );

  CitiesTowns.setVisibility( false );                 
*/  
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                
/*
  var SCNCCoastBathy3D = new OpenLayers.Layer.WorldWind( "NASA with Bathymetry",
                                                  "http://worldwind25.arc.nasa.gov/tile/tile.aspx?", 2, 4,
                                                  {
                                                    T:"bmng.topo.bathy.200406",
                                                    GROUP :  "BaseMap"
                                                  },
                                                  {
                                                    attribution:"Provided by NASA"
                                                  } ); 
  
*/
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // In-Situ Layers
  // This layer is just the presentation layer. It is tilecached, however we cannot issue GetFeatureInfo requests through TileCache. We
  // use the next layer, HTMLObs to be a hidden layer that does the processing for GetFeatureInfo.
  var HTMLObsOverlay = new OpenLayers.Layer.WMS( "Real Time Observations", 
                                           strTileCacheURL,
                                           {
                                           TRANSPARENT : true, 
                                           LAYERS: 'WMShtmlobs' + TileCacheMod, 
                                           REPROJECT: false                                           
                                           },
                                           {
                                            attribution:'Near real time data from measurement stations.',
                                            LAYERURL: 'http://carolinasrcoos.org/DataLinks.php#RealTimeObs',
                                            GROUP : 'InSitu',
                                            legend: ''
                                            //legend : 'http://carolinasrcoos.org/images/ObsLegend.png'
                                           }
                                        );
  // This layer is the HTML tables representing all the obs for the platform we are mousing. 
  var HTMLObs = new OpenLayers.Layer.WMS( "Invisible Obs", 
                                           strMapservURL,
                                           {
                                             MAP : strMapFile, 
                                             TRANSPARENT : true, 
                                             LAYERS : 'HTMLObsTable'
                                           },
                                           {
                                              displayInLayerSwitcher: false,
                                              visibility: false,
                                              GROUP : 'InSitu',            
                                              QUERYABLE: true
                                           }
                                        );
                                           


  var InSituWindsWMS = new OpenLayers.Layer.WMS("In-Situ Winds", 
                                        "http://secoora.org/ncogc/mapserv?", 
                                        {
                                            MAP: '/opt/secoora_ogc/maps/secoora_insitu.map',
                                            SRS: 'epsg:4269',
                                            FORMAT: ImageType,
                                            TRANSPARENT: 'true',
                                            LAYERS: 'wind_obs_hourly_recent',
                                            TIME_OFFSET_HOURS: OFFSET,
                                            WIND_UNITS: 'knots',
                                            MAP_SCALEBAR_STATUS: 'OFF'
                                        },
                                        {
                                            GROUP : 'Composite'
                                        }
                                        );

  InSituWindsWMS.setVisibility( false );
  
  var Bathymetry = new OpenLayers.Layer.WMS( "Bathymetry", 
                                              strTileCacheURL, 
                                              {
                                               TRANSPARENT : true, 
                                               LAYERS : 'WMSBathymetry' + TileCacheMod,
                                               REPROJECT: false
                                              },
                                              {
                                                attribution:'Bathymetry layer.',
                                                GROUP :  'Map Overlay'
                                              }
                                            );
  //Layer is not on initially.
  Bathymetry.setVisibility( false );                 
 
  var WaterQuality = new OpenLayers.Layer.WMS( "Water Quality",
                                              strMapservURL,
                                              {
                                               MAP : strMapFile,
                                               SRS : 'epsg:4326',
                                               FORMAT : ImageType,
                                               TRANSPARENT : true,
                                               LAYERS : 'WaterQuality'
                                              },
                                             {
                                              attribution:'TBD',
                                              LAYERURL: '',
                                              GROUP :  'InSitu',
                                              legend : ''                               
                                              //QUERYABLE: true
                                             }

                                            );
  //Layer is not on initially.
  WaterQuality.setVisibility( false );

  var dhecRain = new OpenLayers.Layer.WMS( "SC DHEC Rain Gauges",
                                              strMapservURL,
                                              {
                                               MAP : strMapFile,
                                               SRS : 'epsg:4326',
                                               FORMAT : ImageType,
                                               TRANSPARENT : true,
                                               LAYERS : 'DHECRaingauges'
                                              },
                                             {
                                              attribution:'Data collected from DHEC rain gauges along the northern SC coast.',
                                              LAYERURL: '',
                                              GROUP :  'InSitu',
                                              legend : ''                               
                                             }

                                            );
  //Layer is not on initially.
  dhecRain.setVisibility( false );
 
  var Wrecks = new OpenLayers.Layer.WMS( "NOAA Wrecks and Obstructions", 
                                              strMapservURL, 
                                              {
                                               MAP : strMapFile, 
                                               SRS : 'epsg:4326', 
                                               FORMAT : ImageType, 
                                               TRANSPARENT : true, 
                                               LAYERS : 'WrecksObstructions'
                                              },
                                             {
                                              attribution:'Derived from NOAA\'s Office of Coast Survey Dataset.',
                                              LAYERURL: 'http://carolinasrcoos.org/DataLinks.php#NOAAWrecks',
                                              GROUP :  'Recreational',
                                              legend: ''
                                             // legend : 'http://carolinasrcoos.org/images/WrecksObstructions.png'                                                                                                                                        
                                              //QUERYABLE: true
                                             }
                                              
                                            );
  //Layer is not on initially.
  Wrecks.setVisibility( false );                 

  var SCDNRReefs = new OpenLayers.Layer.WMS( "SC Artificial Reefs", 
                                            strMapservURL, 
                                            {
                                             MAP : strMapFile, 
                                             SRS : 'epsg:4326', 
                                             FORMAT : ImageType, 
                                             TRANSPARENT : true, 
                                             LAYERS : 'SCDNRReefs'
                                            },
                                           {
                                            attribution:'Derived from South Carolina Department of Natural Resources data.',
                                            LAYERURL: 'http://carolinasrcoos.org/DataLinks.php#SCReefs',
                                            GROUP :  'Recreational',
                                            legend: ''
                                            //legend : 'http://carolinasrcoos.org/images/tree/blue_fish.png'
                                            //QUERYABLE: true
                                           }
                                            
                                          );
  //Layer is not on initially.
  SCDNRReefs.setVisibility( false );                 
  
  var NCReefs = new OpenLayers.Layer.WMS( "NC Artificial Reefs", 
                                            strMapservURL, 
                                            {
                                             MAP : strMapFile, 
                                             SRS : 'epsg:4326', 
                                             FORMAT : ImageType, 
                                             TRANSPARENT : true, 
                                             LAYERS : 'NCReefs'
                                            },
                                           {
                                            attribution:'Derived from North Carolina Division of Marine Fisheries.',
                                            LAYERURL: 'http://carolinasrcoos.org/DataLinks.php#NCReefs',
                                            GROUP :  'Recreational',
                                            legend: ''
                                            //legend : 'http://carolinasrcoos.org/images/tree/blue_fish.png'
                                            //QUERYABLE: true
                                           }
                                            
                                          );
  //Layer is not on initially.
  NCReefs.setVisibility( false );                 

  var SCRamps = new OpenLayers.Layer.WMS( "SC Boat Ramps", 
                                            strMapservURL, 
                                            {
                                             MAP : strMapFile, 
                                             SRS : 'epsg:4326', 
                                             FORMAT : ImageType, 
                                             TRANSPARENT : true, 
                                             LAYERS : 'SCBoatRamps'
                                            },
                                           {
                                            attribution:'Derived from South Carolina Department of Natural Resources.',
                                            LAYERURL: 'http://carolinasrcoos.org/DataLinks.php#SCReefs',
                                            GROUP :  'Recreational',
                                            legend: ''
                                            //legend : 'http://carolinasrcoos.org/images/tree/blue_fish.png'
                                            //QUERYABLE: true
                                           }
                                            
                                          );
  //Layer is not on initially.
  SCRamps.setVisibility( false );                 

  var NCRamps = new OpenLayers.Layer.WMS( "NC Boat Ramps", 
                                            strMapservURL, 
                                            {
                                             MAP : strMapFile, 
                                             SRS : 'epsg:4326', 
                                             FORMAT : ImageType, 
                                             TRANSPARENT : true, 
                                             LAYERS : 'NCBoatRamps'
                                            },
                                           {
                                            attribution:'Derived from North Carolina Department of Natural Resources.',
                                            LAYERURL: 'http://carolinasrcoos.org/DataLinks.php#NCReefs',
                                            GROUP :  'Recreational'
                                            //legend : 'http://carolinasrcoos.org/images/tree/blue_fish.png'
                                            //QUERYABLE: true
                                           }
                                            
                                          );
  //Layer is not on initially.
  NCRamps.setVisibility( false );                 
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

   
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // Radar Layers
  var OI_SST = new OpenLayers.Layer.WMS("Interpolated Surface Temperature", 
                                             strMapservURL, //'http://nautilus.baruch.sc.edu/wms/seacoos_rs?', 
                                            {
                                              MAP: strMapFile,
                                              LAYERS: 'oi_sst',
                                              SRS: 'epsg:4269',
                                              FORMAT: ImageType,
                                              TRANSPARENT: true
                                             },
                                             {
                                               attribution: 'Provided by Ocean Circulation Group at the Institute for Marine Remote Sensing (IMaRS)',
                                               LAYERURL: 'http://carolinasrcoos.org/DataLinks.php#OISST',
                                               GROUP : "Composite",
                                               legend: ''
                                              // legend : 'http://carolinasrcoos.org/images/InterpolatedSST.png'                                                                                                                                        
                                             }
                                            );
  /*var avhrr_wms = new OpenLayers.Layer.WMS("AVHRR SST", "http://129.252.37.86/cgi-bin/mapserv?", 
                                            {
                                                MAP: '/home/rcoos/mapserver/RCOOS.map',
                                                SRS: 'epsg:4269',
                                                FORMAT: ImageType,
                                                TRANSPARENT: 'true',
                                                TIME_OFFSET_HOURS: OFFSET,
                                                LAYERS: 'oi_sst',
                                                MAP_SCALEBAR_STATUS: 'OFF',
                                                GROUP : "RadarImg"
                                            },
  {
      singleTile: true,
      ratio: 2
  });
  */
  OI_SST.setVisibility( false );                 

/*  var GOESEastLatest = new OpenLayers.Layer.WMS("Latest GOESEast Radar", 
                                             strMapservURL, 
                                            {
                                              MAP: strMapFile,
                                              LAYERS: 'goes_east',
                                              SRS: 'epsg:4269',
                                              FORMAT: ImageType,
                                              TRANSPARENT: true
                                             },
                                             {
                                               attribution: 'Near Real Time Spatial Data Products for Geographical Information Systems',
                                               LAYERURL: 'ftp://satepsanone.nesdis.noaa.gov/GIS/GOESeast',
                                               GROUP : "Satellite Imagery"
                                             }
                                            );

  GOESEastLatest.setVisibility( false );                 
  */
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  var myStyles = new OpenLayers.StyleMap({
                                            "default": new OpenLayers.Style({
                                            pointRadius: 4, 
                                            fillColor: "#000000",
                                            strokeColor: "#000000",
                                            fillOpacity: 0.0
                                            //strokeWidth: 2
                                            })
                                        });

  var PlatformGML = new OpenLayers.Layer.GML("RCOOS Platforms", 
                                              "./data/platformGML.xml", 
                                              {
                                                projection: new OpenLayers.Projection("EPSG:4326"),
                                                styleMap: myStyles,
                                                displayInLayerSwitcher: false,
                                                GROUP : 'InSitu'                                              
                                              });   
                                            
                                              
  var platformMouseOverCallbacks = 
  {
    "over": function(feature) 
    {
      //Only change the cursor if the layer is visible.
      if( HTMLObsOverlay.getVisibility() )
      {
        document.getElementById("map").style.cursor="pointer";
      }
        //log("over " + feature.id);
    },
    "out": function(feature) 
    {
      document.getElementById("map").style.cursor="default";
      //log("out " + feature.id);
    },
    "click": function(feature)
    {
      //Only do the GetFeatureInfo query if one of the layers are visible.
      if( HTMLObsOverlay.getVisibility()  || 
          WaterQuality.getVisibility()    || 
          Wrecks.getVisibility()          || 
          SCDNRReefs.getVisibility()      ||
          NCReefs.getVisibility()         ||
          SCRamps.getVisibility()         ||
          NCRamps.getVisibility()         ||
          dhecRain.getVisibility()
        )
      {
        var strQueryLayers = "";
        var iFeatureCount = 1;
        // Add obs to the layers we want if it is visible.
        if( HTMLObsOverlay.getVisibility() )
        {
          strQueryLayers = 'HTMLObsTable';  
          iFeatureCount++;
        }
        if( WaterQuality.getVisibility() )
        {
          if( strQueryLayers.length )
          {
            strQueryLayers += ',';
          }
          strQueryLayers += 'WaterQuality';
          iFeatureCount++;
        }
        if( Wrecks.getVisibility() )
        {
          if( strQueryLayers.length )
          {
            strQueryLayers += ',';
          }
          strQueryLayers += 'WrecksObstructions';
          iFeatureCount++;
        }
        if( SCDNRReefs.getVisibility() )
        {
          if( strQueryLayers.length )
          {
            strQueryLayers += ',';
          }
          strQueryLayers += 'SCDNRReefs';
          iFeatureCount++;
        }      
        if( NCReefs.getVisibility() )
        {
          if( strQueryLayers.length )
          {
            strQueryLayers += ',';
          }
          strQueryLayers += 'NCReefs';
          iFeatureCount++;
        }
        if( SCRamps.getVisibility() )
        {
          if( strQueryLayers.length )
          {
            strQueryLayers += ',';
          }
          strQueryLayers += 'SCBoatRamps';
          iFeatureCount++;
        }
        if( NCRamps.getVisibility() )
        {
          if( strQueryLayers.length )
          {
            strQueryLayers += ',';
          }
          strQueryLayers += 'NCBoatRamps';
          iFeatureCount++;
        }
        if( dhecRain.getVisibility() )
        {
          if( strQueryLayers.length )
          {
            strQueryLayers += ',';
          }
          strQueryLayers += 'DHECRaingauges';
          iFeatureCount++;
        }
        
        
        var LonLat = new OpenLayers.LonLat( feature.geometry.x, feature.geometry.y );
        var Px = feature.layer.getViewPortPxFromLonLat( LonLat );
        OpenLayers.Util.getElement('rolloverbox').innerHTML = '<b>Retrieving Information</b>';
        var DataURL = HTMLObs.getFullRequestString(
        {
            REQUEST : "GetFeatureInfo", 
            EXCEPTIONS : "application/vnd.ogc.se_xml", 
            BBOX : map.getExtent().toBBOX(), 
            X : Px.x, 
            Y : Px.y, 
            INFO_FORMAT : 'text/html', 
            QUERY_LAYERS : strQueryLayers, 
            WIDTH : map.size.w, 
            HEIGHT : map.size.h,
            FEATURE_COUNT: iFeatureCount
        });
        OpenLayers.loadURL(DataURL, '', this, setData);
      }
    }

  };
  
  mouseOverPlatformHandler = new OpenLayers.Handler.Feature(
                                              {
                                                map: map
                                              }, 
                                              PlatformGML, 
                                              platformMouseOverCallbacks
  );
  mouseOverPlatformHandler.activate();
  
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  map.addLayers([TileCacheBaseMap,Chart_11009,Bathymetry,MajorCities,LatLongGrid,HTMLObsOverlay,HTMLObs,WaterQuality,dhecRain,OI_SST,Wrecks,SCDNRReefs,NCReefs,InSituWindsWMS,PlatformGML,SCRamps,NCRamps]);

  map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
  
  var mapResol = map.getMaxResolution();
  
  map.events.register('click', map, function (e)
  {
    //Only do the GetFeatureInfo query if one of the layers are visible.
    if( HTMLObsOverlay.getVisibility()  || 
        WaterQuality.getVisibility()    || 
        Wrecks.getVisibility()          || 
        SCDNRReefs.getVisibility()      ||
        NCReefs.getVisibility()         ||
        SCRamps.getVisibility()         ||
        NCRamps.getVisibility()         ||
        dhecRain.getVisibility()
      )
    {
      var strQueryLayers = "";
      var iFeatureCount = 1;
      // Add obs to the layers we want if it is visible.
      if( HTMLObsOverlay.getVisibility() )
      {
        strQueryLayers = 'HTMLObsTable';  
        iFeatureCount++;
      }
      if( WaterQuality.getVisibility() )
      {
        if( strQueryLayers.length )
        {
          strQueryLayers += ',';
        }
        strQueryLayers += 'WaterQuality';
        iFeatureCount++;
      }
      if( Wrecks.getVisibility() )
      {
        if( strQueryLayers.length )
        {
          strQueryLayers += ',';
        }
        strQueryLayers += 'WrecksObstructions';
        iFeatureCount++;
      }
      if( SCDNRReefs.getVisibility() )
      {
        if( strQueryLayers.length )
        {
          strQueryLayers += ',';
        }
        strQueryLayers += 'SCDNRReefs';
        iFeatureCount++;
      }      
      if( NCReefs.getVisibility() )
      {
        if( strQueryLayers.length )
        {
          strQueryLayers += ',';
        }
        strQueryLayers += 'NCReefs';
        iFeatureCount++;
      }
      if( SCRamps.getVisibility() )
      {
        if( strQueryLayers.length )
        {
          strQueryLayers += ',';
        }
        strQueryLayers += 'SCBoatRamps';
        iFeatureCount++;
      }
      if( NCRamps.getVisibility() )
      {
        if( strQueryLayers.length )
        {
          strQueryLayers += ',';
        }
        strQueryLayers += 'NCBoatRamps';
        iFeatureCount++;
      }
      if( dhecRain.getVisibility() )
      {
        if( strQueryLayers.length )
        {
          strQueryLayers += ',';
        }
        strQueryLayers += 'DHECRaingauges';
        iFeatureCount++;
      }

      OpenLayers.Util.getElement('rolloverbox').innerHTML = '<b>Retrieving Information</b>';
      var DataURL = HTMLObs.getFullRequestString(
      {
          REQUEST : "GetFeatureInfo", 
          EXCEPTIONS : "application/vnd.ogc.se_xml", 
          BBOX : map.getExtent().toBBOX(), 
          X : e.xy.x, 
          Y : e.xy.y, 
          INFO_FORMAT : 'text/html', 
          QUERY_LAYERS : strQueryLayers, 
          WIDTH : map.size.w, 
          HEIGHT : map.size.h,
          FEATURE_COUNT: iFeatureCount
      });
      OpenLayers.loadURL(DataURL, '', this, setData);
    }
   OpenLayers.Event.stop(e)
  });
  
  /////////////////////////////////////////////////////////////////////////////////////////////////////
  // Function: LayerStateChange  
  // 
  // Purpose: 
  //  Callback function that the layerswitcher uses to tell us a layer has either been toggled on or off
  // Parameters:
  /////////////////////////////////////////////////////////////////////////////////////////////////////
  function LayerStateChange(layer,state)
  {
    // If the layer is the real time obs, we need to check the visibility state and either enable/disable the
    // GML layer which is our helper layer to do the mouseovers.
    if( layer.name == 'Real Time Observations' )
    {
      PlatformGML.setVisibility( state );
    }
    if( layer.name == 'Interpolated Surface Temperature' )
    {
        if(state){
              OpenLayers.Util.getElement('legend').innerHTML = '<img src=http://carolinasrcoos.org/images/InterpolatedSST.png border=0>';
        }
        else {
              OpenLayers.Util.getElement('legend').innerHTML = '';
        }
    }
  }
} 

function setData(response)
{
  if (response.responseText.length < '5')
  {
    OpenLayers.Util.getElement('rolloverbox').innerHTML = '<b>No information for this location</b>';
  }
  else 
  {
    OpenLayers.Util.getElement('rolloverbox').innerHTML = response.responseText;
  }
}


/////////////////////////////////////////////////////////////////////////////////////////////////////
//Archives
function archiveInit()
{
  var url;
  var icon;
  var lat=33.8;
  var lon=-77.9;       
  var zoom = 4;
  var map;
  var OFFSET = '2';

  OpenLayers.ProxyHost="http://carolinasrcoos.org/cgi-bin/proxy.php?url=";
  //OpenLayers.ProxyHost="http://rcoos.org/carolinasrcoosrev2/cgi-bin/proxy.php?url=";

  
  var strTileCacheURL = 'http://129.252.37.86/tilecache/tilecache.cgi?';
  var strMapservURL   = 'http://129.252.37.86/cgi-bin/mapserv?';
  var strMapFile      = '/home/rcoos/mapserver/RCOOSArchive.map';
  
  var option= {
                maxExtent: new OpenLayers.Bounds(-82, 31, -74, 37),          
                restrictedExtent: new OpenLayers.Bounds(-82, 31, -74, 37),
                scales: [4000000, 2000000, 1000000, 500000],
                projection:"EPSG:4326",
                tileSize: new OpenLayers.Size(512,512),
                units: "degrees",           
                controls: [new OpenLayers.Control.MouseDefaults(),
                           new OpenLayers.Control.MousePosition({numdigits:2}),                
                           new OpenLayers.Control.PanZoomBar(),
                           new OpenLayers.Control.LayerSwitcherExt("LayerSwitcher"),
                           new OpenLayers.Control.ScaleLine()
                           ]
              };

  map = new OpenLayers.Map( 'map' ,option );
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // Base map layers
  var BaseMap = new OpenLayers.Layer.WMS( "Base Map",
                                            strTileCacheURL,
                                            {
                                               LAYERS : "basemap"
                                            },
                                            {
                                               displayInLayerSwitcher: false,
                                               GROUP : "BaseMap"
                                            }
                                          );                                    
  
  var LatLongGrid = new OpenLayers.Layer.WMS( "Latitude/Longitude Grid", 
                                           strMapservURL,
                                           {
                                             MAP : strMapFile, 
                                             TRANSPARENT : true, 
                                             LAYERS : 'latlonggrid',
                                             FORMAT: ImageType

                                           },
                                           {
                                              singleTile: true,
                                              ratio: 1,
                                              GROUP : 'Map Overlay'            
                                           }
                                        );
  LatLongGrid.setVisibility( false );                 
  
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
   
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //Archives layers 
  // This is our TileCached layer, the one we see on the map.
  var COORMPArchivesOverlay = new OpenLayers.Layer.WMS( "Archives", 
                                                          strTileCacheURL, 
                                                          {
                                                            TRANSPARENT : true, 
                                                            LAYERS : 'WMSArchives' + TileCacheMod, 
                                                            REPROJECT: false                                                                                                      
                                                          },
                                                          {
                                                            GROUP : "In-Situ"                                                          
                                                          }
                                                        );
  // This is the inivible layer used to handle the GetFeatureInfo requests.            
  var COORMPArchives = new OpenLayers.Layer.WMS( "Invisible Archives", 
                                                  strMapservURL, 
                                                   {
                                                     MAP : strMapFile, 
                                                     SRS : 'epsg:4326', 
                                                     FORMAT: ImageType, 
                                                     TRANSPARENT : 'true', 
                                                     LAYERS : 'CORMPArchives'
                                                   },
                                                   {
                                                    displayInLayerSwitcher: false,
                                                    visibility: false
                                                   }                                                 
                                                );
  var CTDArchivesQ1 = new OpenLayers.Layer.WMS( "1st Quarter 1973-2006", 
                                              strMapservURL, 
                                              {
                                               MAP : strMapFile, 
                                               SRS : 'epsg:4326', 
                                               FORMAT: ImageType, 
                                               TRANSPARENT : true, 
                                               LAYERS : 'ctd_q1'
                                              },
                                              {
                                                GROUP :  "MARMAP Conductivity & Temperature",
                                                ATTRIBUTION:'Derived from South Carolina Department of Natural Resources data.',
                                                LAYERURL: 'http://carolinasrcoos.org/ArchiveDataLinks.php#MARMAP'                                             
                                              }
                                            );
  
  CTDArchivesQ1.setVisibility( false );                 
  var CTDArchivesQ2 = new OpenLayers.Layer.WMS( "2nd Quarter 1973-2006", 
                                              strMapservURL, 
                                              {
                                               MAP : strMapFile, 
                                               SRS : 'epsg:4326', 
                                               FORMAT: ImageType, 
                                               TRANSPARENT : true, 
                                               LAYERS : 'ctd_q2'
                                              },
                                              {
                                                GROUP :  "MARMAP Conductivity & Temperature",
                                                ATTRIBUTION:'Derived from South Carolina Department of Natural Resources data.',
                                                LAYERURL: 'http://carolinasrcoos.org/ArchiveDataLinks.php#MARMAP'                                              
                                              }
                                            );
  
  CTDArchivesQ2.setVisibility( false );                 
  var CTDArchivesQ3 = new OpenLayers.Layer.WMS( "3rd Quarter 1973-2006", 
                                              strMapservURL, 
                                              {
                                               MAP : strMapFile, 
                                               SRS : 'epsg:4326', 
                                               FORMAT: ImageType, 
                                               TRANSPARENT : true, 
                                               LAYERS : 'ctd_q3'
                                              },
                                              {
                                                GROUP :  "MARMAP Conductivity & Temperature",
                                                ATTRIBUTION:'Derived from South Carolina Department of Natural Resources data.',
                                                LAYERURL: 'http://carolinasrcoos.org/ArchiveDataLinks.php#MARMAP'                                              
                                              }
                                            );
  
  CTDArchivesQ3.setVisibility( false );                 
  var CTDArchivesQ4 = new OpenLayers.Layer.WMS( "4th Quarter 1973-2006", 
                                              strMapservURL, 
                                              {
                                               MAP : strMapFile, 
                                               SRS : 'epsg:4326', 
                                               FORMAT: ImageType, 
                                               TRANSPARENT : true, 
                                               LAYERS : 'ctd_q4'
                                              },
                                              {
                                                GROUP :  "MARMAP Conductivity & Temperature",
                                                ATTRIBUTION:'Derived from South Carolina Department of Natural Resources data.',
                                                LAYERURL: 'http://carolinasrcoos.org/ArchiveDataLinks.php#MARMAP'                                             
                                              }
                                            );
  
  CTDArchivesQ4.setVisibility( false );                 
  
  
  var Bathymetry = new OpenLayers.Layer.WMS( "Bathymetry", 
                                              strTileCacheURL, 
                                              {
                                               TRANSPARENT : true, 
                                               LAYERS : 'WMSBathymetry' + TileCacheMod,
                                               REPROJECT: false
                                              },
                                              {
                                                attribution:'Bathymetry layer.',
                                                GROUP :  'Map Overlay'
                                              }
                                            );
  //Layer is not on initially.
  Bathymetry.setVisibility( false );                 
  
  //Layer is not on initially.
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
  map.addLayers([BaseMap,Bathymetry,LatLongGrid,COORMPArchivesOverlay,COORMPArchives,CTDArchivesQ1,CTDArchivesQ2,CTDArchivesQ3,CTDArchivesQ4]);

  map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
  
  map.events.register('click', map, function (e)
  {
    if( COORMPArchivesOverlay.getVisibility() || 
        CTDArchivesQ1.getVisibility()          || 
        CTDArchivesQ2.getVisibility()          || 
        CTDArchivesQ3.getVisibility()          || 
        CTDArchivesQ4.getVisibility()          
      )
    {
      var strQueryLayers = "";
      var iFeatureCount = 1;
      // Add obs to the layers we want if it is visible.
      if( COORMPArchivesOverlay.getVisibility() )
      {
        strQueryLayers = 'CORMPArchives';  
        iFeatureCount++;
      }
      if( CTDArchivesQ1.getVisibility() )
      {
        strQueryLayers = 'ctd_q1';  
        iFeatureCount++;
      }
      if( CTDArchivesQ2.getVisibility() )
      {
        strQueryLayers = 'ctd_q2';  
        iFeatureCount++;
      }
      if( CTDArchivesQ3.getVisibility() )
      {
        strQueryLayers = 'ctd_q3';  
        iFeatureCount++;
      }
      if( CTDArchivesQ4.getVisibility() )
      {
        strQueryLayers = 'ctd_q4';  
        iFeatureCount++;
      }
      //Click event for the archivals.
      OpenLayers.Util.getElement('rolloverbox').innerHTML = "<b>Retrieving Archival Information</b>";
      var COORMPArchivesURL = COORMPArchives.getFullRequestString(
      {
          REQUEST : "GetFeatureInfo", 
          EXCEPTIONS : "application/vnd.ogc.se_xml", 
          BBOX : map.getExtent().toBBOX(), 
          X : e.xy.x, 
          Y : e.xy.y, 
          INFO_FORMAT : 'text/html', 
          QUERY_LAYERS : strQueryLayers, 
          WIDTH : map.size.w, 
          HEIGHT : map.size.h,
          FEATURE_COUNT: iFeatureCount
      });
      OpenLayers.loadURL(COORMPArchivesURL, '', this, setArchivesInfo);
    }
    OpenLayers.Event.stop(e)
  });
}

function setArchivesInfo(response)
{
  if (response.responseText.length < '5')
  {
    OpenLayers.Util.getElement('rolloverbox').innerHTML = "<b>No Archival Data for this location</b>"
  }
  else 
  {
    OpenLayers.Util.getElement('rolloverbox').innerHTML = response.responseText;
  }
}

/*function setMouseover(evt,url){
                    OpenLayers.Event.stop(evt);
                    OpenLayers.Util.getElement('rolloverbox').innerHTML= "Station: " + url + "<br>" + data_array[url];
}

function setMouseout(evt){    
                     OpenLayers.Event.stop(evt);
                     OpenLayers.Util.getElement('rolloverbox').innerHTML="";
                     
                       
                        
}
*/
