﻿// Goolgle map js fil
var map;
var resultControlId;

//Opret googl map med navigationskontroller
function initMap(id)
{
    map = new GMap2(document.getElementById(id));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    return map;
}

//Hent kort og sæt centrum til midtsjælland.
function load(id) {
    if (GBrowserIsCompatible() && document.getElementById(id) != null) {
      map = initMap(id);
      
      map.setCenter(new GLatLng(55.43514037752737, 12.141292691230774), 9);
    }
}

//Browserne vil gerne cache siderne. Derfor oprettes unikke adresser.
function uniqueUrl(url)
{
    var date = new Date();
    var ticks = date.getTime();
    return url + "&foo"+ ticks;
}

//Opret ikon til markør
function getIcon(color)
{
    var icon = new GIcon();
    icon.image = "http://www.cellmap.dk/icons/googlemap/mm_20_" + color + ".png";
    icon.shadow = "http://www.cellmap.dk/icons/googlemap/mm_20_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);
    
    return icon;
}

function showAddress(map,geocoder,searchaddress,icon,sadresse, spostnr, sby1, syear) {
    geocoder.getLatLng(
        searchaddress,
        function(point) {
            if (point != null) 
            {
                var marker = new GMarker(point, icon);
                GEvent.addListener(marker, "click", function() 
                {
                    marker.openInfoWindowHtml("<b>" + sadresse + "</b><br>" + spostnr + " " + sby1 + "<br><br><i>Udført i " + syear + "</i>"  );
                });
    
                map.addOverlay(marker);
            }
        }
    );
}

function createMarker(point, message) {
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
        map.openInfoWindowHtml(point, message);
    }); 
    return marker; 
}

function showRefXML(id,urlxml) 
{
    if (GBrowserIsCompatible()) 
    {
        map = initMap(id);
        map.setCenter(new GLatLng(55.43514037752737, 12.141292691230774), 9);

        GDownloadUrl(urlxml, function(data) {
            var geocoder = new GClientGeocoder();
            var xml = GXml.parse(data);
            var markers = xml.documentElement.getElementsByTagName("ref");
            for (var i = 0; i < markers.length; i++) {
                var adresse = markers[i].getAttribute("adresse");
                var postnr = markers[i].getAttribute("postnr");
                var by1 = markers[i].getAttribute("by");
                var year = markers[i].getAttribute("year");
                var lat = markers[i].getAttribute("lat");
                var lng = markers[i].getAttribute("lng");

                if (lat != null && lat != "" && lng != null && lng != "") {
                    var point = new GLatLng(lat, lng);
                    var message = "<b>" + adresse + "</b><br>" + postnr + " " + by1 + "<br><br><i>Udført i " + year + "</i>";

                    map.addOverlay(createMarker(point,message));
                }
            }
        });
        
    }
}

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map and updates position for later saving
function addAddressToMap(response) {
    
    if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
    } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);

        savePosition(point);
        updatePosition(point);       
    }
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation(id) {
    //Findes mapkontrollen?
    if (document.getElementById(id) !== null) 
    {
        map = initMap(id);
        map.setCenter(new GLatLng(55.43514037752737, 12.141292691230774), 9);

        //Compute search address
        var adresss = document.getElementsByName("ctl00$mainContent$DetailsView1$ctl01").item(0).value;
        var zip = document.getElementsByName("ctl00$mainContent$DetailsView1$ctl02").item(0).value;
        var city = document.getElementsByName("ctl00$mainContent$DetailsView1$ctl03").item(0).value;
        var searchAdress = adresss + ", " + zip + "," + city + ", Danmark";

        var geocoder = new GClientGeocoder();
        geocoder.getLocations(searchAdress, addAddressToMap);
    }
}

function showCurrentLocation(id) {
    //Findes mapkontrollen?
    if (document.getElementById(id) !== null) {
        map = initMap(id);
        map.setCenter(new GLatLng(55.43514037752737, 12.141292691230774), 9);

        //Get location
        var resultLatitude = document.getElementById('ctl00_mainContent_DetailsView1_LatitudeTextBox');
        var resultLongitude = document.getElementById('ctl00_mainContent_DetailsView1_LongitudeTextBox');

        //Do we have a location?
        if (resultLatitude != null && resultLatitude != null && resultLatitude.value != "" && resultLongitude.value != "") {
            var point = new GLatLng(resultLatitude.value, resultLongitude.value);

            updatePosition(point);
        }
    }
}

function updatePosition(point) {
    map.clearOverlays();

    //Create marker
    marker = new GMarker(point, { draggable: true });
    map.addOverlay(marker);
    map.setCenter(point, 15);

    GEvent.addListener(marker, "dragend", function() {
        savePosition(marker.getLatLng());
    });
}

function savePosition(point) {
    //Save new position
    var resultLatitude = document.getElementById('ctl00_mainContent_DetailsView1_LatitudeTextBox');
    var resultLongitude = document.getElementById('ctl00_mainContent_DetailsView1_LongitudeTextBox');
    resultLatitude.value = point.lat();
    resultLongitude.value = point.lng();                           
}