//<![CDATA[
// Détection du navigateur
var map;
var tooltip;
//alert ('je passe');
var listEvt=new Array;
var numEvt=0;
var marker_evt=new Array;
var marker_selected=null;
var idSelected=null;
var oControl;
var Tsize;

MapInit = function() {
      
map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addMapType(G_PHYSICAL_MAP) ;
map.setCenter(new GLatLng(48.88754874941472,2.3254215717315674),5);
map.setMapType(G_PHYSICAL_MAP);

/*Tsize = new GSize(200, 200);
oControl=new GOverviewMapControl(Tsize);
map.addControl(oControl);*/



// ====== set up marker mouseover tooltip div ======
tooltip = document.createElement("div");
//document.getElementById("map").appendChild(tooltip);pour la v 2.43
map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip); //pour la v>2.44
tooltip.style.visibility="hidden";


      


for(var i=0; i<listEvt.length; i++)
{
  var point=new GLatLng(listEvt[i][0],listEvt[i][1]);
  var marker = createMarker(point,i,listEvt[i][3],icon_evt );

}


//-------------Limites administratives Géosignal
/*var tileCommunes= new GTileLayer(new GCopyrightCollection("Limites communales : Copyright GEOSIGNAL 2003"),0,9);
tileCommunes.myLayers='Communes';
tileCommunes.myFormat='image/png';
tileCommunes.myBaseURL='http://www.geosignal.org/cgi-bin/wmsmap?';
tileCommunes.getTileUrl=CustomGetTileUrl;
tileCommunes.getCopyright = function(a,b) {return {prefix:"Limites communales: ", copyrightTexts:["© GEOSIGNAL 2003"]};}

var tileDepartements= new GTileLayer(new GCopyrightCollection("Limites départementales : Copyright GEOSIGNAL 2003"),1,17);
tileDepartements.myLayers='Departements';
tileDepartements.myFormat='image/png';
tileDepartements.myBaseURL='http://www.geosignal.org/cgi-bin/wmsmap?';
tileDepartements.getTileUrl=CustomGetTileUrl;
tileDepartements.getCopyright = function(a,b) {return {prefix:"Limites départementales ", copyrightTexts:["© GEOSIGNAL 2003"]};}

var tileRegions= new GTileLayer(new GCopyrightCollection("Limites regionales : Copyright GEOSIGNAL 2003"),1,17);
tileRegions.myLayers='Regions';
tileRegions.myFormat='image/png';
tileRegions.myBaseURL='http://www.geosignal.org/cgi-bin/wmsmap?';
tileRegions.getTileUrl=CustomGetTileUrl;
tileRegions.getCopyright = function(a,b) {return {prefix:"Limites regionales: ", copyrightTexts:["© GEOSIGNAL 2003"]};}

//-------------Limites administratives Géosignal
var tileRaster1000k= new GTileLayer(new GCopyrightCollection("GEOSIGNAL 2003"),5,9);
tileRaster1000k.myLayers='RASTER1000k';
tileRaster1000k.myFormat='image/png';
tileRaster1000k.myBaseURL='http://www.geosignal.org/cgi-bin/wmsmap?';
tileRaster1000k.getTileUrl=CustomGetTileUrl;
tileRaster1000k.getCopyright = function(a,b) {return {prefix:"Carte 1.000.000: ", copyrightTexts:["© GEOSIGNAL 2003"]};}


var tileRaster500k= new GTileLayer(new GCopyrightCollection("GEOSIGNAL 2003"),10,10);
tileRaster500k.myLayers='RASTER500k';
tileRaster500k.myFormat='image/png';
tileRaster500k.myBaseURL='http://www.geosignal.org/cgi-bin/wmsmap?';
tileRaster500k.getTileUrl=CustomGetTileUrl;
tileRaster500k.getCopyright = function(a,b) {return {prefix:"Carte 500.000: ", copyrightTexts:["© GEOSIGNAL 2003"]};}

var tileRaster250k= new GTileLayer(new GCopyrightCollection("GEOSIGNAL 2003"),11,11);
tileRaster250k.myLayers='RASTER250k';
tileRaster250k.myFormat='image/png';
tileRaster250k.myBaseURL='http://www.geosignal.org/cgi-bin/wmsmap?';
tileRaster250k.getTileUrl=CustomGetTileUrl;
tileRaster250k.getCopyright = function(a,b) {return {prefix:"Carte 250.000: ", copyrightTexts:["© GEOSIGNAL 2003"]};}

var tileRaster100k= new GTileLayer(new GCopyrightCollection("GEOSIGNAL 2003"),12,12);
tileRaster100k.myLayers='RASTER100k';
tileRaster100k.myFormat='image/png';
tileRaster100k.myBaseURL='http://www.geosignal.org/cgi-bin/wmsmap?';
tileRaster100k.getTileUrl=CustomGetTileUrl;
tileRaster100k.getCopyright = function(a,b) {return {prefix:"Carte 100.000: ", copyrightTexts:["© GEOSIGNAL 2003"]};}

var tileRaster50k= new GTileLayer(new GCopyrightCollection("GEOSIGNAL 2003"),13,13);
tileRaster50k.myLayers='RASTER50k';
tileRaster50k.myFormat='image/png';
tileRaster50k.myBaseURL='http://www.geosignal.org/cgi-bin/wmsmap?';
tileRaster50k.getTileUrl=CustomGetTileUrl;
tileRaster50k.getCopyright = function(a,b) {return {prefix:"Carte 50.000: ", copyrightTexts:["© GEOSIGNAL 2003"]};}

var tileRaster25k= new GTileLayer(new GCopyrightCollection("GEOSIGNAL 2003"),14,15);
tileRaster25k.myLayers='RASTER25k';
tileRaster25k.myFormat='image/png';
tileRaster25k.myBaseURL='http://www.geosignal.org/cgi-bin/wmsmap?';
tileRaster25k.getTileUrl=CustomGetTileUrl;
tileRaster25k.getCopyright = function(a,b) {return {prefix:"Carte 25.000: ", copyrightTexts:["© GEOSIGNAL 2003"]};}

var tileRaster5k= new GTileLayer(new GCopyrightCollection("GEOSIGNAL 2003"),16,17);
tileRaster5k.myLayers='RASTER5k';
tileRaster5k.myFormat='image/png';
tileRaster5k.myBaseURL='http://www.geosignal.org/cgi-bin/wmsmap?';
tileRaster5k.getTileUrl=CustomGetTileUrl;
tileRaster5k.getCopyright = function(a,b) {return {prefix:"Carte 5.000: ", copyrightTexts:["© GEOSIGNAL 2003"]};}

var layer1000k=[tileRaster1000k];
var layer500k=[tileRaster500k];
var layer250k=[tileRaster250k];
var layer100k=[tileRaster100k];
var layer50k=[tileRaster50k];
var layer25k=[tileRaster25k];
var layer5k=[tileRaster5k];

var layerDepartements=[G_MAP_TYPE.getTileLayers()[0],tileDepartements];
var layerCommunes=[G_SATELLITE_MAP.getTileLayers()[0],tileCommunes];
var layerRegions=[G_MAP_TYPE.getTileLayers()[0],tileRegions];


var custommap1000k = new GMapType(layer1000k, G_SATELLITE_MAP.getProjection(), "Carte-Fr", {maxResolution:17, minResolution:5, errorMessage:_mMapError});
var custommap500k = new GMapType(layer500k, G_SATELLITE_MAP.getProjection(), "Carte-Fr", {maxResolution:17, minResolution:5, errorMessage:_mMapError});
var custommap250k = new GMapType(layer250k, G_SATELLITE_MAP.getProjection(), "Carte-Fr", {maxResolution:17, minResolution:5, errorMessage:_mMapError});
var custommap100k = new GMapType(layer100k, G_SATELLITE_MAP.getProjection(), "Carte-Fr", {maxResolution:17, minResolution:5, errorMessage:_mMapError});
var custommap50k = new GMapType(layer50k, G_SATELLITE_MAP.getProjection(), "Carte-Fr", {maxResolution:17, minResolution:5, errorMessage:_mMapError});
var custommap25k = new GMapType(layer25k, G_SATELLITE_MAP.getProjection(), "Carte-Fr", {maxResolution:17, minResolution:5, errorMessage:_mMapError});
var custommap5k = new GMapType(layer5k, G_SATELLITE_MAP.getProjection(), "Carte-Fr", {maxResolution:17, minResolution:5, errorMessage:_mMapError});
var custommapDepartements = new GMapType(layerDepartements, G_SATELLITE_MAP.getProjection(), "Admin-Fr", {maxResolution:17, minResolution:5, errorMessage:_mMapError});
var custommapCommunes = new GMapType(layerCommunes, G_SATELLITE_MAP.getProjection(), "Admin-Fr", {maxResolution:17, minResolution:5, errorMessage:_mMapError});
var custommapRegions = new GMapType(layerRegions, G_SATELLITE_MAP.getProjection(), "Admin-Fr", {maxResolution:17, minResolution:5, errorMessage:_mMapError});


map.addMapType(custommapDepartements);
map.addMapType(custommap1000k);
*/
map.addControl(new GMapTypeControl());



