﻿var map = null;
var geocoder = null;
var GMAP_setCoords = null;
var GMAP_Coords = null;

function GMAP_loadGMap()
{
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        geocoder = new GClientGeocoder();
    }
}

function GMAP_unload()
{
    if (typeof(GUnload) != "undefined")
        GUnload();
}

function GMAP_showAddress(address,setCoords,addressVerified,lat,lng,name)
{
    if (!geocoder)
        GMAP_loadGMap();
        
    if (geocoder) {
        map.clearOverlays()
        GMAP_setCoords = (setCoords || null);
      
       
            GMAP_Coords = null;
            geocoder.getLatLng(
                address,
                function(point) {
                    GMAP_Coords = null;
                    if (!point) {
                        alert(address + " not found");
                        if (GMAP_setCoords)
                            GMAP_setCoords(null,null);
                    } else {
                        if (addressVerified == 1 && (lat !== null) && (lng !== null))
                        {
                          point = new GLatLng(lat,lng);
                        }
                        map.setCenter(point, 12);
                        var marker = new GMarker(point,{draggable: true,title : name});
                        GEvent.addListener(marker, "dragstart", function() {
		                map.closeInfoWindow();
		                });
        		        GEvent.addListener(marker, "dragend", function() {
		                GMAP_Coords = { "lat":marker.getPoint().lat(), "lng":marker.getPoint().lng() };
                        marker.openInfoWindowHtml('<span class=\"name\"><strong>'+ name+ '</strong></span>');
		                });
                        map.addOverlay(marker);
                        marker.openInfoWindowHtml('<span class=\"name\"><strong>'+ name+ '</strong></span>');
                        GMAP_Coords = { "lat":marker.getPoint().lat(), "lng":marker.getPoint().lng() };
                        $find(GMAP_mpuCheckMapClientID).show();
                        map.checkResize();
                    }
                    }
                );
           
    }
}

function GMAP_confirm()
{
    if (GMAP_setCoords && GMAP_Coords)
        GMAP_setCoords(GMAP_Coords.lat, GMAP_Coords.lng);
    GMAP_close();
}

function GMAP_close()
{
    GMAP_setCoords = null;
    GMAP_Coords = null;
    $find(GMAP_mpuCheckMapClientID).hide();
}
