/*--------------------------------------------------------------------------*/
/*  WNCOutdoors.info Google Maps Controls Script
 *  (c) 2007 WNCOutdoors.info/Jordan Mitchell
 *  All rights reserved.
/*--------------------------------------------------------------------------*/

if (GBrowserIsCompatible())
{
  /* The following code is attributed to a post on the Google Maps API discussion board:
  http://groups.google.com/group/Google-Maps-API/browse_thread/thread/d0277d8373110678/08940a81c71e6ab3?lnk=gst&q=terraserver&rnum=2&fwc=1&utoken=hbcefDIAAACT3lD9cBuQbpQwr14qTtBa3FgEOUIGAE7lYiPpwN3xwJwXIEleGULPzGTlZzIOImixpZBR7_eT-MPotXWli1UJ */
  
  // Create a map layer based on Terraserver WMS
  function WMSCreateMap(name, copyright, baseUrl, layer, minResolution, maxResolution, urlArg )
  {
    var tileLayer = new GTileLayer( new GCopyrightCollection( copyright ), minResolution, maxResolution );
    tileLayer.baseUrl = baseUrl;
    tileLayer.layer = layer;
    tileLayer.getTileUrl = WMSGetTileUrl;
    tileLayer.getCopyright = function () { return copyright; };
    var tileLayers = [ tileLayer ];
    return new GMapType( tileLayers, G_SATELLITE_MAP.getProjection(), name, { errorMessage:"Data Not Available" } );
  }
  
  function WMSGetTileUrl( tile, zoom )
  {
    var southWestPixel = new GPoint( tile.x * 256, ( tile.y + 1 ) * 256);
    var northEastPixel = new GPoint( ( tile.x + 1 ) * 256, tile.y * 256);
    var southWestCoords = G_NORMAL_MAP.getProjection().fromPixelToLatLng( southWestPixel, zoom );
    var northEastCoords = G_NORMAL_MAP.getProjection().fromPixelToLatLng( northEastPixel, zoom );
    var bbox = southWestCoords.lng() + ',' + southWestCoords.lat() +
  	  ',' + northEastCoords.lng() + ',' + northEastCoords.lat();
    return this.baseUrl + '?VERSION=1.1.1&REQUEST=GetMap&LAYERS=' +
  	  this.layer + '&STYLES=&SRS=EPSG:4326&BBOX=' + bbox +
  		'&WIDTH=256&HEIGHT=256&FORMAT=image/jpeg&BGCOLOR=0xCCCCCC&EXCEPTIONS=INIMAGE';
  }
  		
  var WMS_TOPO_MAP = WMSCreateMap( 'Topo', 'Imagery by USGS / Web Service by TerraServer',
    'http://www.terraserver-usa.com/ogcmap6.ashx', 'DRG', 4, 17, 't' );
    
  function importanceOrder (marker,b) 
  {
    return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
  }

  // A function to create the marker and set up the event window
  // Dont try to unroll this function. It has to be here for the function closure
  // Each instance of the function preserves the contents of a different instance
  // of the "marker" and "html" variables which will be needed later when the event triggers.
  
  // Regular marker with info window
  function createRegularMarker(point,name,icontype,html) 
  {
      var marker = new GMarker(point, icontype);
      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
  }
  
  // Important marker with info window - raises the z-index of the marker
  function createImportantMarker(point,name,icontype,html,importanceValue) 
  {
      var marker = new GMarker(point, {icon:icontype,zIndexProcess:importanceOrder});
      marker.importance = importanceValue;
      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
  }
  
  // Regular marker with tabbed info window
  function createMarkerWithTabbedWindow(point,name,icontype,html) 
  {
      var marker = new GMarker(point, icontype);
      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowTabsHtml(html);
      });
      return marker;
  }
  
  // Label marker that zooms when clicked
  function createInitialZoomMarker(point,zoom,name,icontype,html) 
  {
      // === create a marker with the requested icon ===
      var marker = new GMarker(point, icontype);
      GEvent.addListener(marker, "click", function() {
        map.setCenter(point, zoom);
        marker.openInfoWindowHtml(html);
      });
      // save the info we need to use later for the side_bar
      //gmarkers[i] = marker;
      //htmls[i] = html;
      // add a line to the side_bar html
      //side_bar_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br>';
      //i++;
      return marker;
  }

  // Secondary label marker that zooms when clicked
  function createSecondaryZoomMarker(point,zoom,name,icontype,html) 
  {
      // === create a marker with the requested icon ===
      var marker = new GMarker(point, icontype);
      GEvent.addListener(marker, "click", function() {
        map.setCenter(point, zoom);
        marker.openInfoWindowHtml(html);
      });
      // save the info we need to use later for the side_bar
      //gmarkers[i] = marker;
      //htmls[i] = html;
      // add a line to the side_bar html
      //side_bar_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br>';
      //i++;
      return marker;
  }
  
  // Display map, add controls, set location
  var map = new GMap2(document.getElementById("map"));
  map.addMapType(WMS_TOPO_MAP);
  map.addMapType(G_PHYSICAL_MAP);
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());

} // End if GBrowserIsCompatible

