/*
 *   Copyright (C) 2005  James Henstridge
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the
 *   Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall be
 *   included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 *   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 *   CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *   SOFTWARE.
 */

function createMarker(person) {
    var point = new GLatLng(parseFloat(person.getAttribute("lat")),
                            parseFloat(person.getAttribute("long")));
    var marker = new GMarker(point)
    marker.xmlNode = person;
    return marker;
}

function getElement(parent, elementname) {
    var child = parent.firstChild;
    while (child) {
        if (child.nodeType == child.ELEMENT_NODE &&
            child.nodeName == elementname) {
            return child;
        }
        child = child.nextSibling;
    }
    return null;
}
function getText(element) {
    var ret = '';
    var child = element.firstChild;
    while (child) {
        if (child.nodeType == child.TEXT_NODE) {
            ret += child.data;
        }
        child = child.nextSibling;
    }
    return ret;
}

function htmlEscape(str) {
    var ret = str.replace('<', '&lt;');
    ret = ret.replace('>', '&gt;');
    ret = ret.replace('"', '&quot;');
    ret = ret.replace('\'', '&apos;');
    return ret.replace('&', '&amp;');
}

function makeInfoWindowContent(personnode) {
    var ret = '<div>\n';
    var hackergotchi = getElement(personnode, 'hackergotchi');
    if (hackergotchi) {
        ret += '<img class="hackergotchi" src="' +
            htmlEscape(hackergotchi.getAttribute('href')) + '" />\n';
    }
    var name = getElement(personnode, 'name');
    ret += '<div class="name">' + htmlEscape(getText(name));
    ret += ' (' + htmlEscape(name.getAttribute('nick')) + ')</div>\n';
    var location = getElement(personnode, 'location');
    if (location) {
        ret += '<div class="location">' + htmlEscape(getText(location)) +
            '</div>\n';
    }
    var weblog = getElement(personnode, 'weblog');
    if (weblog) {
        ret += '<div class="weblog">';
        ret += '<a href="' + htmlEscape(weblog.getAttribute('href'));
        ret += '">' + htmlEscape(getText(weblog)) + '</a></div>\n';
    }
    ret += '</div>\n';
    return ret;
}

function handleMapClick(overlay, point) {
    /* an overlay was clicked: pop up an info window */
    if (overlay && overlay.xmlNode) {
        overlay.openInfoWindowHtml(makeInfoWindowContent(overlay.xmlNode));
    }
}

function createMap(div, peopleurl) {
    /* create the map */
    var map = new GMap2(div);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());
    map.setCenter(new GLatLng(0, 0), 2, G_SATELLITE_MAP);

    /* download the list of people */
    var request = GXmlHttp.create();
    request.open("GET", peopleurl, true);
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            var xmlDoc = request.responseXML;
            /* create markers for each child of the root element */
            var child = xmlDoc.documentElement.firstChild;
            while (child) {
                if (child.nodeType == child.ELEMENT_NODE) {
                    map.addOverlay(createMarker(child));
                }
                child = child.nextSibling;
            }
        }
    }
    request.send(null);

    GEvent.addListener(map, "click", handleMapClick);

    return map;
}
