/*
 * Google Maps API Version 2
 * http://www.google.com/apis/maps/documentation/
*/
function js_unescape( str )
{
    var r = str;

    r = r.replace( /%22%/g, "\"" );
    r = r.replace( /%27%/g, "'" );

    return ( r );
}

function Map_object( lat, lon, image, info )
{
    this.lat    = lat;
    this.lon    = lon;
    this.image  = image;
    this.info   = info;
}

function Map_config( map_area_id, zoom )
{
    this.map_area_id = "g_map";
    this.zoom = 13;

    if ( map_area_id )  this.map_area_id = map_area_id;
    if ( zoom )         this.zoom = zoom;
}

function google_map( map_container_id, zoom_level, center_object, object_list, map_type )
{

        // optional: object_list, map_type
        // map_type: G_NORMAL_MAP G_SATELLITE_MAP G_HYBRID_MAP
    var map = new GMap2( document.getElementById( map_container_id ) );
    map.addControl( new GLargeMapControl() );
    map.addControl( new GMapTypeControl() );

        // Create our marker icon - base
    var base_icon    = new GIcon();
    base_icon.image      = "images/map_icon_for_sale.gif";
    base_icon.shadow     = "images/map_icon_shadow.gif";
    base_icon.iconSize   = new GSize( 40, 44 );
    base_icon.shadowSize = new GSize( 60, 44 );
    base_icon.iconAnchor = new GPoint( 10, 40 );
    base_icon.infoWindowAnchor = new GPoint( 10, 25 );

        // center object
    var point = new GLatLng( center_object.lat, center_object.lon );
    map.setCenter( point, zoom_level );
    if ( map_type ) {
        map.setMapType( map_type );
    }

    var icon    = new GIcon( base_icon );
    icon.image  = center_object.image;
    var marker  = new GMarker( point, icon );
    if ( center_object.info ) {
        marker.info = js_unescape( center_object.info );
        GEvent.addListener( marker, "mouseover", function() { this.openInfoWindowHtml( this.info ); } );
        GEvent.addListener( marker, "click", function() { this.openInfoWindowHtml( this.info ); } );
    }
    map.addOverlay( marker );
    if ( center_object.info ) {
        marker.openInfoWindowHtml( marker.info );
    }

        // object list
    if ( object_list ) {
        for (var i=0; i<object_list.length; i++ ) {
            var o = object_list[ i ];
            var point = new GLatLng( o.lat, o.lon );
            var icon  = new GIcon( base_icon );
            icon.image = o.image;
            var marker = new GMarker( point, icon );
            if ( o.info ) {
                marker.info = js_unescape( o.info );
                GEvent.addListener( marker, "mouseover", function() { this.openInfoWindowHtml( this.info ); } );
                GEvent.addListener( marker, "click", function() { this.openInfoWindowHtml( this.info ); } );
            }
            map.addOverlay( marker );
        }
    }
}

function Map_object_v2( id, lat, lon, image, info )
{
    this.id     = id;
    this.lat    = lat;
    this.lon    = lon;
    this.image  = image;
    this.info   = info;
}

function google_map_config_v2()
{
        // set defaults
        // Use:
        //  var config = google_map_config_v2()
        //  config.zoom_level = 5;
        //  google_map_v2( center_object, object_list, config );

    this.map_area_id    = "g_map";
    this.zoom_level     = 13;
    this.map_control    = 1;    // 0: no control 1: GLargeMapControl() 2: GSmallMapControl()
    this.map_type       = 1;    // 0: no control 1: G_NORMAL_MAP 2: G_SATELLITE_MAP 3: G_HYBRID_MAP
}

function google_map_base_icon()
{
        // Create our marker icon - base
    var base_icon = new GIcon();
    base_icon.image      = "images/map_icon_for_sale.gif";
    base_icon.shadow     = "images/map_icon_shadow.gif";
    base_icon.iconSize   = new GSize( 40, 44 );
    base_icon.shadowSize = new GSize( 60, 44 );
    base_icon.iconAnchor = new GPoint( 35, 40 );
    base_icon.infoWindowAnchor = new GPoint( 10, 25 );

    return ( base_icon );
}

