//
// Google Maps API Version 2 Documentation
//  http://www.google.com/apis/maps/documentation/
//

var g_map_container_id      = "map_container";
var g_map_area_id           = "map_area";
var g_zoom_level            = 11;               // from 0 to 17
var g_listing_list          = new Array();
var g_run_google_map        = false;
var g_google_map_running    = false;
var g_map                   = false;

function r7_listing( id, lat, lon, info )
{
    this.id     = id;
    this.lat    = lat;
    this.lon    = lon;
    this.info   = info;
}

function r7_add_listing( id, lat, lon, info )
{
    var listing = new r7_listing( id, lat, lon, info );
    g_listing_list.push( listing );
}

function r7_decode( str )
{
    var r = str;

    r = r.replace( /%22%/g, "\"" );
    r = r.replace( /%27%/g, "'" );

    return ( r );
}

function destroy_google_map()
{
    var o = document.getElementById( g_map_container_id );
    if ( o ) o.parentNode.removeChild( o );

    return;
}

function start_google_map()
{
    g_google_map_running = true;

    g_map= new GMap2( document.getElementById( g_map_area_id ) );
    icon = new GIcon();

    g_map.addControl( new GLargeMapControl() );
    g_map.addControl( new GMapTypeControl() );

    icon.image      = "images/google_marker.png";
    icon.shadow     = "images/google_marker_shadow.png";
    icon.iconSize   = new GSize( 30, 40 );
    icon.shadowSize = new GSize( 47, 40 );
    icon.iconAnchor = new GPoint( 15, 40 );
    icon.infoWindowAnchor = new GPoint( 15, 10 );

    var n = g_listing_list.length;
    for ( var i=0; i<n; i++) {
        var listing = g_listing_list[i];
        var point = new GLatLng( listing.lat, listing.lon );
        var marker= new GMarker( point, icon );

        if ( i == 0 ) {
            g_map.setCenter( point, g_zoom_level );
        }

        marker.info = r7_decode( listing.info );
        GEvent.addListener( marker, "mouseover", function() { this.openInfoWindowHtml( this.info ); });
        GEvent.addListener( marker, "click", function() { this.openInfoWindowHtml( this.info ); });
        g_map.addOverlay( marker );

        if ( i == 0 ) {
            marker.openInfoWindowHtml( marker.info );
        }
    }

}

function unload_google_map()
{
    if ( g_google_map_running ) {
        GUnload();
    }
}

function load_google_map()
{
    var none = false;

    if ( g_run_google_map ) {
        r7_google_map_listings();
        if (g_listing_list.length <= 0) none = true;
    } else {
        none = true;
    }
    if ( ! GBrowserIsCompatible() ) none = true;

    if ( none ) {
        destroy_google_map();
        return;
    }

    start_google_map();
}

