﻿// VARIABLES
var map;                // Map object
var vepoints;           // Map points to be displayed
var velayerItems;       // Layer that will contain every items
var vepointsAgencies;   // Map points for agencies
var velayerAgencies;    // Layer that will contain every agency


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(field_mapStyle).value.substring(0, 1).toLowerCase();

    // Initialize the map
    InitMap("ve_habitat_search_map", document.getElementById(field_mapCenterLat).value
                                    , document.getElementById(field_mapCenterLng).value
                                    , document.getElementById(field_mapZoom).value
                                    , mapStyleShort);
}

// ---------------------------------------------------------------------------
// Gestion du Zoom : Si zoom maximum on dégroupe les points, sinon, on les regroupe!
function EndZoomHandler(e) {

    if (velayerItems != null) {
        var z = map.GetZoomLevel();
        if (z > 18)
            velayerItems.SetClusteringConfiguration(VEClusteringType.None);
        else
            velayerItems.SetClusteringConfiguration(VEClusteringType.Grid);
    }
}
// ---------------------------------------------------------------------------
// 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);

    // Eric.G - 02/11/2009 : Abonnement à l'événement OnEndZoom
    map.AttachEvent("onendzoom", EndZoomHandler);    

    // Map post-Settings
    map.SetDashboardSize(VEDashboardSize.Small);


    if (document.getElementById(field_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(field_mapDashboardVisible).value != "True")
            map.HideDashboard();

        if (document.getElementById(field_mapScalebarVisible).value != "True")
            map.HideScalebar();

        //Add elements from hidden fields and set the corresponding zoom
        map.Clear();
    }

    vepoints = [];
    AddShapeFromHiddenField(field_mapAgenciesPoints, true);
}

// ---------------------------------------------------------------------------

function AddShapeFromHiddenField(idField, setMapView) {
    if (document.getElementById(idField).value != "") {
        //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-";

        vepointsAgencies = [];
        // Initialize the layer that will contain agencies
        velayerAgencies = new VEShapeLayer();        
        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
                   null,
                   velayerAgencies);

            vepointsAgencies.push(new VELatLong(tabPointsInfo[i].split(c_separator)[0].replace(",", "."), 
                                                tabPointsInfo[i].split(c_separator)[1].replace(",", ".")));
        }
        map.AddShapeLayer(velayerAgencies);

        if (setMapView) {
            //Set the view on every shape
            if (vepointsAgencies.length >= 2)
                map.SetMapView(vepointsAgencies);
            else
                map.SetCenterAndZoom(vepointsAgencies[0], 4);
        }
    }
    else {
        document.getElementById("ve_habitat_map").innerHTML = document.getElementById("mapErrorMessage").value;
    }
}

// ---------------------------------------------------------------------------

function AddPin(lat, lng, title, description, icon, useSmallIcon, layer) {
    //Create the shape position and store
    var shapePos = new VELatLong(lat, lng);
    vepoints.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(field_mapIconUrl).value;
    if (icon == "")
        if (pathPin == "")
            shapeTemp.SetCustomIcon("/_layouts/Habitat.WebParts.GeoLocWebPart/Images/pin-agence.png");
        else
            shapeTemp.SetCustomIcon(pathPin);
    else
        shapeTemp.SetCustomIcon(icon);

    
    if (layer != null)
        layer.AddShape(shapeTemp);      // Add the shape on the specified layer
    else
        map.AddShape(shapeTemp);        //Add the shape on the base layer map
}
