/*
    guestmap.js
    written by Edoardo Sabadelli
    http://www.sabadelli.it/edoardo
*/

// event listener
/*
_addEvent(
    document,
    'load',
    load
);
*/
window.onload = load;

var oGMap;

function load()
{
    oGMap = new GMap2( document.getElementById( 'gmap' ) );

    oGMap.addControl( new GSmallMapControl() );
    oGMap.addControl( new GMapTypeControl() );
    oGMap.setCenter( new GLatLng( 46.090648, 13.435000 ), 7 );

    GDownloadUrl(
        '/edoardo/gmapxml' + location.search,
        function( oData, cResponseCode )
        {
            var oXml = GXml.parse( oData );
            var aGuests = oXml.documentElement.getElementsByTagName( 'guest' );

            for( var i = 0; i < aGuests.length; i++ )
            {
                oGMap.addOverlay( new createMarker( aGuests[i] ) );
            }
        }
    );
}

function createMarker( oGuest )
{
    var oPoint =
        new GLatLng(
            parseFloat( oGuest.getAttribute('lat') ),
            parseFloat( oGuest.getAttribute('long') )
        );
                              
    var oMarker = new GMarker( oPoint );

    GEvent.addListener(
        oMarker,
        'mouseover',
        function()
        {
            oGMap.closeInfoWindow();
            oMarker.openInfoWindowHtml
            (
                '<b>' + oGuest.getAttribute('name') + '</b><br />' +
                '<span class="location ' + oGuest.getAttribute('country') + '">' + oGuest.getAttribute('location') + '</span><br /><br />' +
                '<a href="#g' + oGuest.getAttribute('id') + '" title="jump to the message">read ' + ( oGuest.getAttribute('gender') == 'female' ? 'her' : 'his' ) + ' message...</a>'
            );
        }
    );

    return oMarker;
}

function checkGeocoding( oForm )
{
    var oGeocoder = new GClientGeocoder();

    var cLocation = oForm.elements['location'].value + ', ' + oForm.elements['country'].value;

    oGeocoder.getLocations(
        cLocation,
        function( oResponse )
        {
            if( ! oResponse || oResponse.Status.code != 200 )
            {
                alert( 'Sorry, Google geocoder cannot resolve this location: ' + cLocation );
            }
            else
            {
                if(
                    confirm( 'Google has geocoded the location you give (' + cLocation + ') and found the following address:\n' +
                             oResponse.Placemark[0].address + ' -  ' + oResponse.Placemark[0].Point.coordinates[1] + ' N, ' + oResponse.Placemark[0].Point.coordinates[0] + ' E\n\n' +
                             'If this is the place you mean please confirm to use the coordinates to pin yourself on the map, otherwise cancel for changing the location.' )
                )
                {
                    oForm.elements['lat'].value = oResponse.Placemark[0].Point.coordinates[1];
                    oForm.elements['long'].value = oResponse.Placemark[0].Point.coordinates[0];

                    oForm.submit();
                }
            }
        }
    );

    return false;
}

function showUser( oParams )
{
    if( oParams.id && oParams.lat && oParams.long )
    {
        oGMap.clearOverlays();

        oGMap.setCenter( new GLatLng( oParams.lat, oParams.long ), 10 );

        GDownloadUrl(
            '/edoardo/gmapxml?id=' + oParams.id,
            function( oData, cResponseCode )
            {
                var oXml = GXml.parse( oData );
                var aGuests = oXml.documentElement.getElementsByTagName( 'guest' );

                oGMap.addOverlay( new createMarker( aGuests[0] ) );
            }
        );
    }

    return false;
}

function centerMap( oParams )
{
    if( oParams.country )
    {
        var cCountry = oParams.country;

        var oGeocoder = new GClientGeocoder();

        oGeocoder.getLatLng(
            cCountry,
            function( oPoint )
            {
                if( ! oPoint )
                {
                    alert( 'Sorry, Google geocoder cannot find the coordinates to center the map on ' + cCountry + '. Uhm... that\'s strange?!' );
                }
                else
                {
                    oGMap.setZoom(5);
                    oGMap.panTo( oPoint );
                }
            }
        );
    }
    else if( oParams.lat && oParams.long )
    {
        oGMap.setCenter( new GLatLng( oParams.lat, oParams.long ), 10 );
    }

    return false;
}

// _addEvent: cross-browser event handler
function _addEvent( oObj, cEvent, rFunction )
{
    if( oObj.addEventListener )
    {
        oObj.addEventListener( cEvent, rFunction, false );
        return true;
    }
    else if( oObj.attachEvent )
    {
        return oObj.attachEvent( 'on' + cEvent, rFunction );
    }
    else
    {
        return false;
    }
}

