﻿var latlng;
var zoom;
var mapTypeId;
var map;
var geocoder;

function initialize() {
    latlng = new google.maps.LatLng(56.2, 11.6);
    zoom = 6; 
    mapTypeId = google.maps.MapTypeId.ROADMAP;
    
    var options = {
        zoom: zoom,
        center: latlng,
        mapTypeId: mapTypeId,
        scaleControl: true
    };

    map = new google.maps.Map(document.getElementById("map"), options);
    geocoder = new google.maps.Geocoder();
}

function setMarker(html, address) {
    var visible = false;
        
    var infoWindow = new google.maps.InfoWindow({ content: html });

    geocoder.geocode({ address: address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
                google.maps.event.addListener(marker, 'click', function() {
                    if (!visible) {
                        visible = true;
                        infoWindow.open(map, marker);
                        map.setZoom(12);
                        map.setCenter(results[0].geometry.location);
                    } else {
                        visible = false;
                        infoWindow.close();
                        map.setZoom(zoom);
                        map.setCenter(latlng);
                    }
                });
            } else {
                window.alert("'" + address + "' ikke fundet");
            }
        }
    );
} 