if (GBrowserIsCompatible()) {
      var side_bar_html = "";
      var gmarkers = [];
      var htmls = [];
      // arrays to hold variants of the info window html with get direction forms open
      var to_htmls = [];
      var from_htmls = [];
      var adds=[];
      var geo;
			var opts = {};
			var names = [];

      // A function to create the marker and set up the event window
      function createMarker(point,name,html,add,typ) {
				var mainIcon = new GIcon();
				mainIcon.image = 'map/atm.png';
				mainIcon.iconSize = new GSize(32, 32);
				mainIcon.iconAnchor = new GPoint(16, 16);
				mainIcon.infoWindowAnchor = new GPoint(25, 7);

				opt = {
	"icon" : mainIcon
	}

				if (typ==1)
				{
        	var marker = new GMarker(point);
        }
        else
        {
        	var marker = new GMarker(point,opt);
        }

        var i = gmarkers.length;
				names[i]=name;
        // The info window version with the "to here" form open
        to_htmls[i] ='<b style="color:#ff0000;">'+name+'</b>'+'<br><br>Directions: <b>To here<\/b>' +
           '<br><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" class="btn"><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() + 
           '"/><input type="hidden" id="did" value="'+i+'"/>';
        
        adds[i]=add+'<br><br><a href="javascript:showinfo(' + i +')">&lt;&lt;Back</a>';
        // The inactive version of the direction info
        if (typ==1)
        {
        	if (add!='n/a')
        	{
        		html ='<b style="color:#ff0000;">'+name+'</b>'+'<br><br>'+ html + '<a href="javascript:showadd('+i+')"><br>Additional Information</a><br><br>Directions: <a href="javascript:tohere('+i+')">To here<\/a>';
        	}
        	else
        		{
        			html ='<b style="color:#ff0000;">'+name+'</b>'+'<br><br>'+ html + '<br>Directions: <a href="javascript:tohere('+i+')">To here<\/a>';
        		}
        }
        else
        {
        	html ='<b style="color:green;">'+name+'</b>'+'<br><br>'+ html+'<br>Directions: <a href="javascript:tohere('+i+')">To here<\/a>';
        }

        GEvent.addListener(marker, "mouseover", function() {
          marker.openInfoWindowHtml(outwin(html));
        });
        // save the info we need to use later for the side_bar
        gmarkers.push(marker);
        htmls[i] = html;
        // add a line to the side_bar html
        //side_bar_html += '<a href="javascript:myclick(' + i + ')">' + name + '<\/a><br>';
        return marker;
      }

      // ===== request the directions =====
      function getDirections() {
        // ==== Set up the walk and avoid highways options ====
        
        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;
        var did= document.getElementById("did").value;
        //alert(did);
        var temps="from: "+saddr+" to: "+daddr;
        //alert(temps);
        
        
        
        ////
        geo.getLocations(saddr, function (result)
          {
          	var didmsg;
            //map.clearOverlays(); 
            if (result.Status.code == G_GEO_SUCCESS) {
              // ===== If there was more than one result, "ask did you mean" on them all =====
              if (result.Placemark.length > 1) { 
                didmsg = "<b>Did you mean:</b>";
                // Loop through the results
                for (var i=0; i<result.Placemark.length; i++) {
                  var p = result.Placemark[i].Point.coordinates;
                  didmsg += "<br>"+(i+1)+": <a href=\"javascript:place(&quot;"+result.Placemark[i].address +"@"+p[1]+","+p[0]+"&quot;,&quot;"+daddr +"&quot;,"+ did + ")\">"+ result.Placemark[i].address+"<\/a>";
                }
                didyoumean(did,didmsg);
              }
              // ===== If there was a single marker =====
              else {
               //didmsg = "";
                

                
                
                var p = result.Placemark[0].Point.coordinates;
                place(saddr+'@'+p[1]+","+p[0],daddr,did);
          
          
              }
            }
            // ====== Decode the error status ======
            else {
              var reason="Code "+result.Status.code;
              if (reasons[result.Status.code]) {
                reason = reasons[result.Status.code]
              } 
              alert('Could not find "'+saddr+ '" ' + reason);
            }
          }
          );
        ////

      }

			function place(s,t,did)
			{
				 gmarkers[did].closeInfoWindow();
				 //gmarkers[did].hide();
         gdir.load("from: "+s+" to: "+t, opts);
         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);
      });
			}
      // This function picks up the click and opens the corresponding info window
      
      function didyoumean(i,msg) {
        msg+='<br><a href="javascript:tohere('+i+')">&lt;&lt;Back</a>'
        gmarkers[i].openInfoWindowHtml(outwin(msg));
      }
      function myclick(i) {
        gmarkers[i].openInfoWindowHtml(outwin(htmls[i]));
      }

      // functions that open the directions forms
      function tohere(i) {
        gmarkers[i].openInfoWindowHtml(outwin(to_htmls[i]));
      }
      
      function showadd(i){
      	gmarkers[i].openInfoWindowHtml(outwin('<div style="width:240px">'+adds[i]+'</div>'));
      }
      
      
      function showinfo(i) {
      	
        gmarkers[i].openInfoWindowHtml(outwin(htmls[i]));
      }
      
      function fromhere(i) {
        gmarkers[i].openInfoWindowHtml(outwin(from_htmls[i]));
      }

			function TextControl(value,left,top,clickFunction) {
            this.value_ = value||"button";
            this.left_ = left||77;
            this.top_ = top||7;
            this.clickFunction_ = clickFunction;
        }
        TextControl.prototype = new GControl();
        TextControl.prototype.initialize = function(map)
        {
            var container = document.createElement("div");
            var containerDiv = document.createElement("div");
            this.setButtonStyle_(containerDiv);
            container.appendChild(containerDiv);
            containerDiv.appendChild(document.createTextNode(this.value_));
            GEvent.addDomListener(containerDiv, "click", this.clickFunction_);
            this.map_ = map;
            this.map_.getContainer().appendChild(container);       
            return container;
        }
        TextControl.prototype.getDefaultPosition = function()
        {
            return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(this.left_,this.top_));
        }
        TextControl.prototype.setButtonStyle_ = function(button)
        {
            button.style.color = "#000000";
            button.style.backgroundColor = "white";
            button.style.font = "12px 'Arial'";
            button.style.border = "1px solid black";
            button.style.padding = "1px";
            button.style.marginBottom = "3px";
            button.style.textAlign = "center";
            button.style.width = "6em";
            button.style.cursor = "pointer";
            button.style.fontWeight="bold";
        }

      // create the map
      function resetit()
      {
      	map.setCenter(new GLatLng(21.453068633086783, -157.97515869140625), 10);
	gdir.clear();
	 for (var i = 0; i < gmarkers.length; i++) {
		 gmarkers[i].closeInfoWindow();
	}	
      }
      
      function outwin(msg)
      {
      	return '<div class="win">'+msg+'</div>';
      }
      var map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(21.453068633086783, -157.97515869140625), 10);
      map.addControl(new TextControl("Reset",77,7,resetit));
      
      ////
      geo = new GClientGeocoder();
      ////

      // === 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 ===
      /*
      
      */


			        
        
      // Read the data from example.xml
      var request = GXmlHttp.create();
      request.open("GET", "map/example.xml", true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          var xmlDoc = GXml.parse(request.responseText);
          // obtain the array of markers and loop through it
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");
          
          for (var i = 0; i < markers.length; i++) {
            // obtain the attribues of each marker
            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 label = markers[i].getAttribute("label");
            var addinfo=markers[i].getAttribute("add");
            var typ=markers[i].getAttribute("typ");
            
            // create the marker
            var marker = createMarker(point,label,html,addinfo,typ);
            map.addOverlay(marker);
          }
          // put the assembled side_bar_html contents into the side_bar div
          //document.getElementById("side_bar").innerHTML = side_bar_html;
        }
      }
      request.send(null);
    }

    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }

