//<![CDATA[

var map;
var geocoder;

function loadMaps() {
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(0, 0), 1);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();
}

// 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("Could not find address on map.");
    } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.addOverlay(marker);
        map.setCenter(point, 8);

		// Scroll to top of page
		window.scroll(0,0);
				
		// Our info window content
        var infoTabs = [
          new GInfoWindowTab("Address", document.getElementById('address' + document.mapSearchForm.e.value).innerHTML),
          new GInfoWindowTab("Contact", document.getElementById('contact' + document.mapSearchForm.e.value).innerHTML)
        ];
		
        if (place.AddressDetails.Accuracy > 1) {
          marker.openInfoWindowTabsHtml(infoTabs);
		  GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowTabsHtml(infoTabs);
	      });
		}
    }
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
    var address = document.mapSearchForm.q.value;
	var eid = document.mapSearchForm.e.value;
    geocoder.getLocations(address, addAddressToMap);
}

// findLocation() is used to enter the sample addresses into the form.
function findLocation(address,eid) {
    document.mapSearchForm.q.value = address;
	document.mapSearchForm.e.value = eid;
    showLocation();
}

// showAddress() adds point to map when called
function showAddress(address) {
  geocoder.getLatLng(
    address,
    function(point) {
 		htmlcontent = '<p>' + place.address + '</p><p><a href="http://maps.google.com/?q=' + place.address + '">Get Directions</p>';		
        map.setCenter(point, 8);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(address);
    }
  );
}

//]]>