// JavaScript Document
var map;
var geocoder;
var directionsPanel;
var directions;
var html;
var end_html = '<br>Directions: <a href="javascript:tohere()">To here</a> - <a href="javascript:fromhere()">From here</a>';
var gmarkers ;
var to_htmls;
var from_htmls;
var beg_to_htmls = '<br><b>Directions: To here</b> - <a href="javascript:fromhere()">From here</a>' +
           '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="daddr" value="';
		   
var beg_from_htmls = '<br><b>Directions: <a href="javascript:tohere()">To here</a> - From here</b>' +
           '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="saddr" value="';
var end_htmls = '"/><br/>';	  
	  
	
var mapAddyArr = new Array;
mapAddyArr[0] = "816 Greenbrier Circle, Suite 208, Chesapeake, VA 23320";
mapAddyArr[1] = "2027 Thomas Drive, Panama City Beach, FL 32408";
mapAddyArr[2] = "22454 Three Notch Road, Suite 102, Lexington Park, MD 20653";
mapAddyArr[3] = "1201 M Street, SE, Suite 130, Washington, DC 20003";
mapAddyArr[4] = "16349 Dahlgren Rd, King George, VA 22485";

// Create a base icon for all of our markers that specifies the
// shadow, icon dimensions, etc.
var baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);
		
		
function initialize() {
	map = new GMap2(document.getElementById("map_canvas"),
	{ size: new GSize(850,400) } );
	map.setCenter(new GLatLng(37, -95), 4);
	map.addControl(new GLargeMapControl());
	map.enableScrollWheelZoom();
	geocoder = new GClientGeocoder();
	map.addControl(new GMapTypeControl());
	setTimeout("showAll()", 2000);
}

function showAll(){
	map.clearOverlays();
	for (var i = 0; i < mapAddyArr.length; i++) {
		showAddress(i);
	}
}

function showAddress(index) {
  geocoder.getLatLng(
    mapAddyArr[index],
    function(point) {
      if (!point) {
        /*alert(address + " not found");*/
		return null;
      } else {
       createMarker(point, index);
      }
    }
  );
}

// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, index) {

  // Create a lettered icon for this point using our icon class
  var letter = String.fromCharCode("A".charCodeAt(0) + index);
  var letteredIcon = new GIcon(baseIcon);
  letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

  // Set up our GMarkerOptions object
  markerOptions = { icon:letteredIcon };
  var marker = new GMarker(point, markerOptions);
	map.addOverlay(marker);
  GEvent.addListener(marker, "click", function() {
  
  		html = '<b>Address: </b><br />' + mapAddyArr[index];

// The info window version with the "to here" form open
        to_htmls = html + beg_to_htmls  + mapAddyArr[index] + end_htmls;
        // The info window version with the "to here" form open
        from_htmls = html + beg_from_htmls  + mapAddyArr[index] + end_htmls;

        // The inactive version of the direction info
        html = html + end_html;
		marker.openInfoWindowHtml(html);
        gmarkers = marker;
  });
  return marker;
}

// findLocation() is used to enter the sample addresses into the form.
function findLocation(index) {
	geocoder.getLocations(mapAddyArr[index], addAddressToMap);
}
	
// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
	map.clearOverlays();
	if (!response || response.Status.code != 200) {
		alert("Sorry, we were unable to geocode that address. Error# "+ response.Status.code);
	} else {
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		marker = new GMarker(point);
		map.addOverlay(marker);
		html = '<b>Address: </b><br />' + place.address;

		// The info window version with the "to here" form open
		to_htmls = html + beg_to_htmls  + place.address + end_htmls;
		// The info window version with the "to here" form open
		from_htmls = html + beg_from_htmls  + place.address + end_htmls;
		
		// The inactive version of the direction info
		html = html + end_html;
		marker.openInfoWindowHtml(html);
		gmarkers = marker;
        	
	}
}

// functions that open the directions forms
function tohere() {
	gmarkers.openInfoWindowHtml(to_htmls);
}
function fromhere() {
	gmarkers.openInfoWindowHtml(from_htmls);
}