
function LoadTopMap () {

    if ( ! GBrowserIsCompatible() ) return false;

    var map = new GMap2( document.getElementById( 'map' ) );
    map.addControl( new GSmallMapControl() );
    map.addControl( new GMapTypeControl() );
    map.addControl( new GScaleControl() );
    // map.addControl( new GOverviewMapControl() );
    map.setCenter( new GLatLng( 36.2088, 138.3398 ), 6 );

    $.getJSON( '/json/estate.list.php', null, function ( result ) {
	map.clearOverlays();
	var bounds = new GLatLngBounds();
	$.each( result, function ( i, row ) {

	    var point = new GLatLng( row.latitude, row.longitude );
	    bounds.extend( point );
	    
	    var marker = new GMarker( point );
	    GEvent.addListener( marker, 'click', function() {
		$.get( '/json/estate.detail.php', { id: row.id }, function ( result ) {
		    marker.openInfoWindowHtml( result );
		} ) } );
	    map.addOverlay( marker );
	} );
	map.setCenter( bounds.getCenter(), map.getBoundsZoomLevel( bounds ) );
    } );

    GEvent.addListener( map, 'dragend', function () {

	var bounds = map.getBounds();
	var sw = bounds.getSouthWest();
	var ne = bounds.getNorthEast();

	var param = {};
	param.sw_lat = sw.lat();
	param.sw_lng = sw.lng();
	param.ne_lat = ne.lat();
	param.ne_lng = ne.lng();
	
	$.getJSON( '/json/estate.list.php', param, function ( result ) {
	    map.clearOverlays();
	    $.each( result, function ( i, row ) {
		var marker = new GMarker( new GLatLng( row.latitude, row.longitude ) );
		GEvent.addListener( marker, 'click', function() {
		    $.get( '/json/estate.detail.php', { id: row.id }, function ( result ) {
			marker.openInfoWindowHtml( result );
		    } ) } );
		map.addOverlay( marker );
	    } );
	} );
    } );
}

function LoadDetailMap () {

    if ( ! GBrowserIsCompatible() ) return false;

    var latlng = new GLatLng( $('#latitude').val(), $('#longitude').val() );
    var map = new GMap2( document.getElementById( 'map' ) );
    map.addControl( new GSmallMapControl() );
    map.addControl( new GMapTypeControl() );
    map.addControl( new GScaleControl() );
    map.setCenter( latlng, 14 );
    map.addOverlay( new GMarker( latlng ) );
}

function init () {

    $( 'a.remove' ).click( function () {
        return confirm( '削除します。\nよろしいですか？' );
    } );

}

$( init );