GEvent.addListener(map, 'click', function(overlay, point) {

      if (overlay) 
      {

            for(var i=0;i<marker_evt.length;++i)
            
            if (marker_evt[i]==overlay)
            {
              //on commence par remettre la couleur initiale au picto qui était sélectionné avnt
              if (idSelected!=null && idSelected!=i)
              {
                //remise en etat de l'ancien picto
                map.removeOverlay(marker_evt[idSelected]);
                var point=new GLatLng(listEvt[idSelected][0],listEvt[idSelected][1]);
                var marker=createMarker(point,idSelected,listEvt[idSelected][3], icon_evt);
                 document.getElementById(listEvt[idSelected][2]).style.backgroundColor="#ffffff";
                 
              }
              if (idSelected!=i)
              {
                //changement du nouveau
                map.removeOverlay(marker_evt[i]);
                var point=new GLatLng(listEvt[i][0],listEvt[i][1]);
                var marker=createMarker(point,i,listEvt[i][3], icon_evt_c);
                document.getElementById(listEvt[i][2]).style.backgroundColor="#D1E2F2";
                idSelected=i;
              }

              var adresse='#'+listEvt[i][2];
              window.location.href=adresse;
            }

      } 

  
  });
  
/*  
  GEvent.addListener(map, 'maptypechanged', function() {
          var currentZoom=map.getZoom();
          if (map.getCurrentMapType()==custommap1000k)
          {
          //GLog.write("custom");
              if (currentZoom>15)map.setMapType(custommap5k);
              else if (currentZoom>13)map.setMapType(custommap25k);
                      else if (currentZoom>12)map.setMapType(custommap50k);
                              else if (currentZoom>11)map.setMapType(custommap100k);
                                      else if (currentZoom>10)map.setMapType(custommap250k);
                                              else if (currentZoom>9)map.setMapType(custommap500k);
                                                      else map.setMapType(custommap1000k);

          }

          if (map.getCurrentMapType()==custommapDepartements)
          {
                  
              if (currentZoom>9)map.setMapType(custommapCommunes);
              else if (currentZoom>5) map.setMapType(custommapDepartements);
                  else map.setMapType(custommapRegions);
          }

    });
    
    
        GEvent.addListener(map, 'zoomend', function() {
        //GLog.write("zoomend");
        var currentZoom=map.getZoom();
        if (map.getCurrentMapType()==custommap1000k || map.getCurrentMapType()==custommap500k || map.getCurrentMapType()==custommap250k || map.getCurrentMapType()==custommap100k || map.getCurrentMapType()==custommap50k || map.getCurrentMapType()==custommap25k || map.getCurrentMapType()==custommap5k)
        {
          //GLog.write("custom");
              if (currentZoom>15)map.setMapType(custommap5k);
              else if (currentZoom>13)map.setMapType(custommap25k);
                      else if (currentZoom>12)map.setMapType(custommap50k);
                              else if (currentZoom>11)map.setMapType(custommap100k);
                                      else if (currentZoom>10)map.setMapType(custommap250k);
                                              else if (currentZoom>9)map.setMapType(custommap500k);
                                                      else map.setMapType(custommap1000k);
        }
        if (map.getCurrentMapType()==custommapDepartements || map.getCurrentMapType()==custommapCommunes || map.getCurrentMapType()==custommapRegions)
        {
              
              if (currentZoom>9)map.setMapType(custommapCommunes);
              else if (currentZoom>5) map.setMapType(custommapDepartements);
                  else map.setMapType(custommapRegions);
        }

        //center = map.getCenter();
        //bounds = map.getBounds();
        zoom = map.getZoom();
		
        //if (zoom>8 && map.getCurrentMapType()==G_MAP_TYPE) map.setMapType(G_HYBRID_TYPE);
  
	});
*/
        /*
        function TextualZoomControl() {
        }
        TextualZoomControl.prototype = new GControl();
        
        TextualZoomControl.prototype.initialize = function(map) {
          var container = document.createElement("div");
        
          var zoomInDiv = document.createElement("div");
          this.setButtonStyle_(zoomInDiv);
          container.appendChild(zoomInDiv);        
        
          map.getContainer().appendChild(container);
          return container;
        }
        
        TextualZoomControl.prototype.getDefaultPosition = function() {
          return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 30));
        }
        
        TextualZoomControl.prototype.setButtonStyle_ = function(button) {
          button.style.backgroundImage="url(./img/logo/logo_small_georezo.gif)";
          button.style.width = "120px";
          button.style.height = "30px";
          
        }
        map.addControl(new TextualZoomControl());*/
        
        //map.addControl(new GOverviewMapControl(new GSize(200,200)));
}

