// JavaScript Document

var address = "";
var addresshtml = "";

var map = null;
var geocoder = null;

function setAddress(address1, city, state, zip)
{
	address = address1 + ", " + city + ", " + state + " " + zip;
	addresshtml = address1 + "<br />" + city + ", " + state + " " + zip;
}
		
function initialize() 
{
	if (GBrowserIsCompatible()) 
	{
		map = new GMap2(document.getElementById("map_canvas"));
		geocoder = new GClientGeocoder();
	}
}
		
function showAddress(address) 
{
	if (geocoder) 
	{
		geocoder.getLatLng(
			address,
			function(point)
			{
				//if the address returns an error
				if (!point) 
				{
					//for exceptions, geoXml msid shouldn't change as long as we are using the "Virginia Cooper­ative Exten­sion Exceptions" map
					//GLatLng need to be changed for new exceptions.  I get those by looking at that data in the map link after adding the pin for that location
					//However, be careful when getting the Lat and Long, as it marks the center of the map NOT the location of the pin.
					
					//Highland County
					if (address == "83 Highland Center Drive, Monterey, VA 24465")
					{
						map_canvas = document.getElementById('map_canvas');
						
						var haschildren = true;
						
						while(haschildren)
						{
							haschildren = map_canvas.hasChildNodes();
							if(haschildren)
							{
								map_canvas.removeChild(map_canvas.getElementsByTagName('div')[0]);
							}
						}
						
						if (GBrowserIsCompatible()) 
						{
        					var map2 = new GMap2(document.getElementById("map_canvas"));
							var geoXml = new GGeoXml("http://maps.google.com/maps/ms?ie=UTF8&hl=en&msa=0&output=kml&msid=110138830572995352606.000465f1cbaa211b35d04");
        					map2.addControl(new GSmallMapControl());
							
        					map2.setCenter(new GLatLng(38.410441, -79.586881), 14);
							map2.openInfoWindowHtml(new GLatLng(38.410441, -79.586881),addresshtml);
        					map2.addOverlay(geoXml);
						}
					}
					
					//Gloucester County
					else if (address == "7400 Carriage Court, Gloucester, VA 23061")
					{
						map_canvas = document.getElementById('map_canvas');
						
						var haschildren = true;
						
						while(haschildren)
						{
							haschildren = map_canvas.hasChildNodes();
							if(haschildren)
							{
								map_canvas.removeChild(map_canvas.getElementsByTagName('div')[0]);
							}
						}
						
						if (GBrowserIsCompatible()) 
						{
        					var map2 = new GMap2(document.getElementById("map_canvas"));
							var geoXml = new GGeoXml("http://maps.google.com/maps/ms?ie=UTF8&hl=en&msa=0&output=kml&msid=110138830572995352606.000465f1cbaa211b35d04");
        					map2.addControl(new GSmallMapControl());
							
        					map2.setCenter(new GLatLng(37.415897,-76.528101), 14);
							map2.openInfoWindowHtml(new GLatLng(37.415897,-76.528101),addresshtml);
        					map2.addOverlay(geoXml);
						}
					}
					
				}
				//these are maps which validate but show the wrong location on the map
				
				//Hanover County
				else if (address == "13224 Hanover Courthouse Rd, Hanover, VA 23069")
				{
					var map2 = new GMap2(document.getElementById("map_canvas"));
					var geoXml = new GGeoXml("http://maps.google.com/maps/ms?ie=UTF8&hl=en&msa=0&output=kml&msid=110138830572995352606.000465f1cbaa211b35d04");
        			map2.addControl(new GSmallMapControl());	
					
					//change these
					map2.setCenter(new GLatLng(37.765486,-77.367659), 14);
					map2.openInfoWindowHtml(new GLatLng(37.765486,-77.367659),addresshtml);
        			map2.addOverlay(geoXml);
				}
				
				//Buckingham County
				else if (address == "54 Administration Lane, Buckingham, VA 23921")
				{
					var map2 = new GMap2(document.getElementById("map_canvas"));
					var geoXml = new GGeoXml("http://maps.google.com/maps/ms?ie=UTF8&hl=en&msa=0&output=kml&msid=110138830572995352606.000465f1cbaa211b35d04");
        			map2.addControl(new GSmallMapControl());	
					
					//change these
					map2.setCenter(new GLatLng(37.55222,-78.551576), 14);
					map2.openInfoWindowHtml(new GLatLng(37.55222,-78.551576),addresshtml);
        			map2.addOverlay(geoXml);
				}
				
				//else, all is well and print out the map
				else 
				{
					map.setCenter(point, 14);
					var marker = new GMarker(point);
					map.addOverlay(marker);
					marker.openInfoWindowHtml(addresshtml);
					map.addControl(new GSmallMapControl());
				}
			}
		);
	}
}