var map;
var geoXml=[];
var contour= 0;
var contour_kml;
var geoXml2=[];
var gmarkers = [];
var gicons = [];
var categ;
var icone;

iconBase = new GIcon();
iconBase.iconSize=new GSize(32,32);
iconBase.shadowSize=new GSize(56,32);
iconBase.iconAnchor=new GPoint(16,32);
iconBase.infoWindowAnchor=new GPoint(16,0);
var icone_ville = new GIcon(iconBase, "http://www.carte-france.info/img/l-mairie.png", null, "http://maps.google.com/mapfiles/ms/micons/pushpin_shadow.png");
var icone_depart = new GIcon(iconBase, "http://www.carte-france.info/img/l-borne-dep.png", null, "http://maps.google.com/mapfiles/ms/micons/pushpin_shadow.png");
var icone_region = new GIcon(iconBase, "http://www.carte-france.info/img/l-borne-reg.png", null, "http://maps.google.com/mapfiles/ms/micons/pushpin_shadow.png");
var icone_pays = new GIcon(iconBase, "http://www.carte-france.info/img/drapeau.png", null, "http://maps.google.com/mapfiles/ms/micons/pushpin_shadow.png");
var icone_kml   = new GIcon(iconBase, "http://www.carte-france.info/img/pointe.png", null, "http://maps.google.com/mapfiles/ms/micons/pushpin_shadow.png");

function ferme() {
document.getElementById('cartemoins').style.display='none';
}
function agrandir() {
document.getElementById('map').style.height='600px';
document.getElementById('carteplus').style.display='none';
document.getElementById('cartemoins').style.display='block';
document.getElementById('carte').style.top='675px';
document.getElementById('ombre-bas').style.top='565px';
}
function retrecir() {
document.getElementById('map').style.height='450px';
document.getElementById('carteplus').style.display='block';
document.getElementById('cartemoins').style.display='none';
document.getElementById('carte').style.top='525px';
document.getElementById('ombre-bas').style.top='415px';
}
function affiche_contour(geoXml) {
if (contour == 1) {
 map.removeOverlay(geoXml);
	contour = 0;
 } else {
	map.addOverlay(geoXml);
 contour = 1;
	}
}
function decoche_kml(id) {
  map.closeInfoWindow();
  for (var i=0; i<50; i++) { //-- masquage des kml chargés
   if (geoXml2[i]!=undefined) {
    if (id!=i) { //-- on esquive l'id du kml demandé pour traitement plus tard (bascule affiche/masque)
      geoXml2[i].visible=false;
      geoXml2[i].hide();
      document.getElementById("check"+i).checked=false;
    }
   }
  }
}

function chargeKML(url,id,autocentre) {
  decoche_kml(id);
  if (document.forms['formul']) masque_points("azertyui"); //-- pour décocher les point et les masquer
  if (geoXml2[id]!=undefined) {
   if (geoXml2[id].visible==true) {
    geoXml2[id].hide();
    geoXml2[id].visible=false;
    document.getElementById("check"+id).checked=false;
   } else {
    geoXml2[id].show();
    geoXml2[id].visible=true;
    document.getElementById("check"+id).checked=true;
   }
   if (autocentre==true) {
       geoXml2[id].gotoDefaultViewport(map);
       map.zoomOut();
   }

  } else {
     document.getElementById("chargement").style.display="block";
     var a= new GGeoXml(url);
     map.addOverlay(a);
     document.getElementById("check"+id).checked=true;
     GEvent.addListener(a, "load", function() {
     	 if (autocentre==true) {
          a.gotoDefaultViewport(map);
          map.zoomOut();
       }
       a.visible=true;
       geoXml2[id]=a;
       document.getElementById("chargement").style.display="none";
     });
  }
   if (autocentre!=true) {
     recentre();
   }
}