image_evt = new Image(13,22);
image_evt.src = "./img/Picto_Bleu.png";

image_evt_c = new Image(13,22);
image_evt_c.src = "./img/Picto_rouge.png";

var icon_evt = new GIcon();
icon_evt.image=image_evt.src;
icon_evt.iconSize = new GSize(13, 22);
icon_evt.iconAnchor = new GPoint(6, 22);
icon_evt.infoWindowAnchor = new GPoint(5, 1);

var icon_evt_c = new GIcon();
icon_evt_c.image=image_evt_c.src;
icon_evt_c.iconSize = new GSize(13, 22);
icon_evt_c.iconAnchor = new GPoint(6, 22);
icon_evt_c.infoWindowAnchor = new GPoint(5, 1);


//image_evt = new Image(15,14);
//image_evt.src = "./img/map/geo_submit.gif";

var icon_ev = new GIcon();
icon_ev.image=image_evt.src;
icon_ev.iconSize = new GSize(15, 15);
icon_ev.iconAnchor = new GPoint(6, 6);
icon_ev.infoWindowAnchor = new GPoint(5, 1);


function createMarker(point,markerId,html,icon) 
{
        var marker = new GMarker(point,icon);
        marker.tooltip = '<div class="tooltip">'+html+'</div>';
        marker_evt[markerId]=marker;        
        GEvent.addListener(marker,"mouseover", function() {
            showTooltip(marker);
        });        
        GEvent.addListener(marker,"mouseout", function() {
                            tooltip.style.visibility="hidden"
        });
        map.addOverlay(marker);
      
}

