/* google-maps.js -- JavaScript code for interfacing with Google Maps.  */

/*
 * Copyright (C) 2005 Tudor Hulubei <tudor@hulubei.net>.
 * Distributed under the GNU General Public License.
 * Heavily based on the Google Maps API examples.
 */


var g_map;
var g_markers;


/* Create a marker whose info window displays the given text.  */
function CreateMarker(point, html)
{
    var marker = new GMarker(point);

    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });

    return marker;
}


function GenerateLatitudeLongitudeString()
{
    var center = g_map.getCenter();
    var mapCoordinates = 'Latitude: ' + center.lat() + ', Longitude: ' + center.lng();
    document.getElementById("MapCoordinates").innerHTML = mapCoordinates;
    window.status = DefaultWindowStatus();
}


function ReframeMap(category, categoryLatitude, categoryLongitude, categoryZoom)
{
    ShowMap(category, categoryLatitude, categoryLongitude, categoryZoom, true);
}


function ShowItemOnMap(category, stem, type)
{
    SetCookie(g_mapCookie, category + ':' + stem + ':' + type, '', '/');
    ShowMap(category, 48.872337341308594, 2.331676483154297, 0);
}


function PositionOverview(x, y)
{
    var omap = document.getElementById("map_overview");
    omap.style.left = x + "px";
    omap.style.top = y + "px";
    
    // Restyling.
    omap.firstChild.style.border = "1px solid gray";
    omap.firstChild.firstChild.style.left = "4px";
    omap.firstChild.firstChild.style.top = "4px";
}


function ShowMap(
    category,
    categoryLatitude,
    categoryLongitude,
    categoryZoom,
    reframe)
{
    if (!GBrowserIsCompatible())
    {
        document.getElementById("MapCoordinates").innerHTML =
            "Unsupported browser or key mismatch!";
        return;
    }

    var mapCookie = GetCookie(g_mapCookie);
    var mapCookieCategory = '';
    var mapCookieStem = '';
    var mapCookieType = '';
    var mapCookieMatches = false;
    var initialized = false;

    if (mapCookie)
    {
        /* Expect "category:stem:type".  */
        var parts = mapCookie.split(":");

        if (parts.length >= 2)
        {
            mapCookieCategory = parts[0];
            mapCookieStem = parts[1];
        }

        if (parts.length >= 3)
            mapCookieType = parts[2];

        mapCookieMatches = (mapCookieCategory == category);
    }

    if (!g_map)
    {
        g_map = new GMap2(document.getElementById("map"));
        g_map.addControl(new GLargeMapControl());
        g_map.addControl(new GMapTypeControl());
        g_map.addControl(new GScaleControl());

        /* The Map Overview Control implementation is kinda flaky, so
	   it's disabled for now.  PositionOverview() should be
	   removed if it turns out that it is not necessary in the
	   final implementation.  */
        /*
        g_map.addControl(new GOverviewMapControl(new GSize(160, 160)));
        setTimeout("PositionOverview(?, ?);", 1);
        */
    }
    else
        initialized = true;

    var mapType;
    if (mapCookieType == "Street")
        mapType = G_NORMAL_MAP;
    else if (mapCookieType == "Satellite")
        mapType = G_SATELLITE_MAP;
    else
        mapType = G_HYBRID_MAP;

    point = new GLatLng(categoryLatitude, categoryLongitude);
    g_map.setCenter(point, categoryZoom, mapType);

    var request = GXmlHttp.create();
    request.open("GET", category + "-Map.xml", true);
    request.onreadystatechange = function()
    {
        if (request.readyState == 4)
        {
            var xmlDoc = request.responseXML;
            var markers = xmlDoc.documentElement.getElementsByTagName("marker");
            if (!g_markers)
                g_markers = new Array(markers.length);
            else
            {
                if (g_markers.length != markers.length)
                    alert('Bad size of markers array!');
            }

            for (var n = 0; n < markers.length; n++)
            {
                var stem = markers[n].getAttribute("stem");
                var description = markers[n].getAttribute("description");
                var thumbnail = markers[n].getAttribute("thumbnail");
                var link = markers[n].getAttribute("link");
                var width = parseInt(markers[n].getAttribute("width"));
                var height = parseInt(markers[n].getAttribute("height"));
                var latitude = parseFloat(markers[n].getAttribute("latitude"));
                var longitude = parseFloat(markers[n].getAttribute("longitude"));
                var point = new GLatLng(latitude, longitude);
                var zoom = parseInt(markers[n].getAttribute("zoom"));
                var isDefault = markers[n].getAttribute("default");
                var html =
                    '<b>' + description + '</b><br/>' +
                    '<a href="' + link + '">' +
                    '<img style="border: solid 3px black;" src="' +
                    thumbnail + '"' +
                    ' width="' + width + '"' +
                    ' height="' + height + '"' +
                    '/></a>';

                var marker;

                if (initialized)
                    marker = g_markers[n];
                else
                {
                    marker = CreateMarker(point, html);
                    g_map.addOverlay(marker);
                    g_markers[n] = marker;
                }

                /* Opening the info window will reposition the map.  */
                if (mapCookieMatches)
                {
                    if (mapCookieStem == stem)
                    {
                        if (!reframe)
                            g_map.setZoom(zoom);
                        marker.openInfoWindowHtml(html);
                    }
                }
                else
                {
                    if (n == 0 || isDefault == "true")
                        marker.openInfoWindowHtml(html);
                }
            }
        }

        window.status = DefaultWindowStatus();
    }

    /* Print the latitude and longitude.  */
    GEvent.addListener(g_map, "moveend", GenerateLatitudeLongitudeString);

    request.send(null);

    GenerateLatitudeLongitudeString();
    window.status = DefaultWindowStatus();
}


function photoIndexMain(
    category,
    categoryLatitude,
    categoryLongitude,
    categoryZoom)
{
    main();
    ShowMap(category, categoryLatitude, categoryLongitude, categoryZoom);
}

