
//
// geocoder: http://www.localsearchmaps.com/geo
// document: http://emad.fano.us/blog/?p=277
//
// Google Maps API Version 2 Documentation
//  http://www.google.com/apis/maps/documentation/
//

var g_listing_list          = new Array();
var g_geoed_listing_list    = new Array();
var g_geoed_lat_lng_list    = new Array();
var g_run_google_map        = false;
var g_google_map_running    = false;
var g_map_container_id      = "map_container";
var g_map_area_id           = "map_area";
var g_zoom_level            = 11;               // from 0 to 17


function r7_listing( id, query, info )
{
    this.id     = id;
    this.info   = info;
    this.query  = query;
    this.geoed  = false;
    this.lat    = 0;
    this.lng    = 0;
}

function r7_add_listing( id, query, info )
{
    var listing = new r7_listing( id, query, 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 r7_lat_lng( valid, lat, lng )
{
    this.valid  = valid;
    this.lat    = lat;
    this.lng    = lng;
}

function r7_geo_ok( lat, lng, city, state, country )
{

    google_map_msg( "Got Geocoding Response OK: latitude=" + lat + " longitude=" + lng );

    var l = new r7_lat_lng( true, lat, lng );

    return ( l );
}

function r7_geo_error( msg )
{
    google_map_msg( "Got Geocoding Response Error: " + msg );

    var l = new r7_lat_lng( false, 0, 0 );

    return ( l );
}


function google_map_destroy()
{
    var o = document.getElementById( g_map_container_id );
    if ( o ) o.parentNode.removeChild( o );

    return;
}

function google_map_msg( msg )
{
    var o = document.getElementById( g_map_area_id );
    if ( !o ) return;

    o.innerHTML = o.innerHTML + "<br>" + msg;

}

function unload_google_map()
{
    if ( g_google_map_running ) {
        GUnload();
    }
}

function google_map()
{
    g_google_map_running = true;

    google_map_msg( "Loading Google Map ..." );

    var map = new GMap2( document.getElementById( g_map_area_id ) );
    map.addControl( new GLargeMapControl() );
    map.addControl( new GMapTypeControl() );

        // Create our marker icon
    var icon = new GIcon();
    icon.image  = "images/google_marker.gif";
    icon.shadow = "images/google_marker_shadow.gif";
    icon.iconSize   = new GSize( 30, 40 );
    icon.shadowSize = new GSize( 40, 40 );
    icon.iconAnchor = new GPoint( 10, 40 );
    icon.infoWindowAnchor = new GPoint( 15, 1 );

        // first listing is being centered
    var listing = g_geoed_listing_list[ 0 ];
    var point = new GLatLng( listing.lat, listing.lng );
    map.setCenter( point, g_zoom_level );

    var marker = new GMarker( point, icon );
    marker.info = r7_decode( listing.info );
    GEvent.addListener( marker, "mouseover", function() { this.openInfoWindowHtml( this.info ); } );
    GEvent.addListener( marker, "click", function() { this.openInfoWindowHtml( this.info ); } );
    map.addOverlay( marker );
    marker.openInfoWindowHtml( marker.info );

        // the rest of the listings
    for ( var i=1; i<g_geoed_listing_list.length; i++ ) {
        var listing = g_geoed_listing_list[ i ];
        var point = new GLatLng( listing.lat, listing.lng );
        var marker = new GMarker( point, icon );
        marker.info = r7_decode( listing.info );
        GEvent.addListener( marker, "mouseover", function() { this.openInfoWindowHtml( this.info ); });
        GEvent.addListener( marker, "click", function() { this.openInfoWindowHtml( this.info ); });
        map.addOverlay( marker );
    }

}

function geo_query_monitor()
{
    var sent = g_listing_list.length;
    var rcvd = g_geoed_lat_lng_list.length;

    if ( sent != rcvd ) {
        setTimeout( "geo_query_monitor()", 500 );
        return;
    }

    google_map_msg( "OK. All geocoding is completed." );

    for ( i=0; i<rcvd; i++ ) {
        var listing = g_listing_list[i];
        var lat_lng = g_geoed_lat_lng_list[i];
        if ( lat_lng.valid ) {
            listing.lat = lat_lng.lat;
            listing.lng = lat_lng.lng;
            g_geoed_listing_list.push( listing );
        }
    }

    var n = g_geoed_listing_list.length;
    if ( n <= 0 ) {
        google_map_msg( "Nothing to display. Quit." );
        alert("Sorry. Nothing to display on Google Map.");
        google_map_destroy();
        return;
    }

    google_map_msg( "OK. Loading Google Map ..." );
    google_map();
}

function run_geo_query()
{
    var n = g_listing_list.length;
    for ( var i=0; i<n; i++) {
        var listing = g_listing_list[i];
        google_map_msg( "Sending Geocoding Request for Listing ID " + listing.id + " ..." );
        geo_query( listing, i );
    }

    google_map_msg( "Waiting for Geocoding Responses. We need " + n + " responses.");

    geo_query_monitor();
}

function geo_query( listing, index )
{
    var geo_query = listing.query;
    var geo_ok    = 'g_geoed_lat_lng_list[' + index + ']=r7_geo_ok';
    var geo_error = 'g_geoed_lat_lng_list[' + index + ']=r7_geo_error';

    geo_ok      = escape( geo_ok );
    geo_error   = escape( geo_error );

    var url = 'http://www.localsearchmaps.com/geo/?' + 'cb=' + geo_ok + '&cbe=' + geo_error + '&' + geo_query;
    var script   = document.createElement( "script" );
    script.src   = url;
    script.type  = "text/javascript";
    document.getElementsByTagName( "head" )[0].appendChild( script );

}

function run_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 ) {
        google_map_destroy();
        return;
    }

    run_geo_query();
}
