﻿
var gmap_geocoder = undefined;
var gmap_markers = new Array();

function gMapInit() {
    googleMap = new GMap2(document.getElementById('map'));
    googleMap.enableContinuousZoom();
    $(document.body).unload(function() { GUnload(); });
}

function gMapInitialize(navigateControl, lat, lng, isCentral) {

    googleMap = new GMap2(document.getElementById('map'));
    googleMap.enableContinuousZoom();
    $(document.body).unload(function() { GUnload(); });
    
    if (navigateControl) {
        googleMap.addControl(new GLargeMapControl3D());
    }

    if (lat == null && lng == null) {
        gmap_geocoder = new GClientGeocoder();
        gmap_geocoder.getLatLng('Sweden', function(point) {
            if (point) {
                googleMap.setCenter(point, 5);
            }
        });
    }
    else {
        var point = new GLatLng(lat, lng, false);
        addMapMarker(point, false, isCentral);
        googleMap.zoomToMarkers();
    }
}

function gMapGotoPoint(lat, lng) {
    var point = new GLatLng(lat, lng, false);
    if (point) {
        googleMap.setCenter(point, 5);
    }
}

function gMapGotoSweden() {

    if (googleMap.isLoaded && googleMap.getZoom() != 5) {
        gmap_geocoder.getLatLng('Sweden', function(point) {
            if (point) {
                googleMap.setCenter(point, 4);
                googleMap.zoomIn(point, true, true);
            }
        });
    }
}

function addMapMarker(point, infoHtml, isCentral) {

    var iconUrl = isCentral ? '/images/SAPEvent/mapMarker_centralt.png' : '/images/SAPEvent/mapMarker_normal.png';
    var cIcon = new GIcon();
    cIcon.image = iconUrl;
    cIcon.iconSize = new GSize(39, 35);
    cIcon.iconAnchor = new GPoint(11, 32);
    cIcon.infoWindowAnchor = new GPoint(17, 2); 


    var marker = new GMarker(point, { icon: cIcon, draggable: false });
    googleMap.addOverlay(marker);
    gmap_markers.push(marker);

    if (infoHtml) {
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(infoHtml, { maxWidth: 255 });
        });
    }
}

function removeMapMarkers() {
    while (gmap_markers.length > 0) {
        googleMap.removeOverlay(gmap_markers.pop());
    }
}

// Zoom to markers from http://www.pixeldevelopment.com/
GMap2.prototype.zoomToMarkers = function(slopPercentage, heightOffsetPct) {
    var count = 0;
    var thePoint, x, y, minX, maxX, minY, maxY, span;

    for (var i = 0; i < gmap_markers.length; i++) {
        thePoint = gmap_markers[i];
        x = thePoint.getPoint().y; y = thePoint.getPoint().x;
        if (i == 0) {
            minX = x; maxX = x; minY = y; maxY = y;
        }
        else {
            if (x < minX) minX = x;
            if (x > maxX) maxX = x;
            if (y < minY) minY = y;
            if (y > maxY) maxY = y;
        }
    }
    if (gmap_markers.length == 1)
        this.setCenter(new GLatLng(x, y), 14);
    else if (gmap_markers.length > 1) {
        var center = new GLatLng((parseFloat(minX) + parseFloat(maxX)) / 2, (parseFloat(minY) + parseFloat(maxY)) / 2);

        span = new GSize(Math.abs(maxX - minX), Math.abs(maxY - minY));
        slopWid = 0;
        slopHgt = 0;
        if (typeof slopPercentage != "undefined") {
            slopWid = span.width * slopPercentage / 200;
            slopHgt = span.height * slopPercentage / 200;
            span.width *= 1 + slopPercentage / 100;
            span.height *= 1 + slopPercentage / 100;
        }
        deltaHgt = 0;
        if (typeof heightOffsetPct != "undefined") {
            deltaHgt = span.height * heightOffsetPct / 100;
            center = new GLatLng(center.lat() + deltaHgt, center.lng());
        }
        // needs slop
        var bounds = new GLatLngBounds(new GLatLng(minX - slopHgt, minY - slopWid), new GLatLng(maxX + slopHgt, maxY + slopWid)); // sw, ne


        var zoom = this.getBoundsZoomLevel(bounds);
        this.setCenter(center, zoom);
    }
}
