function initMap(data) {
    window.onload = function () {
        if (GBrowserIsCompatible()) {  
			changeContent(2);
            var xml = GXml.parse(data);
            var markers = xml.documentElement.getElementsByTagName("OU");
            var map = new GMap2(document.getElementById("map"));
            //Set map center and zoom (1024 by 768 pixels)
            map.setCenter(new GLatLng(56.35, 11.228027), 6);
            //Set map center and zoom (1280 by 1024 pixels)
            //map.setCenter(new GLatLng(55.991474,11.568604), 7);
            //map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 10)));
            //map.addControl(new GOverviewMapControl(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10, 10)));
            for (var i = 0; i < markers.length; i++) {
                var point = new GLatLng(parseFloat(markers[i].getAttribute("Latitude")), parseFloat(markers[i].getAttribute("Longitude")));
                var icon = new GIcon();
                switch (markers[i].getAttribute('AD_PATH').substring(0, 8)) {
                    case '/HJK': icon.image = "http://specmod.hjv.dk/HJV/images/googlepin_green.png"; break;
                    case '/HJK/HHV': icon.image = "http://specmod.hjv.dk/HJV/images/googlepin_green.png"; break;
                    case '/HJK/MHV': icon.image = "http://specmod.hjv.dk/HJV/images/googlepin_blue.png"; break;
                    case '/HJK/FHV': icon.image = "http://specmod.hjv.dk/HJV/images/googlepin_lblue.png"; break;
                    case '/HJK/VHV': icon.image = "http://specmod.hjv.dk/HJV/images/googlepin_red.png"; break;
                }
                icon.shadow = "http://specmod.hjv.dk/HJV/images/googlepin_shadow.png";
                icon.iconSize = new GSize(12, 20);
                icon.shadowSize = new GSize(22, 20);
                icon.iconAnchor = new GPoint(6, 20);
                icon.infoWindowAnchor = new GPoint(5, 1);
                var marker = new GMarker(point, { title: markers[i].getAttribute('Name') + markers[i].getAttribute('Address1') + markers[i].getAttribute('PostalCode'), icon: icon });
                addPopup(marker, markers[i].getAttribute('Guid'), markers[i].getAttribute('Name'));
                //Add marker to map
                map.addOverlay(marker);
            }
            //Enables double click to zoom in and out (disabled by default). (Since ver. 2.58)
            map.enableDoubleClickZoom();
            //Add map to web page
            map.addControl(new GLargeMapControl());            
        }
    }
}

function addPopup(marker, OuGuid, OuName) {
    var onClick = function () {
        var onDownloadComplete = function (data) {
            var oeInfo = GXml.parse(data).documentElement;
            var contactUrl = oeInfo.getAttribute('ContactUrl').substring(0, 7) == "http://" ? oeInfo.getAttribute('ContactUrl') :
                "http://" + oeInfo.getAttribute('ContactUrl');
            var infoDiv = "<div style='width:290px'><h3>" + OuName + "</h3><br />";
            infoDiv += oeInfo.getAttribute('ImageUrl') == "" ? "" : "<img align='right' src='" + oeInfo.getAttribute('ImageUrl') + "'>";
            infoDiv += oeInfo.getAttribute('Description').replace(/\\n/g, "<br />") + "<br /><br />";
            infoDiv += "<a href='http://www.hjv.dk/" + oeInfo.getAttribute('DefaultShortUrlName') + "' target='_blank'>Gå til vores lokale sider</a>";
            infoDiv += "</div>";
            var contactDiv = "<div style='width:290px'><h3>" + OuName + "</h3><br />";
            contactDiv += oeInfo.getAttribute('Address1') == "" ? "" : oeInfo.getAttribute('Address1');
            contactDiv += oeInfo.getAttribute('Address1') == "" || oeInfo.getAttribute('PostalCode') == "" ? "" : ", ";
            contactDiv += oeInfo.getAttribute('PostalCode') == "" ? "<br />" : oeInfo.getAttribute('PostalCode') + "<br />";
            contactDiv += oeInfo.getAttribute('Address2') == "" ? "" : oeInfo.getAttribute('Address2') + "<br />";
            contactDiv += oeInfo.getAttribute('Address1') == "" && oeInfo.getAttribute('PostalCode') == "" &&
                oeInfo.getAttribute('Address2') == "" ? "" : "<br />";
            contactDiv += oeInfo.getAttribute('InfoFullName') == "" && oeInfo.getAttribute('InfoEmail') == "" &&
                oeInfo.getAttribute('InfoPhone') == "" ? "" : "<b>Vedr: Generel information</b><br />";
            contactDiv += oeInfo.getAttribute('InfoFullName') == "" ? "" : oeInfo.getAttribute('InfoFullName') + "<br />";
            contactDiv += oeInfo.getAttribute('InfoEmail') == "" ? "" : "<a href='mailto:" + oeInfo.getAttribute('InfoEmail') + "'>" + oeInfo.getAttribute('InfoEmail') + "</a>";
            contactDiv += oeInfo.getAttribute('InfoEmail') == "" || oeInfo.getAttribute('InfoPhone') == "" ? "" : ", ";
            contactDiv += oeInfo.getAttribute('InfoPhone') == "" ? "" : oeInfo.getAttribute('InfoPhone') + "<br />";
            contactDiv += oeInfo.getAttribute('InfoPhone') != "" ? "" : "<br />";
            contactDiv += oeInfo.getAttribute('InfoFullName') == "" && oeInfo.getAttribute('InfoEmail') == "" &&
                oeInfo.getAttribute('InfoPhone') == "" ? "" : "<br />";
            contactDiv += oeInfo.getAttribute('ApplicationFullName') == "" && oeInfo.getAttribute('ApplicationEmail') == "" &&
                oeInfo.getAttribute('ApplicationPhone') == "" ? "" : "<b>Ønsker at blive kontaktet om optagelse</b><br />";
            contactDiv += oeInfo.getAttribute('ApplicationFullName') == "" ? "" : oeInfo.getAttribute('ApplicationFullName') + "<br />";
            contactDiv += oeInfo.getAttribute('ApplicationEmail') == "" ? "" : "<a href='mailto:" + oeInfo.getAttribute('ApplicationEmail') + "'>" + oeInfo.getAttribute('ApplicationEmail') + "</a>";
            contactDiv += oeInfo.getAttribute('ApplicationEmail') == "" || oeInfo.getAttribute('ApplicationPhone') == "" ? "" : ", ";
            contactDiv += oeInfo.getAttribute('ApplicationPhone') == "" ? "" : oeInfo.getAttribute('ApplicationPhone');
            contactDiv += "<br /><br />";
            contactDiv += oeInfo.getAttribute('ContactUrl') == "" ? "<a href='http://www.hjv.dk/" + oeInfo.getAttribute('DefaultShortUrlName') + "' target='_blank'>Gå til vores lokale sider</a>" :
                "<a href=\"" + contactUrl + "\" target='_blank'>Gå til vores lokale sider</a>";
            //Open popup
            marker.openInfoWindowTabsHtml([new GInfoWindowTab("Info", infoDiv), new GInfoWindowTab("Kontakt", contactDiv)]);
        }
        GDownloadUrl("http://www.blivmedlem.nu/mapproxy.asp?OuGuid=" + escape(OuGuid), onDownloadComplete);
    }
    //Add function which will be called and open a popup when marker is clicked
    GEvent.addListener(marker, "click", onClick);
}
