// Load the API
document.write('<script src="' + API + '" type="text/javascript"></script>');
$(document).ready(function(){
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById(COORDINATES_MAP));
        map.addControl(new GSmallMapControl());
        var lookupTimer = false;
	
    GEvent.addDomListener(document.getElementById('crosshair'),
        'dbclick', function() {
            map.zoomIn();
        }
    );

    var point;
    /* If latitude and longitude are populated, center there and add a flag*/
    if ($('#id_latitude').val() && $('#id_longitude').val()) {
            point = new GLatLng(
            $('#id_latitude').val(),
            $('#id_longitude').val()
        );
        map.setCenter(point, 14);
	
	var position =  new GLatLng($('#id_latitude').val(),
				    $('#id_longitude').val());
    } else {
        map.setCenter(new GLatLng(-12.069203,-77.077332), 11);
		// palce a draggable marker
		position = new GLatLng(-12.069203,-77.077332);
    }
	
    var is_draggable = $('#mapWrapper').hasClass("draggable");
    var marker = new GMarker(position, 
			     {icon: G_DEFAULT_ICON, draggable: is_draggable});
    map.addOverlay(marker);
	
    if (is_draggable){
		marker.enableDragging();
		// add a listener to the marker's on  drag event
		GEvent.addListener(marker, "drag", function(){
			$('#id_latitude').val(marker.getPoint().lat());
			$('#id_longitude').val(marker.getPoint().lng());
		    });
		}
    } else {
        alert("Sorry, the Google Maps API is not compatible with this browser");
    }
	map.checkResize();

});

