﻿// VARIABLES
var map;            // Map object
var points;         // Map points to be displayed

var c_separator = ";#";

// ---------------------------------------------------------------------------
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}
addLoadEvent(loadMapElement);

function loadMapElement() {
    // Get the mapStyle value (a, r, h, o)
    var mapStyleShort = document.getElementById("mapStyle").value.substring(0, 1).toLowerCase();

    // Initialize the map
    InitMap("ve_copronord_map", document.getElementById("mapCenterLat").value
                            , document.getElementById("mapCenterLng").value
                            , document.getElementById("mapZoom").value
                            , mapStyleShort);
}

// ---------------------------------------------------------------------------

// Initialize and load the map with options and data
function InitMap(id, lat, lng, zoom, style, height, width) {

    // Set a new size for the control, if specified
    if (height != null)
        document.getElementById(id).style.height = height;
    if (width != null)
        document.getElementById(id).style.width = width;

    // New instance of the map
    map = new VEMap(id);

    // Map post-Settings
    map.SetDashboardSize(VEDashboardSize.Small);


    if (document.getElementById("mapErrorMessage").value == "") {
        // Load the map, centered on the specified coordinates, if not specified : default zoom = 4
        if (zoom == null)
            map.LoadMap(new VELatLong(lat, lng), 4);
        else
            map.LoadMap(new VELatLong(lat, lng), zoom, style);

        // Map pre-Settings
        map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);

        if (document.getElementById("mapDashboardVisible").value != "True")
            map.HideDashboard();

        if (document.getElementById("mapScaleBarVisible").value != "True")
            map.HideScalebar();


        //Add elements from hidden fields and set the corresponding zoom
        map.Clear();
    }

    points = [];
    AddShapeFromHiddenField("mapPoints", true);

    // Avoid UI bug in IE
    document.getElementById(id).style.overflow = "visible";
    document.getElementById(id).style.overflow = "hidden";
}

// ---------------------------------------------------------------------------

function AddShapeFromHiddenField(idField, setMapView) {
    if (document.getElementById(idField).value != null) {
        //Get the informations
        var tabPointsInfo = document.getElementById(idField).value.split("|");

        // Get if we use a large number of pin on the map
        var useSmallPin = "";
        if (tabPointsInfo.length >= 50)
            useSmallPin = "s-";

        for (var i = 0; i < tabPointsInfo.length - 1; i++) {
            //Add the corresponding pin from the extracted informations
            AddPin(tabPointsInfo[i].split(c_separator)[0].replace(",","."),     //latitude
                   tabPointsInfo[i].split(c_separator)[1].replace(",", "."),     //longitude
                   tabPointsInfo[i].split(c_separator)[2],     //title
                   tabPointsInfo[i].split(c_separator)[3],     //description
                   useSmallPin + tabPointsInfo[i].split(c_separator)[4]);    //icon with small icon option
        }

        if (setMapView) {
            //Set the view on every shape
            if (points.length >= 2)
                map.SetMapView(points);
            else
                map.SetCenterAndZoom(points[0], 4);
        }
    }
    else {
        document.getElementById("ve_copronord_map").innerHTML = document.getElementById("mapErrorMessage").value;
    }
}

// ---------------------------------------------------------------------------

function AddPin(lat, lng, title, description, icon) {
    //Create the shape position and store
    var shapePos = new VELatLong(lat, lng);
    points.push(shapePos);

    //Create the shape
    var shapeTemp = new VEShape(VEShapeType.Pushpin, shapePos);

    // Set information
    if (title != "")
        shapeTemp.SetTitle(title);

    if (description != "")
        shapeTemp.SetDescription(description);

    //Initialize the shape's properties
    var pathPin = document.getElementById("mapIconUrl").value;
    if (icon == "")
        if (pathPin == "")
        shapeTemp.SetCustomIcon("/_layouts/Copronord.WebParts.GeoLocWebPart/Images/pin-agencsse.png");
    else
        shapeTemp.SetCustomIcon(pathPin);
    else
        shapeTemp.SetCustomIcon(icon);

    //Add the shape on the base layer map
    map.AddShape(shapeTemp);
}