function geoxml_simple(url,id,autocentre) {
  if (geoXml[id]!=undefined) {
   if (geoXml[id].visible==true) {
    geoXml[id].hide();
    geoXml[id].visible=false;
   } else {
    geoXml[id].show();
    geoXml[id].visible=true;
   }
   if (autocentre==true) {
       geoXml[id].gotoDefaultViewport(map);
   }
  } else {
     document.getElementById("chargement").style.display="block";
     var a= new GGeoXml(url);
     map.addOverlay(a);
     GEvent.addListener(a, "load", function() {
     	 if (autocentre==true) {
          a.gotoDefaultViewport(map);
       }
       a.visible=true;
       geoXml[id]=a;
       document.getElementById("chargement").style.display="none";
     });
  }
}
function ZoomControl() {}
	ZoomControl.prototype = new GControl();
	ZoomControl.prototype.initialize = function(map) {
	var container = document.createElement("div");
	var zoomInDiv = document.createElement("div");
	this.setButtonStyle_(zoomInDiv);
	container.appendChild(zoomInDiv);
	zoomInDiv.appendChild(document.createTextNode("Zoom In"));
	zoomInDiv.innerHTML = '<img src="http://www.carte-france.info/img/plus.png" title="Zoomer">';
	GEvent.addDomListener(zoomInDiv, "click", function() {
	map.zoomIn();
	});
	var zoomOutDiv = document.createElement("div");
	this.setButtonStyle_(zoomOutDiv);
	container.appendChild(zoomOutDiv);
	zoomOutDiv.innerHTML = '<img src="http://www.carte-france.info/img/moins.png" title="Dézoomer">';
	GEvent.addDomListener(zoomOutDiv, "click", function() {
	map.zoomOut();
	});
	map.getContainer().appendChild(container);
	return container;
	}
	ZoomControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(40,110));// left/top
	}
	ZoomControl.prototype.setButtonStyle_ = function(button) {
	button.style.cursor = "pointer";
}
function PanUpControl() {}
PanUpControl.prototype = new GControl();
PanUpControl.prototype.initialize = function(map) {
		var container = document.createElement("div");
		var panUp = document.createElement("div");
		this.setButtonStyle_(panUp);
		container.appendChild(panUp);
		panUp.innerHTML = '<img src="http://www.carte-france.info/img/haut.png" title="Décaler vers le haut">';
		GEvent.addDomListener(panUp, "click", function() {
		map.panDirection(0,1);
		});
		map.getContainer().appendChild(container);
		return container;
}
PanUpControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(40,15));// right/top
}
PanUpControl.prototype.setButtonStyle_ = function(button) {
	button.style.cursor = "pointer";
}
function PanDownControl() {}
PanDownControl.prototype = new GControl();
PanDownControl.prototype.initialize = function(map) {
		var container = document.createElement("div");
		var panDown = document.createElement("div");
		this.setButtonStyle_(panDown);
		container.appendChild(panDown);
		panDown.innerHTML = '<img src="http://www.carte-france.info/img/bas.png" title="Décaler vers le bas">';
		GEvent.addDomListener(panDown, "click", function() {
				map.panDirection(0,-1);
		});
		map.getContainer().appendChild(container);
		return container;
}
PanDownControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(40,73));// right/bottom
}
PanDownControl.prototype.setButtonStyle_ = function(button) {
		button.style.cursor = "pointer";
}
function PanRightControl() {}
PanRightControl.prototype = new GControl();
PanRightControl.prototype.initialize = function(map) {
		var container = document.createElement("div");
		var panRight = document.createElement("div");
		this.setButtonStyle_(panRight);
		container.appendChild(panRight);
		panRight.innerHTML = '<img src="http://www.carte-france.info/img/droite.png" title="Décaler à doite">';
		GEvent.addDomListener(panRight, "click", function() {
				map.panDirection(-1,0);
		});
		map.getContainer().appendChild(container);
		return container;
}
PanRightControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(15, 45));
}
PanRightControl.prototype.setButtonStyle_ = function(button) {
		button.style.cursor = "pointer";
}
function PanLeftControl() {}
PanLeftControl.prototype = new GControl();
PanLeftControl.prototype.initialize = function(map) {
		var container = document.createElement("div");
		var panLeft = document.createElement("div");
		this.setButtonStyle_(panLeft);
		container.appendChild(panLeft);
		panLeft.innerHTML = '<img src="http://www.carte-france.info/img/gauche.png" title="Décaler à gauche">';
		GEvent.addDomListener(panLeft, "click", function() {
				map.panDirection(1,0);
		});
		map.getContainer().appendChild(container);
		return container;
}
PanLeftControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(53, 45));
}
PanLeftControl.prototype.setButtonStyle_ = function(button) {
		button.style.cursor = "pointer";
}
function CustomMapControl() {}
CustomMapControl.prototype = new GControl();
CustomMapControl.prototype.initialize = function(map) {
		var container = document.createElement("div");
		var CustomMapPlan = document.createElement("div");
		this.setButtonStyle_(CustomMapPlan);
		container.appendChild(CustomMapPlan);
		CustomMapPlan.innerHTML = '<img src="http://www.carte-france.info/img/plan.png" title="Carte routière">';
		GEvent.addDomListener(CustomMapPlan, "click", function() {
				map.setMapType(G_NORMAL_MAP);
		});

		var CustomMapHybrid = document.createElement("div");
		this.setButtonStyle_(CustomMapHybrid);
		container.appendChild(CustomMapHybrid);
		CustomMapHybrid.innerHTML = '<img src="http://www.carte-france.info/img/mixte.png" title="Vue mixte routière / satellite">';
		GEvent.addDomListener(CustomMapHybrid, "click", function() {
				map.setMapType(G_HYBRID_MAP);
		});


		var CustomMapSatellite = document.createElement("div");
		this.setButtonStyle_(CustomMapSatellite);
		container.appendChild(CustomMapSatellite);
		CustomMapSatellite.innerHTML = '<img src="http://www.carte-france.info/img/sat.png" title="Carte satellite">';
		GEvent.addDomListener(CustomMapSatellite, "click", function() {
				map.setMapType(G_SATELLITE_MAP);
		});

		var CustomMapRelief = document.createElement("div");
		this.setButtonStyle_(CustomMapRelief);
		container.appendChild(CustomMapRelief);
		CustomMapRelief.innerHTML = '<img src="http://www.carte-france.info/img/relief.png" title="Carte du relief">';
		GEvent.addDomListener(CustomMapRelief, "click", function() {
				map.setMapType(G_PHYSICAL_MAP);
		});

		map.getContainer().appendChild(container);
		return container;
}

CustomMapControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,180));
}
CustomMapControl.prototype.setButtonStyle_ = function(button) {
		button.style.marginBottom = "8px";
		button.style.cursor = "pointer";
}

function createMarker(point, texte, icon, ouvre_auto) {
	var marker = new GMarker(point,icon);
	map.addOverlay(marker);
	GEvent.addListener(marker, "mouseover", function() {
			marker.openInfoWindowHtml(texte);
	});
   if (ouvre_auto==true) {
       marker.openInfoWindowHtml(texte);
   }
}

function createMarker2(point,name,html,category,icone) {
  var marker = new GMarker(point,icone);
  marker.icone = icone;
  marker.point = point;
  marker.mycategory = category;
  marker.id = name;
  marker.html = html;
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
  gmarkers.push(marker);
  gmarkers[category]=category;
  return marker;
}

function createMarkerHome(point,name,html,category,icone) {
  var marker = new GMarker(point,icone);
  marker.icone = icone;
  marker.point = point;
  marker.mycategory = category;
  marker.id = name;
  marker.html = html;
  GEvent.addListener(marker, "click", function() {
    affiche_lieu(name);
  });
  gmarkers.push(marker);
  gmarkers[category]=category;
  return marker;
}

function show(category) {
	if (gmarkers[category]!=undefined) {
		for (var i=0; i<gmarkers.length; i++) {
			if (gmarkers[i].mycategory == category) {
					gmarkers[i].show();
			}
		}
	} else {
		aff(category);
	}
}

function localiser(num_point,zoom) {
		for (var i=0; i<gmarkers.length; i++) {
			if (gmarkers[i].id == num_point) {
					gmarkers[i].openInfoWindowHtml(gmarkers[i].html);
					if (zoom) {
             map.setZoom(zoom);
             map.panTo(gmarkers[i].point);
          }
			}
		}
}


function hide(category) {
  map.closeInfoWindow();
  for (var i=0; i<gmarkers.length; i++) {
    if (gmarkers[i].mycategory == category) {
      gmarkers[i].hide();
    }
  }
}
function checkAllInput(form, action,exception) {
 var i = document.forms[form].getElementsByTagName("input");
  for ( var cpt = 0; cpt < i.length; cpt++) {
    if (i[cpt].id!=exception) i[cpt].checked =action;
  }
}

function masque_points(check_box) {
  //-- on cache tout les points
  map.closeInfoWindow();
  for (var i=0; i<gmarkers.length; i++) {
      gmarkers[i].hide();
  }
//-- on décoche toutes les cases sauf la cases concernée (la bascule est traitée par qui())
checkAllInput("formul",false,check_box);
}

function qui(check_box,leurre) {
  decoche_kml('9999999'); //-- pour décocher les kml et les masquer
  masque_points(check_box); //-- pour décocher les point et les masquer

	if (leurre) {
		if (document.getElementById(check_box).checked) {
				document.getElementById(check_box).checked = false;
		} else {
				document.getElementById(check_box).checked = true;
		}
	}

  if (document.getElementById(check_box).checked) {
//     document.getElementById("div_"+check_box).style.display="block";
     show(check_box);
  } else {
//     document.getElementById("div_"+check_box).style.display="none";
     hide(check_box);
  }
  recentre();
}

function ajoutControle(maCarte) {
	map.addControl(new ZoomControl());
	map.addControl(new PanUpControl());
	map.addControl(new PanDownControl());
	map.addControl(new PanRightControl());
	map.addControl(new PanLeftControl());
	map.addControl(new CustomMapControl());
	map.addControl(new GScaleControl());
	map.setMapType(G_NORMAL_MAP);
	if (document.getElementById('uri_map')) {
  	GEvent.addListener(map, "moveend", maj_form_enre);
	}
}

function init_carte() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById('map'));
    ajoutControle(map);
	} else {
		alert('Désolé, mais votre navigateur n\'est pas compatible avec Google Maps');
	}
}
function maj_form_enre() {
         document.getElementById('uri_map').value=document.getElementById('site_carte').value+'?lat='+map.getCenter().lat()+'&lng='+map.getCenter().lng()+'&zoom='+map.getZoom();
}

function ouvre_info(type_info) {//-- ouvre la fenêtre d'info
  if (type_info=='imp_carte') {
     url ='http://www.carte-france.info/information.php?type=i';
     w=800;
     h=700;
  }
  var Left=window.screen.width/2-(w/2);
  var Top=50;
  var Configuration="'toolbar=no, menubar=no, location=no, directories=no, status=no, resizeable=yes, width=" + w + ", height=" + h + ", left=" + Left + ", top=" + Top;
  window.open(url,'',Configuration);
}
ferme();
initialize();