
function display_places() {
    jQuery.each(
        places,
        function(i, place) {
            var lat = place[1]
            var lon = place[2]
            var latlon = new GLatLng(lat, lon);
            var marker = new GMarker(latlon, {title: place[5]});
            map.addOverlay(marker);
            var url = place[0]
            GEvent.addListener(marker, "click", function() {
                window.location = url;
            });                                        
            var lat_offset = place[3]
            var lon_offset = place[4]
            var place_boundary = new GPolygon(
                [
                    new GLatLng(lat - lat_offset, lon - lon_offset),
                    new GLatLng(lat - lat_offset, lon + lon_offset),
                    new GLatLng(lat + lat_offset, lon + lon_offset),
                    new GLatLng(lat + lat_offset, lon - lon_offset),
                    new GLatLng(lat - lat_offset, lon - lon_offset)
                ],
                "#308141", 2, 1, "#308141", 0.3
            );
            map.addOverlay(place_boundary);
            /*var place_boundary = new GPolyline(
                [
                    new GLatLng(lat - lat_offset, lon - lon_offset),
                    new GLatLng(lat - lat_offset, lon + lon_offset),
                    new GLatLng(lat + lat_offset, lon + lon_offset),
                    new GLatLng(lat + lat_offset, lon - lon_offset),
                    new GLatLng(lat - lat_offset, lon - lon_offset)
                ],
                "#308141", 2, 1
            );
            map.addOverlay(place_boundary);*/
        }
    );
}

function Geocode() {
    find = $('#geocode_text').val();
    if (find) {
        $('#geocode_button').attr('disabled', true);
        $('#geocode_status').html('Please wait...');
        geocoder.setBaseCountryCode('au');
        geocoder.getLocations(
            find,
            function(d) {
                $('#geocode_button').attr('disabled', false);
                if (d.Status.code == 200) {
                    $('#geocode_status').html('Found.');
                    lon = d.Placemark[0].Point.coordinates[0]
                    lat = d.Placemark[0].Point.coordinates[1]
                    map.setCenter(new GLatLng(lat, lon), 11);
                } else {
                    $('#geocode_status').html('Not found!');
                }
            }
        );
    }
}

$(function () {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        //map.setMapType(G_HYBRID_MAP);
        map.setMapType(G_NORMAL_MAP);
        latlon = new GLatLng(centre_lat, centre_lon);
        map.setCenter(latlon, 7);
        //map.enableScrollWheelZoom();
        map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10)));
        map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,30)));
        //map.disableDragging();
    }
    display_places();
    /*var cvga_border = new GPolyline(
        [
            new GLatLng(-35.336175, 143.560181),
            new GLatLng(-36.418555, 143.612840),
            new GLatLng(-37.284294, 142.928229),
            new GLatLng(-37.055661, 142.779380),
            new GLatLng(-35.981043, 142.917808),
            new GLatLng(-35.336175, 143.560181)
        ],
        "#FFFF00", 2, 0.5
    );
    map.addOverlay(cvga_border);*/
    $('#geocode_button').attr('disabled', false);
    $('#geocode_button').click(Geocode);
});

