/*
 * Creates a marker at the given point with the given text
 * @param GLatLng point The point to use
 * @param string title The marker header title
 * @param string body The body (can contain raw html)
 * @return GMarker The marker
 */
function createChildMarker(point, infoWindowHtml, icon)
{
    var marker = new GMarker(point, {draggable:false,icon:icon});
    GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(infoWindowHtml); });
    GEvent.addListener(marker, "infowindowopen", function() {
        this.opened = true;
    });
    GEvent.addListener(marker, "infowindowclose", function() {
        this.opened = false;
    });
    return marker;
}

/**
 * Apply view on a map
 * @param Object view The view must hold: (x,y,z) for setting center and zoom level and/or (m) for map type
 * @param GMap2 map The map to apply view on
 * @return void
 */
function applyMapView(view, map)
{
    if (view.m) {
        mapTypes = map.getMapTypes();
        map.setMapType(mapTypes[view.m]);
    }

    if (view.x && view.y) {
        map.setCenter(new GLatLng(view.x, view.y));
    }

    if (view.z) {
        map.setZoom(parseInt(view.z));
    }

    if (view.sm) {
        // apply selected marker
        if (window.markers[view.sm]) {
            GEvent.trigger(window.markers[view.sm], 'click');
        }
    }

    map.savePosition();

}

/**
 * Initializes 'link to this page' on the <a> node with the map
 * @param DOMNode node The <a> node
 * @param GMap2 map The map to get state from
 * @param string label The text to show in the link
 */
function showMapLink(node, map, label)
{
    var labelNode = document.createTextNode(label);
    var a = document.createElement('a');
    a.setAttribute('id', 'maplink');
    a.setAttribute('class', 'permalink');
    a.setAttribute('href', window.location.toString());
    a.appendChild(labelNode);
    a.map = map;
    a.onfocus = function ()
//    node.onclick = function ()
    {
        var url = window.location.toString();
        var currentFragment = getFragment(url);
        if (currentFragment.length > 0) {
            url = url.substring(0, url.length - currentFragment.length);
        }
        url = url + '#';

        // determine selected map type
        var m = -1;
        if ((mapTypes = this.map.getMapTypes()) && (mapType = this.map.getCurrentMapType())) {
            for (var i = 0; i < mapTypes.length; i++) {
                if (mapTypes[i] == mapType) m = i;
            }
        }
        if (m > -1) {
            url = url + (url.substr(url.length - 1, 1) != '#' ? ';' : '') + 'm=' + m.toString();
        }

        // get selected center
        center = this.map.getCenter();
        if (center) {
            url = url + (url.substr(url.length - 1, 1) != '#' ? ';' : '') + 'x=' + center.lat() + ';y=' + center.lng();
        }

        // determine selected zoom level
        z = this.map.getZoom();
        if (z) {
            url = url + (url.substr(url.length - 1, 1) != '#' ? ';' : '') + 'z=' + z.toString();
        }

        // determine selected marker
        if (window.markers) {
            for (el in window.markers) {
                if (window.markers[el].opened == true) {
                    url = url + (url.substr(url.length - 1, 1) != '#' ? ';' : '') + 'sm=' + el;
                    break;
                }
            }
        }

        if (url.substr(url.length - 1, 1) == '#') url = url.substr(0, url.length - 1);

        this.setAttribute('href', url);
        return true;
    }
    a.onmouseover = a.onfocus;

    insertAfter(a, node);
}

/**
 * Prepares an html element for Google maps by adding styles to it
 * @param id The id of the html element
 * @param width The width of the map
 * @param height The height of the map
 */
function prepareMap(id, width, height)
{
    var map = document.getElementById(id);
    map.style.width = width + 'px';
    map.style.height = height + 'px';
}