
//<![CDATA[
var map = null;
var geocoder = null;
var center = null;
var updateX = null;
var updateY = null;
var marker = null;
var geopoints = new Array();
    
function showAddress(address, htmlInfo, moveToPoint) {
    if (geocoder) {
        geocoder.getLatLng(
            address,
            function(point) {
                if (!point) {
                    // alert("Location not found:" + address);
                    moveToPoint = false;
                    return false;
                } else {
                    if (moveToPoint) {
                        map.setCenter(point, 15);
                    }
                    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), 5);
    }
    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) {
    alert(addressString);
    if (geocoder) { 
        geocoder.getLatLng(
            addressString,
            function(point) {       
                if (!point) {
                // alert("Location not found:" + addressString);
                return false;
            } else {                                    
                center = point;
                map.panTo(point);           
            }
        });    
    }
}

function moveToAddressDMarker(addressString) {
  if (geocoder) { 
   geocoder.getLatLng(
     addressString,
     function(point) {       
       if (!point) {
         // alert("Location not found:" + addressString);
         return false;
       } 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();              
}

google.load("maps", "2");

function initNXGMap(mapElement, address, htmlInfo, moveToPoint) {
    if (GBrowserIsCompatible()) {
//        alert(address);
//        alert(htmlInfo);
        map = new google.maps.Map2(mapElement); 
        
//        if (!geocoder )
//        {
            geocoder = new GClientGeocoder();
//        }
        map.setCenter(new GLatLng(0,0),1);
        map.addControl(new GSmallZoomControl());
        updateX="coordX"; 
        updateY="coordY"; 
        showAddress(address, htmlInfo, moveToPoint);
        //showGeopoints();
    }
}
//]]>