//<![CDATA[
var map;
var mapdiv;
var gcoder;
var mgr;
var config;
var data;
var initScript;
var ICONPOOL = {};
ICONPOOL[0] = new GIcon();
ICONPOOL[0].image = "/images/GoogleMap/danmark_knappenaal.png";
ICONPOOL[0].iconAnchor = new GPoint(31, 28);
ICONPOOL[0].infoWindowAnchor = new GPoint(0, 0);
ICONPOOL[0].iconSize = new GSize(31, 28);
ICONPOOL[1] = new GIcon();
ICONPOOL[1].image = "/images/GoogleMap/udland_knappenaal.png";
ICONPOOL[1].iconAnchor = new GPoint(25, 33);
ICONPOOL[1].infoWindowAnchor = new GPoint(0, 0);
ICONPOOL[1].iconSize = new GSize(25, 33);
ICONPOOL[2] = new GIcon();
ICONPOOL[2].image = "/images/GoogleMap/kursus_knappenaal_blaa.png";
ICONPOOL[2].iconAnchor = new GPoint(37, 16);
ICONPOOL[2].infoWindowAnchor = new GPoint(0, 0);
ICONPOOL[2].iconSize = new GSize(37, 16);

function load() {

    if (GBrowserIsCompatible()) {
        mapdiv = document.getElementById("map");

        // Exit if mapdiv is null
        if (!mapdiv) {
            return;
        }

        map = new GMap2(mapdiv);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());

        map.enableDoubleClickZoom();

        gcoder = new GClientGeocoder();
        GDownloadUrl(config, function (vdata, responseCode) {
            var fail = '';

            if (responseCode == 200) {
                var xml = GXml.parse(vdata);
                if (xml) {
                    config = xml.documentElement.getElementsByTagName('config')[0];
                    data = xml.documentElement.getElementsByTagName('data')[0];
                    initScript = xml.documentElement.getElementsByTagName('initscript')[0];
                    fail = GmapInit();
                } else {
                    fail = 'error on xml';
                }
            } else {
                fail = 'error on get configuration';
            }
            if ((fail) && (fail != ''))
                alert(fail);
        }
                );
    }
}

function Initialize() {
    if (initScript.firstChild) {
        if (initScript.firstChild.data) {
            eval(initScript.firstChild.data);
        }
    }
}

function nada() {
}

function GmapInit() {

    //size part
    var size = config.getElementsByTagName('size')[0];
    var width = size.getElementsByTagName('width')[0].firstChild.data;
    var height = size.getElementsByTagName('height')[0].firstChild.data;
    setMapSize(width, height);

    //map center
    var center = config.getElementsByTagName('center')[0];
    center = center.getElementsByTagName('poi')[0];

    var gcenter = new gAddress(center, true);
    var zoom = config.getElementsByTagName('zoom')[0].firstChild.data;
    zoom = parseInt(zoom);
    findCenter(gcenter, zoom);
    setTimeout(GmapPutData, 500);
    if (initScript)
        setTimeout(Initialize, 500);

}

function GmapPutData() {
    var pois = data.getElementsByTagName('poi');
    for (var i = 0; i < pois.length; i++) {
        var poi = new gAddress(pois[i], false);



        updateMarker(poi, null);
    }
}

function centerIt(ll, zoom) {
    map.setCenter(ll, zoom);
}

function findCenter(address, zoom) {
    var ll = null;

    if ((address.lat) && (address.lng)) {
        ll = new GLatLng(address.lat, address.lng);
        centerIt(ll, zoom);

    } else {
        var where = address.city + ',' + address.country;
        gcoder.getLatLng(where, function (point) {
            if (point) {
                centerIt(point, zoom);
            } else {
                gcoder.getLatLng(address.country, function (point) {
                    if (point)
                        centerIt(point, zoom);
                });
            }
        }
       );

    }
}

function updateMarker(address, marker) {
    var ll = null;

    if ((address.lat) && (address.lng)) {
        ll = new GLatLng(address.lat, address.lng);

        uMarker(ll, marker, address.info, address.icontype);
    } else {
        var where = address.city + ',' + address.country;
        gcoder.getLatLng(where, function (point) {
            if (point) {
                uMarker(point, marker, address.info);
            } else {
                gcoder.getLatLng(address.country, function (point) {
                    if (point)
                        uMarker(point, marker, address.info);
                });
            }
        }
       );

    }
}

function uMarker(ll, marker, address, iconnr) {
    if (!marker) {
        //mgr = new GMarkerManager(map);





        marker = new GMarker(ll, { icon: ICONPOOL[iconnr], draggable: false });
        map.addOverlay(marker);
        GEvent.addListener(marker, 'click', function () {
            if (address)
                marker.openInfoWindowHtml(address);
        }
        );
        //mgr.addMarker(marker);
        //mgr.refresh();
    } else {
        marker.setPoint(ll);
        GEvent.addListener(marker, 'click', function () {
            marker.openInfoWindowHtml(address);
        }
        );
    }
}

function setMapSize(width, height) {
    mapdiv.style.width = width + 'px';
    mapdiv.style.height = height + 'px';
    map.checkResize();
}

function gAddress(xnode, isCenter) {

    if (!isCenter && xnode.getElementsByTagName('icontype')[0].firstChild)
        this.icontype = xnode.getElementsByTagName('icontype')[0].firstChild.data;
    else
        this.icontype = '';

    if (xnode.getElementsByTagName('city')[0].firstChild)
        this.city = xnode.getElementsByTagName('city')[0].firstChild.data;
    else
        this.city = '';


    if (xnode.getElementsByTagName('info')[0]) {
        if (xnode.getElementsByTagName('info')[0].firstChild)
            this.info = xnode.getElementsByTagName('info')[0].firstChild.data;
        else
            this.info = '';
    } else {
        this.info = '';
    }

    if (xnode.getElementsByTagName('country')[0].firstChild)
        this.country = xnode.getElementsByTagName('country')[0].firstChild.data;
    else
        this.country = '';
    var gpoint = xnode.getElementsByTagName('gpoint')[0];
    if (gpoint.getElementsByTagName('lat')[0].firstChild)
        this.lat = parseFloat(gpoint.getElementsByTagName('lat')[0].firstChild.data);
    else
        this.lat = null;
    if (gpoint.getElementsByTagName('lng')[0].firstChild)
        this.lng = parseFloat(gpoint.getElementsByTagName('lng')[0].firstChild.data);
    else
        this.lng = null;
}
//]]>
