var map = null;
var geocoder = null;
var icon = null;

function createMarker(point, texte, typeIcone, vars) {
  switch (typeIcone) {
  	case 'user'   : var marker = new GMarker(point,icon); break;
	case 'sortie' : var marker = new GMarker(point,iconS); break;
	case 'resto'  : var marker = new GMarker(point,iconR); break;
  	case 'groupe' : var marker = new GMarker(point,iconG); break;
  }
  //var marker = new GMarker(point,icon);
  GEvent.addListener(marker, "click", function() {
  	if (typeIcone=='groupe'){
		/*if (vars.substr(0,7)=='http://') window.open(vars);
		else if (vars) document.location='http://' + vars;*/
		ajax('lstGroupes'+vars,document.getElementById('lstGroupes'),1);
	}
	else {
	    document.getElementById('users').src='./tabUsers.php?'+vars;
	    document.getElementById('frmUsers').style.visibility='visible';
	}
    clic=true;
  });
  GEvent.addListener(marker, "mouseover", function() {
    marker.openInfoWindowHtml(texte+"<BR><BR><FONT size=1><I>"+txtD+"</I></FONT>");
  });
  GEvent.addListener(marker, "mouseout", function() {
    map.closeInfoWindow();
  });
  return marker;
}

function setHP(adresse,texte,vars) {
	geocoder.getLatLng(
		adresse,
		function(point) {
			if (point) {
				marker=createMarker(point,texte,'user',vars);
				map.addOverlay(marker);
			}
		}
	);
}

function setSortie(adresse,texte,vars) {
	geocoder.getLatLng(
		adresse,
		function(point) {
			if (point) {
				marker=createMarker(point,texte,'sortie',vars);
				map.addOverlay(marker);
			}
		}
	);
}

function setRestaurant(adresse,texte,vars) {
	geocoder.getLatLng(
		adresse,
		function(point) {
			if (point) {
				marker=createMarker(point,texte,'resto',vars);
				map.addOverlay(marker);
			}
		}
	);
}

function setGroupe(adresse,texte,vars) {
	geocoder.getLatLng(
		adresse,
		function(point) {
			if (point) {
				marker=createMarker(point,texte,'groupe',vars);
				map.addOverlay(marker);
			}
		}
	);
}

function getAddress(overlay, latlng) {
	if (latlng != null) {
		address = latlng;
		//geocoder.getLocations(latlng, showAddress);
		geocoder.getLocations(latlng, function(response) {
			if (response && response.Status.code == 200) {
				//document.getElementById("addVisite").style.display="";
				place = response.Placemark[0];
				if (place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName) {
					point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
					marker = new GMarker(point);
					map.addOverlay(marker);
					marker.openInfoWindowHtml(
						'<FORM action="profils.php"><INPUT type=hidden name=action value=addVisite>' +
						'<LABEL class="top" for="pays">Test</LABEL>' +
						'<INPUT type=text name=pays value="' + place.AddressDetails.Country.CountryNameCode + '">' +
						'<INPUT type=text name=ville value="' + place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName + '"><BR>' +
						'<INPUT type=submit value=OK></FORM>'
					);
				}
			}
		});
	}
}

function showAddress(response) {
	alert('ok');
	map.clearOverlays();
	if (response && response.Status.code == 200) {
	} else {
		document.getElementById("addVisite").style.display="";
		/*marker.openInfoWindowHtml(
			'<FORM action="profils.php"><INPUT type=hidden name=action value=addVisite>' +
			'<INPUT type=text name=ville value="' + place.AddressDetails.Country.CountryNameCode + '">' +
			'<INPUT type=text name=ville value="' + place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName + '"><BR>' +
			'<INPUT type=submit value=OK></FORM>'
		);
		map.addOverlay(marker);*/
	}
}

