var pinpointMap = new Array();

function pinpointGatherData(instance)
{
    var result = new Array();
    if ($("pinpointGmapsPreviewKey_" + instance)) {
        result["previewKey"] = $("pinpointGmapsPreviewKey_" + instance).value;
    }
    result["liveKey"]    = $("pinpointGmapsLiveKey_" + instance).value;
    result["lat"]        = $("pinpointGmapsLat_" + instance).value;
    result["lng"]        = $("pinpointGmapsLng_" + instance).value;
    if (pinpointMap[instance].map) {
        result["zoom"]       = pinpointMap[instance].map.getZoom();
    }
    result["address"]    = $("pinpointGmapsAddress_" + instance).value;

    return result;
}

function pinpointMapReady(instance, lat, lng, zoom)
{
    pinpointMap[instance] = new Object();
    if (GBrowserIsCompatible()) {
        var map = new GMap2($("pinpointMap_" + instance));
        pinpointMap[instance].map = map;

        if (lat == "" || lng == "") {
            lat = 37.4419;
            lng = -122.1419;
        }
        if (zoom == "") {
            zoom = 15;
        }

        var point = new GLatLng(lat - 0, lng - 0);
        map.setCenter(point, zoom - 0);
        var marker = new GMarker(point, {draggable: true});
        pinpointMap[instance].marker = marker;
        pinpointEnableDragging(instance, false);
        map.addOverlay(marker);

        map.addControl(new GSmallZoomControl());
        //map.openInfoWindow(map.getCenter(), document.createTextNode("Best restaurant in town"));
    }
}

function pinpointEnableDragging(instance, enabled)
{
    if (enabled) {
        pinpointMap[instance].marker.enableDragging();
    } else {
        pinpointMap[instance].marker.disableDragging();
    }
}

function pinpointSetCoordonates(instance, point)
{
    $("pinpointGmapsLat_" + instance).value = point.lat();
    $("pinpointGmapsLng_" + instance).value = point.lng();
}

function pinpointLocateAddress(instance, address, dragging)
{
    var geocoder = new GClientGeocoder();
    //var address = "121 Donald Lynch Blvd Marlborough, MA 01752";
    var map = pinpointMap[instance].map;

    geocoder.getLatLng(
        address,
        function(point) {
            if (!point) {
                alert(address + " not found");
            } else {
                map.setCenter(point, 13);
                if (pinpointMap[instance].marker) {
                    marker = pinpointMap[instance].marker;
                    marker.setLatLng(point);
                } else {
                    var marker = new GMarker(point, {draggable: true});
                    pinpointMap[instance].marker = marker;
                }
                pinpointSetCoordonates(instance, point);
                pinpointEnableDragging(instance, dragging);
                GEvent.addListener(marker, "dragstart", function() {
                    map.closeInfoWindow();
                });
                GEvent.addListener(marker, "dragend", function() {
                    var point = marker.getLatLng();
                    pinpointSetCoordonates(instance, point);
                    //marker.openInfoWindowHtml("Just bouncing along...");
                });


                map.addOverlay(marker);
                //marker.openInfoWindowHtml(address);
            }
        }
    );
}

