﻿/* variables globales */
var map;
var sw;
var ne;
var bounds;

var larg;  // largeur calculé au lancement
var haut;  // hauteur calculé au lancement

lock = true;

var isClick = false; // click sur un produit

// fonction d'initialisation
function GMap_init() {

    //On adapte la taille du div de la carte à la taille de la fenetre (ou de l'iframe)
    if (document.getElementById('map')) {
        larg = (document.getElementById('map').parentNode.clientWidth);
        haut = (document.getElementById('map').parentNode.clientHeight);
    }
    else 
    {
        larg = (window.innerWidth);
        haut = (window.innerHeight);
    }
    if (larg == 0) larg = haut = 200;
    document.getElementById('map').style.height = haut + "px";
    document.getElementById('map').style.width = larg + "px";
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        sw = new GLatLng(latitude, longitude);
        ne = new GLatLng(latitude, longitude);
        bounds = new GLatLngBounds(sw, ne);
        //zoomLvl = map.getBoundsZoomLevel(bounds);
        centre = bounds.getCenter();
        //if (1 == 1) zoomLvl = 13;

        map.setCenter(centre, zoomLvl);
        
        //map.maxResolution = maxGMapZoom;
        //map.minResolution = minGMapZoom;

        if (cfgMin == 1) {
            map.addControl(new GOverviewMapControl());
        }
        if (cfgEch == 1) {
            map.addControl(new GScaleControl());
        }
        map.addControl(new PSMBoutonControl());
        map.addControl(new ZOOMBoutonControl());

        if (navigationType == 3) {
            var mapType = G_PHYSICAL_MAP;
            document.getElementById("navRelief").className = "navSelected";
        } else if (navigationType == 2) {
            var mapType = G_HYBRID_MAP;
            document.getElementById("navMixte").className = "navSelected";
        } else if (navigationType == 1) {
            var mapType = G_SATELLITE_MAP;
            document.getElementById("navSatellite").className = "navSelected";
        } else {
            var mapType = G_NORMAL_MAP;
            document.getElementById("navPlan").className = "navSelected";
        }
        map.setMapType(mapType);   //var mapType = G_NORMAL_MAP;  // G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP

        // désactive les options de zoom auto
        //map.disableDoubleClickZoom();
        //map.disableScrollWheelZoom();

        map.enableContinuousZoom();
        map.enableScrollWheelZoom();

        //map.enableDoubleClickZoom();

        GEvent.addListener(map, "infowindowclose", function() {
            document.getElementById('fond_container_controls').style.display = 'block';
            document.getElementById('container_controls').style.display = 'block';

            if (treeSlide.open == false && markLockMenu == false) {
                treeSlide.show();
                navigationSlide.show();
                produitsSlide.show();
            }
            isClick = false;
            //map.panTo(centre, zoomLvl);
        });

        GEvent.addListener(map, "infowindowopen", function() {
            isClick = true;
            document.getElementById('fond_container_controls').style.display = 'none';
            document.getElementById('container_controls').style.display = 'none';

            if (treeSlide.open == true) {
                treeSlide.hide();
                navigationSlide.hide();
                produitsSlide.hide();
            }
        });

        // Pour les events de scroll, gestion du min/max
        GEvent.addListener(map, "zoomend", function(oldZ, newZ) {
            if (oldZ > newZ) {
                if (newZ <= minGMapZoom) {
                    map.setZoom(minGMapZoom);
                }
            } else {
                if (newZ >= maxGMapZoom) {
                    map.setZoom(maxGMapZoom);
                }
            }
            slider.set(map.getZoom() - minGMapZoom);
        });


        // pour les clicks
        cercle = DrawCircle(new GPoint(0, 0), 0, 2);
        GEvent.addListener(map, "click", function(overlay, pointm) {
        });
        GEvent.addListener(map, "dblclick", function(overlay, pointm) {
        });



    }

    // Ajout de l'icone par defaut
    icon["i0"] = new GIcon();
    icon["i0"].image = iconeDir + "marker.png";
    icon["i0"].shadow = "http://www.google.com/mapfiles/shadow50.png";
    icon["i0"].iconSize = new GSize(20, 34);
    icon["i0"].shadowSize = new GSize(37, 34);
    //icon["i0"].iconAnchor = new GPoint(9, 34);
    icon["i0"].iconAnchor = new GPoint(9, 9);
    icon["i0"].infoWindowAnchor = new GPoint(9, 2);
    icon["i0"].infoShadowAnchor = new GPoint(18, 25);

    addDefaultIcone();
    
    // fonction d'ajout des points par défaut
    addDefaultPoint();

    // Initialisation du menu
    mootree_init();

    // Masque les menu / navigation / prduits si besoin
    if (cfgMen != 1) {
        document.getElementById("MenuLayer").style.display = "none";
    }
    if (cfgNav != 1) {
        document.getElementById("NavigationLayer").style.display = "none";
    }
    if (cfgPro != 1) {
        document.getElementById("ProduitLayer").style.display = "none";
    }

    // trie de la liste des produits affichés   
    productSort();
}

/* Variables pour la minimap */
var map_moving = 0;
var minimap_moving = 0;
var minimap;
var xhair;

/* Crée une map et la synchronise avec la map principale */
function miniMap_init() {

    // create the crosshair icon, which will indicate where we are on the minimap
    // Lets not bother with a shadow
    var Icon = new GIcon();
    Icon.image = "xhair.png";
    Icon.iconSize = new GSize(21, 21);
    Icon.shadowSize = new GSize(0, 0);
    Icon.iconAnchor = new GPoint(11, 11);
    Icon.infoWindowAnchor = new GPoint(11, 11);
    Icon.infoShadowAnchor = new GPoint(11, 11);

    // Create the minimap
    minimap = new GMap2(document.getElementById("navigationMiniMap"));
    minimap.setCenter(new GLatLng(latitude, longitude), minGMapZoom);

    // Add the crosshair marker at the centre of teh minimap and keep a reference to it

    xhair = new GMarker(minimap.getCenter(), Icon);
    minimap.addOverlay(xhair);

    minimap.disableDoubleClickZoom();
    minimap.disableScrollWheelZoom(); 

    // ====== Handle the Map movements ======

    // Variables that log whether we are currently causing the maps to be moved
    map_moving = 0;
    minimap_moving = 0;

    function Move() {
        minimap_moving = true;
        if (map_moving == false) {
            minimap.setCenter(map.getCenter());
            xhair.setPoint(map.getCenter());
            xhair.redraw(true);
        }
        minimap_moving = false;
    }

    function MMove() {
        map_moving = true;
        if (minimap_moving == false) {
            map.setCenter(minimap.getCenter());
            xhair.setPoint(minimap.getCenter());
            xhair.redraw(true);
        }
        map_moving = false;
    }

    // Listen for when the user moves either map
    GEvent.addListener(map, 'move', Move);
    GEvent.addListener(minimap, 'moveend', MMove);
    

}