function plotEvt(idEvt)
{
    for(var i=0;i<listEvt.length;++i)
    {
      if(listEvt[i][2]==idEvt)
      {
        //on commence par remettre la couleur initiale au picto qui était sélectionné avnt
        if (idSelected!=null && idSelected!=i)
        {
          //remise en etat de l'ancien picto
          map.removeOverlay(marker_evt[idSelected]);
          var point=new GLatLng(listEvt[idSelected][0],listEvt[idSelected][1]);
          var marker=createMarker(point,idSelected,listEvt[idSelected][3], icon_evt);
          document.getElementById(listEvt[idSelected][2]).style.backgroundColor="#ffffff";
          
        }
        if (idSelected!=i)
        {
          //changement du nouveau
          map.removeOverlay(marker_evt[i]);
          var point=new GLatLng(listEvt[i][0],listEvt[i][1]);
          var marker=createMarker(point,i,listEvt[i][3], icon_evt_c);
          document.getElementById(listEvt[i][2]).style.backgroundColor="#D1E2F2";
          idSelected=i;
          map.setCenter(point);
        }
          //window.location.href="#map";
      }
    }
}



function showTooltip(marker) 
{

 	/*tooltip.innerHTML = marker.tooltip;
        //alert(tooltip.innerHTML);
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.point,map.getZoom());
	var anchor=marker.icon.iconAnchor;
	var width=marker.icon.iconSize.width;
	var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";*/
        
        
      	tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var height=tooltip.clientHeight;
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";
}






function popup(page) 
{
	window.open(page,'Edition','width=300,height=200,toolbar=false,scrollbars=false');	
}

//]]>