function google_map_v2( center_object, object_list, map_config )
{
    var config = map_config? map_config : new google_map_config_v2();

        // optional: map_config

    var map = new GMap2( document.getElementById( config.map_area_id ) );

    var map_control;
    switch ( config.map_control ) {
        case    0:
            map_control = false;
            break;
        case    1:
            map_control = new GLargeMapControl();
            break;
        case    2:
            map_control = new GSmallMapControl();
            break;
        default:
            map_control = new GLargeMapControl();
            break;
    }
    if ( map_control ) map.addControl( map_control );

    var map_type;
    switch ( config.map_type ) {
        case    0:
            map_type = false;
            break;
        case    1:
            map_type = G_NORMAL_MAP;
            break;
        case    2:
            map_type = G_SATELLITE_MAP;
            break;
        case    3:
            map_type = G_HYBRID_MAP;
            break;
        default:
            map_type = G_NORMAL_MAP;
            break;
    }
    if ( map_type ) map.addControl( new GMapTypeControl() );

        // center object
    var point = new GLatLng( center_object.lat, center_object.lon );
    map.setCenter( point, config.zoom_level );
    if ( map_type ) map.setMapType( map_type );

    if ( center_object.id != 0 ) {
        var base_icon = google_map_base_icon();
        var icon    = new GIcon( base_icon );
        icon.image  = center_object.image;
        var marker  = new GMarker( point, icon );
        if ( center_object.info ) {
            marker.info = js_unescape( center_object.info );
            GEvent.addListener( marker, "mouseover", function() { this.openInfoWindowHtml( this.info ); } );
            GEvent.addListener( marker, "click", function() { this.openInfoWindowHtml( this.info ); } );
        }
        map.addOverlay( marker );
        if ( center_object.info ) {
            marker.openInfoWindowHtml( marker.info );
        }
    }
        // object list
    if ( object_list ) {
        for (var i=0; i<object_list.length; i++ ) {
            var o = object_list[ i ];
            var point = new GLatLng( o.lat, o.lon );
            var icon  = new GIcon( base_icon );
            icon.image = o.image;
            var marker = new GMarker( point, icon );
            if ( o.info ) {
                marker.info = js_unescape( o.info );
                GEvent.addListener( marker, "mouseover", function() { this.openInfoWindowHtml( this.info ); } );
                GEvent.addListener( marker, "click", function() { this.openInfoWindowHtml( this.info ); } );
            }
            map.addOverlay( marker );
        }
    }

    return ( map );
}

function google_map_center( map, map_object, zoom_level )
{
    var zoom = zoom_level? zoom_level : 10;

    if ( !map ) return;
    if ( !map_object ) return;

    var point = new GLatLng( map_object.lat, map_object.lon );
    map.setCenter( point, zoom_level );

    var base_icon = google_map_base_icon();
    var icon    = new GIcon( base_icon );
    icon.image  = map_object.image;
    var marker  = new GMarker( point, icon );
    if ( map_object.info ) {
        marker.info = js_unescape( map_object.info );
        GEvent.addListener( marker, "mouseover", function() { this.openInfoWindowHtml( this.info ); } );
        GEvent.addListener( marker, "click", function() { this.openInfoWindowHtml( this.info ); } );
    }
    map.addOverlay( marker );
    if ( map_object.info ) {
        marker.openInfoWindowHtml( marker.info );
    }

}

/* vim: set expandtab sw=4 ts=4 sts=4: */

var g_map_config = new Map_config();
function bl_map_start()
{
    if ( !GBrowserIsCompatible() ) {
        alert( "Sorry. Google map is not available. Because \n\nThe browser is not able to connect to Google map server at this moment.\n\nOr your browser is not compatible with Google map." );
        return;
    }

    var center = g_map_data_list.shift();
    google_map( g_map_config.map_area_id, g_map_config.zoom, center, g_map_data_list, G_NORMAL_MAP );	
}

function bl_map_stop()
{
    GUnload();
}

function bl_view_start()
{    	
    if ( typeof g_map_data_list != 'undefined' ) {
        bl_map_start();
    }
}

function bl_view_stop()
{
    if ( typeof g_map_data_list != 'undefined' ) {
        bl_map_stop();
    }
}