function load() {
  if (GBrowserIsCompatible()) {
  	map=new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(37.4419, 0), 2);

    map.addControl(new GLargeMapControl(),
				   new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 40)));
    map.addControl(new GMapTypeControl(),
				   new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 10)));
    map.addControl(new GScaleControl());
    map.addControl(new GOverviewMapControl());
    map.enableDoubleClickZoom();
	GEvent.addListener(map, "mouseover", function() {
		map.closeInfoWindow();
	});

    mgr = new GMarkerManager(map);

	icon.image = "/images/utils/pSmile.gif";
	icon.iconSize = new GSize(15, 15);
	icon.iconAnchor = new GPoint(15 >> 1, 15 >> 1);
	icon.infoWindowAnchor = new GPoint(9, 2);
	icon.infoShadowAnchor = new GPoint(18, 25);

	for (i=0;i<total;i++) {
	  lstU=tabUsers[i][2]; listeHP="";
	  for (j=0;j<lstU.length;j++) {
	  	id	  	= lstU[j][0];
	  	pseudo	= lstU[j][1];
	  	age 	= lstU[j][2];
	  	enreg	= lstU[j][3];
	  	listeHP+="<B>" + pseudo + "</B> (<I>" + age + " ans</I>)<BR>";
	  }
      adresse=tabUsers[i][0] + ", " + tabUsers[i][1];
      vars="ville="+tabUsers[i][0]+"&pays="+tabUsers[i][1];
      setHP(adresse,"<h3>" + tabUsers[i][0] + "</h3><P>" + listeHP + "</P>",vars);
	}
  }
}

function loadGroupes() {
  if (GBrowserIsCompatible()) {
  	map=new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(44.930962,4.88912), 5);

    map.addControl(new GLargeMapControl(),
				   new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 40)));
    map.addControl(new GMapTypeControl(),
				   new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 10)));
    map.addControl(new GScaleControl());
    map.addControl(new GOverviewMapControl());
    map.enableDoubleClickZoom();
	GEvent.addListener(map, "mouseover", function() {
		map.closeInfoWindow();
	});

    mgr = new GMarkerManager(map);

	iconG.image = "/images/iconesMap/group.png";
	iconG.iconSize = new GSize(30, 30);
	iconG.iconAnchor = new GPoint(15 >> 1, 15 >> 1);
	iconG.infoWindowAnchor = new GPoint(9, 2);
	iconG.infoShadowAnchor = new GPoint(18, 25);

	for (i=0;i<total;i++) {
      adresse=tabGroupes[i][2] + "," + tabGroupes[i][3];
      vars=tabGroupes[i][0];
      setGroupe(adresse,"<h3>" + tabGroupes[i][1] + "</h3>",vars);
	}		
  }
}	

function initializeTravelMap(autorise) {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 7);
    geocoder = new GClientGeocoder();
	//GEvent.addListener(map,"singlerightclick",getAddress);
  }
}

function showVisite(address,color,id,autorise) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          //alert(address + " not found");
        } else {
          map.setCenter(point, 3);
		  icon.image = "/images/utils/flag_" + color + ".gif";
		  icon.iconSize = new GSize(15, 15);
		  icon.iconAnchor = new GPoint(15 >> 1, 15 >> 1);
		  icon.infoWindowAnchor = new GPoint(9, 2);
		  icon.infoShadowAnchor = new GPoint(18, 25);
          var marker = new GMarker(point,icon);
		  GEvent.addListener(marker, "mouseover", function() {
		    marker.openInfoWindowHtml(address);
		  });
		  GEvent.addListener(marker, "mouseout", function() {
		    map.closeInfoWindow();
		  });
		  if (autorise) {
			  GEvent.addListener(marker, "click", function() {
			    if (confirm("Voulez-vous vraiment supprimer cette destination ?"))
					alert ("ok !");
			  });
		  }
		  map.addOverlay(marker);
        }
      }
    );
  }
}


//Fonctions finales !
function initializeMap(div,centre,zoom) {
  if (GBrowserIsCompatible()) {
	setTimeout(function() {
        map = new GMap2(document.getElementById(div));
	}, 100);					
    geocoder = new GClientGeocoder();
	geocoder.getLatLng(centre, function (coord) {
	    if (coord!=null) map.setCenter(coord, 15);
	});
  }
}

function setPoint(adresse,texte,icone,clic,vars) {
	geocoder.getLatLng(
		adresse,
		function(point) {
			if (point) {
				var icon = new GIcon();
				icon.image = "/images/iconesMap/" + icone;
				icon.iconSize = new GSize(25, 25);
				icon.iconAnchor = new GPoint(25 >> 1, 25 >> 1);
				icon.infoWindowAnchor = new GPoint(9, 2);
				icon.infoShadowAnchor = new GPoint(18, 25);
				var marker = new GMarker(point,icon);

				GEvent.addListener(marker, "click", clic);
				if (texte){
					GEvent.addListener(marker, "mouseover", function() {
					marker.openInfoWindowHtml(texte+"<BR><BR><FONT size=1><I>"+txtD+"</I></FONT>");
					});

					GEvent.addListener(marker, "mouseout", function() {
					map.closeInfoWindow();
					});
				}

				setTimeout(function() {
				      map.addOverlay(marker);
				}, 300);
			}
		}
	);
}
