<script type="text/javascript">
//<![CDATA[
var map = null;
var geocoder = null;
var center = null;
var updateX = null;
var updateY = null;
var marker = null;
var geopoints = new Array();

var addresses = new Array( new Array("3041 River Road West Goochland VA", "<strong>3 Fellers Bakery</strong><br>3041 River Road West<br>Goochland, VA <br>Telephone: (804)556-0671", true));

function showAddresses() {
  for (i=0; i < addresses.length; i++) {
 	  	showAddress(addresses[i][0], addresses[i][1], addresses[i][2]);
  }	
}
    	
function showAddress(address, htmlInfo, moveToPoint) {
 if (geocoder) {
   geocoder.getLatLng(
     address,
     function(point) {
       if (!point) {
         alert("Location not found:" + address);
       } else {              
         if (moveToPoint) {
           map.setCenter(point, 16);
         }
         var marker = new GMarker(point);
         map.addOverlay(marker);
         if (htmlInfo != "") {
           GEvent.addListener(marker, "click", function() {
              marker.openInfoWindowHtml(htmlInfo);
           });              
         }
       }
     }
   );
  }
}

function showGeopoints() {
  for (i=0; i < geopoints.length; i++) {
 	  	showGeopoint(geopoints[i][0], geopoints[i][1], geopoints[i][2], geopoints[i][3]);
  }	
}

function showGeopoint(longitude, latitude, htmlInfo, moveToPoint) {
  if (moveToPoint) {
    map.setCenter(new GLatLng(longitude, latitude), 16);
  }
  var marker = new GMarker(new GLatLng(longitude, latitude));
  map.addOverlay(marker);
  if (htmlInfo != "") {
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(htmlInfo);
    });              
     }   
}

function moveToGeopoint(index) {
	map.panTo(new GLatLng(geopoints[index][0], geopoints[index][1]));
}

function moveToAddress(index) {
  moveToAddressEx(addresses[index][0]); 
}

function moveToAddressEx(addressString) {
  if (geocoder) { 
   geocoder.getLatLng(
     addressString,
     function(point) {       
       if (!point) {
         alert("Location not found:" + addressString);
       } else {                                    
          center = point;
          map.panTo(point);           
       }
     });    
  }
}

function moveToAddressDMarker(addressString) {
  if (geocoder) { 
   geocoder.getLatLng(
     addressString,
     function(point) {       
       if (!point) {
         alert("Location not found:" + addressString);
       } else {                                    
          center = point;
          setZoomFactor(14);
          map.panTo(point);  
          addDragableMarker();         
       }
     });    
  }
}

function setZoomFactor(factor) {
	  map.setZoom(factor);
}

function addDragableMarker() {
  if (!marker) {
    marker = new GMarker(center, {draggable: true});
    map.addOverlay(marker);
       
    GEvent.addListener(marker, "dragend", function() {      
      var tpoint =  marker.getPoint();      
      document.getElementById(updateX).value = tpoint.lat();
      document.getElementById(updateY).value = tpoint.lng();              
  });

  } else {
  	marker.setPoint(center);  	 
  }
  
  var tpoint =  marker.getPoint();      
  document.getElementById(updateX).value = tpoint.lat();
  document.getElementById(updateY).value = tpoint.lng();              
}
    	
function initNXGMap(mapElement) {
alert('hi');
 	if (GBrowserIsCompatible()) {
		map = new GMap2(mapElement);        
		geocoder = new GClientGeocoder(); map.addControl(new GLargeMapControl());
 map.addControl(new GMapTypeControl());
 map.addControl(new GOverviewMapControl());
updateX="coordX"; updateY="coordY";    showAddresses();
    showGeopoints();

 	}
    	}
     //]]>
  	 </script>

