$(function(){
	$('a[rel=external]').each(function(){this.target="_blank"});
	//$('#navigation li').hover(function(){$(this).addClass('hover');}, function(){$(this).removeClass('hover');});
	$('#navigation a').each(function(){ if(window.location == this.href) $(this).addClass('indicator')});
	if($("#map").length==1){
        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 = [];


              // A function to create the marker and set up the event window
              function createMarker(point,name,html) {
                var marker = new GMarker(point);

                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>' +
                   '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.openInfoWindowHtml(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 ====
                var opts = {};
                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);
		        map.getInfoWindow().hide();
		        document.getElementById("directions").style.height = "auto";
              }


              // 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]);
              }


	        // create the map
	        var map = new GMap2(document.getElementById("map"));
	        map.addControl(new GLargeMapControl3D());
	        map.setCenter(new GLatLng(34.416502,-84.113356), 14);

	        var gdir=new GDirections(map, document.getElementById("directions"));
	        var point = new GLatLng(34.416502,-84.113356);
	        var marker = createMarker(point,"PrayDawson","<p style='margin: 0;'>415 Highway 53 East<br />Dawsonville, GA  30534</p>");
	        map.addOverlay(marker);
	
	        marker.openInfoWindowHtml(htmls[0]);
	
	        document.onunload = "GUnload()";
        }
    }
	
});
