//<![CDATA[

    // Check to see if this browser can run the Google API
    if (GBrowserIsCompatible()) {
	
	var select_html = '<select onChange="handleSelected(this)">' +
                        '<option selected> Select a Marina . . . </option>';         
      
      var gmarkers = [];
      var htmls = [];
          
      var to_htmls = [];
      var from_htmls = [];
	  
	  
var myIcon = new GIcon();
myIcon.image = 'images/maps/marinas/image.png';
myIcon.shadow = 'images/maps/marinas/shadow.png';
myIcon.iconSize = new GSize(42,52);
myIcon.shadowSize = new GSize(72,52);
myIcon.iconAnchor = new GPoint(0,52);
myIcon.infoWindowAnchor = new GPoint(26,0);
myIcon.imageMap = [37,0,39,1,39,2,40,3,41,4,41,5,41,6,41,7,41,8,41,9,41,10,41,11,41,12,41,13,41,14,41,15,41,16,41,17,41,18,41,19,41,20,41,21,41,22,41,23,41,24,41,25,41,26,41,27,41,28,41,29,41,30,41,31,41,32,41,33,41,34,41,35,41,36,40,37,40,38,39,39,38,40,37,41,34,42,30,43,26,44,23,45,19,46,16,47,13,48,9,49,6,50,2,51,0,51,0,50,1,49,2,48,2,47,3,46,4,45,4,44,5,43,5,42,4,41,3,40,2,39,1,38,0,37,0,36,0,35,0,34,0,33,0,32,0,31,0,30,0,29,0,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,1,3,2,2,3,1,4,0,37,0];


	  
	  

    
  
      // ==================================================
      // A function to create a tabbed marker and set up the event window
      function createTabbedMarker(point,html,html2,label1,label2,name) {
        var marker = new GMarker(point, myIcon);

		
		var i = gmarkers.length;  
		
		// The info window version with the "to here" form open
        to_htmls[i] = html + '<br>Directions: <b>To here<\/b> - <a href="javascript:fromhere(' + i + ')">From here<\/a>' +
           '<br>Start address:<form action="javascript:getDirections()">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT"><br>' +
           'Walk <input type="checkbox" name="walk" id="walk" /> &nbsp; Avoid Highways <input type="checkbox" name="highways" id="highways" />' +
           '<input type="hidden" id="daddr" value="'+name+"@"+ point.lat() + ',' + point.lng() + 
           '"/>';
        // The info window version with the "from here" form open
        from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <b>From here<\/b>' +
           '<br>End address:<form action="javascript:getDirections()">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT"><br>' +
           'Walk <input type="checkbox" name="walk" id="walk" /> &nbsp; Avoid Highways <input type="checkbox" name="highways" id="highways" />' +
           '<input type="hidden" id="saddr" value="'+name+"@"+ point.lat() + ',' + point.lng() +
           '"/>';
        // The inactive version of the direction info
        html = html + '<br>Directions: <a href="javascript:tohere('+i+')">To here<\/a> - <a href="javascript:fromhere('+i+')">From here<\/a>';
		
		
		
		
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowTabsHtml([new GInfoWindowTab(label1,html), new GInfoWindowTab(label2,html2)]);
        });
		
		
		
		// save the info we need to use later for the side_bar
        gmarkers.push(marker);
		
		
		
		
        htmls[i] = html;
		
		select_html += '<option> ' + name + '<\/option>';
		
        i++;
		return marker;
      }
	  
	  
	  
	  
       // ===== request the directions =====
      function getDirections() {
        // ==== Set up the walk and avoid highways options ====
        var opts = {};
        if (document.getElementById("walk").checked) {
           opts.travelMode = G_TRAVEL_MODE_WALKING;
        }
        if (document.getElementById("highways").checked) {
           opts.avoidHighways = true;
        }
        // ==== set the start and end locations ====
        var saddr = document.getElementById("saddr").value
        var daddr = document.getElementById("daddr").value
        gdir.load("from: "+saddr+" to: "+daddr, opts);
      }
 
        
        
      // This function picks up the click and opens the corresponding info window
      function myclick(i) {
        gmarkers[i].openInfoWindowHtml(htmls[i]);
      }

      // functions that open the directions forms
      function tohere(i) {
        gmarkers[i].openInfoWindowHtml(to_htmls[i]);
      }
      function fromhere(i) {
        gmarkers[i].openInfoWindowHtml(from_htmls[i]);
      }
      
      
      
	  
      function handleSelected(opt) {
        var i = opt.selectedIndex - 1; 
        if (i > -1) {
          GEvent.trigger(gmarkers[i],"click");
        }
        else {
          map.closeInfoWindow();
        }
      }
      

      // Display the map, with some controls and set the initial location 
      var map = new GMap2(document.getElementById("map"));
	  map.addMapType(G_PHYSICAL_MAP);
      map.setUIToDefault();
      map.setCenter(new GLatLng(38.2400,-121.5811), 12, G_PHYSICAL_MAP);
    
      // ==================================================
	  
	  
	  
	  
	  
	  // === create a GDirections Object ===
      var gdir=new GDirections(map, document.getElementById("directions"));

      // === Array for decoding the failure codes ===
      var reasons=[];
      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.";
      reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
      reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
      reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";

      // === catch Directions errors ===
      GEvent.addListener(gdir, "error", function() {
        var code = gdir.getStatus().code;
        var reason="Code "+code;
        if (reasons[code]) {
          reason = reasons[code]
        } 

        alert("Failed to obtain directions, "+reason);
      });
	  
	  
	  
	  	  
	  
      // Set up the tabbed markers
      var request = GXmlHttp.create();
      request.open("GET", "xml/marinas/north.xml", true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
        var xmlDoc = GXml.parse(request.responseText);
          
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");
          
          for (var i = 0; i < markers.length; i++) {
            
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var point = new GLatLng(lat,lng);
			
            var html = markers[i].getAttribute("html");
			var html2 = markers[i].getAttribute("html2");
			var label1 = markers[i].getAttribute("label1");
			var label2 = markers[i].getAttribute("label2");
			var label = markers[i].getAttribute("label");
			
				
            
            var marker = createTabbedMarker(point,html,html2,label1,label2,label);
            map.addOverlay(marker);
          }

    
    select_html += '<\/select>';
        document.getElementById("selection").innerHTML = select_html;
      }
    }
	request.send(null);
	}

    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
    
