// JavaScript Document
//-------------------------------------------------------------------------------------------------
//		Définition des fonctions
//-------------------------------------------------------------------------------------------------
//***********************************************************************************************
//		Fonctions de l'espace publique
//***********************************************************************************************
//----------Variables globales

var map;
var geocode;
var markers=new Array();
var infoWindow=new Array();
var root;
var pano;
var zoom_origine;

//----------Création de la carte
function cdcgeoloc_setMap(origine,zoom0,dir,id_localisation,mapOptions,id_type) {
	root=dir;
	zoom_origine=zoom0;
 	map = new GMap2(document.getElementById("cdcgeoloc_map"));
	//controles
	if (mapOptions.zoomscale==1) map.addControl(new GLargeMapControl ());
	else map.addControl(new GSmallZoomControl());
	if (mapOptions.zoomdblclc==1) map.enableDoubleClickZoom();
	else map.disableDoubleClickZoom();
	if (mapOptions.zoomroll==1) map.enableScrollWheelZoom();
	else map.disableScrollWheelZoom();
	map.addControl(new GMapTypeControl());
	//geocodage
	geocode=new GClientGeocoder();
	geocode.getLatLng(origine,function(point){map.setCenter(point,zoom0);});
	if (!id_localisation) {
		if (id_type>0) cdcgeoloc_allMarker(id_type);
	}
	else {
		pano=cdcgeoloc_localisation[id_localisation]['id_type'];
		GEvent.addListener(map,'load',function() {
			cdcgeoloc_setMarker(id_localisation,1,1);
		});
	}
	
}

//----------Placer un marker
function cdcgeoloc_setMarker(id_localisation,withinfo,center) {
	//Marker déjà sur la carte
	if (markers[id_localisation]!=undefined) {
		markers[id_localisation].show();
		if (center==1) {map.setCenter(point,15);}
		if (withinfo==1) cdcgeoloc_createInfoWindow(id_localisation);
		return;
	}
	//Options du marker
	var Icon = new GIcon(G_DEFAULT_ICON);
	Icon.image = 'IMG/cdcgeoloc_type'+cdcgeoloc_localisation[id_localisation]['id_type']+'.png';
	Icon.iconSize = new GSize(30, 30);
	Icon.shadowSize = new GSize(30, 30);
	Icon.iconAnchor = new GPoint(1, 30);
	Icon.infoWindowAnchor = new GPoint(10, 1);
	markerOptions = { icon:Icon,title:cdcgeoloc_localisation[id_localisation]['nom'] };
	//Création du marker (LATLNG)
	if (cdcgeoloc_localisation[id_localisation]['latlng']!='0') {
		co=cdcgeoloc_localisation[id_localisation]['latlng'].split(',');
		point=new GLatLng(co[0],co[1]);
		markers[id_localisation]=new GMarker(point,markerOptions);	
		map.addOverlay(markers[id_localisation]);
		cdcgeoloc_addEvents(id_localisation);
		if (center==1) {map.setCenter(point,15);}
		if (withinfo==1) cdcgeoloc_createInfoWindow(id_localisation);
	}
	//Création du marker (Par adresse)
	else {
		geocode.getLatLng(cdcgeoloc_localisation[id_localisation]['adresse'],function(point) {									  	
			if (point==null) return;
			markers[id_localisation]=new GMarker(point,markerOptions);	
			map.addOverlay(markers[id_localisation]);
			cdcgeoloc_addEvents(id_localisation);
			if (center==1) {map.setCenter(point,15)}
			if (withinfo==1) cdcgeoloc_createInfoWindow(id_localisation);
		});
	}
}

//----------Affectation des evenements à un marker
function cdcgeoloc_addEvents(id_localisation) {
	GEvent.addListener(markers[id_localisation], "click", function() {
		cdcgeoloc_createInfoWindow(id_localisation);
		map.panTo(markers[id_localisation].getLatLng());
		map.setZoom(15);
	});
	GEvent.addListener(markers[id_localisation], "infowindowopen", function() {
		$("#cdcgeoloc-infoWindow-"+id_localisation).load("spip.php?page=cdcgeoloc_squelette_infoWindow&id_localisation="+id_localisation);
	});
}

//----------Créer la fenetre d'info
function cdcgeoloc_createInfoWindow(id_localisation) {
	$.ajax({
		method:'GET',
		url:"spip.php?page=cdcgeoloc_squelette_infoWindowMax&id_localisation="+id_localisation,
		success:function (msg) {
			windowOptions={maxContent:msg};
			infoWindow[id_localisation]=markers[id_localisation].openInfoWindowHtml(
				'<div class="cdcgeoloc-window" id="cdcgeoloc-infoWindow-'+id_localisation+'">Patientez...</div>',
				windowOptions
			);
		}
	});
}

//----------effacer les markers
function cdcgeoloc_deleteMarker() {
	if (markers!=null && markers!="")
	{
		for (mark in markers) {
			if(mark!="contains")
			{
				markers[mark].hide();
				markers[mark].closeInfoWindow();
			}
		}
	}
}


//----------Afficher un marker
function cdcgeoloc_oneMarker(id_localisation) {
	cdcgeoloc_deleteMarker();
	cdcgeoloc_setMarker(id_localisation,1,1);
}


//----------Voir tout les localisation de même type
function cdcgeoloc_allMarker(id_type) {
	cdcgeoloc_deleteMarker();
	openPan(id_type);
	tab=new Array();
	for (id_localisation in cdcgeoloc_localisation) {
		if (cdcgeoloc_localisation[id_localisation]['id_type']==id_type){
			cdcgeoloc_setMarker(id_localisation,0,0);
		}
	}
	map.setZoom(zoom_origine);
}



//----------Gestion des panneaux
function openPan(id) {
	if (document.getElementById('geoloc-detail-'+pano)) {
		document.getElementById('geoloc-detail-'+pano).style.display='none';
		document.getElementById('cdcgeoloc_open-'+pano).innerHTML='+';
	}
	if (pano!=id) {
		document.getElementById('geoloc-detail-'+id).style.display='block';
		document.getElementById('cdcgeoloc_open-'+id).innerHTML='-';
		pano=id;
	}
	else {
		document.getElementById('geoloc-detail-'+id).style.display='none';
		document.getElementById('cdcgeoloc_open-'+id).innerHTML='+';
		pano=0;
	}
	
}

//***********************************************************************************************
//		Fonctions de l'espace privée
//***********************************************************************************************
var cdcgeoloc_prive_mode=''
var cdcgeoloc_marker;
var cdcgeoloc_current_center;
//----------Création de la carte
function cdcgeoloc_prive_setMap(latlng,zoom,adr_sos) {
 	map = new GMap2(document.getElementById("cdcgeoloc_map"));
	map.clearOverlays();
	map.addControl(new GMapTypeControl());
	map.addControl(new GSmallZoomControl());
	geocode=new GClientGeocoder();
	if (latlng!='') {
		cdcgeoloc_prive_center(latlng,12);
		cdcgeoloc_prive_createMarker(latlng);
	}
	else {
		geocode.getLatLng(adr_sos,function(point){
			map.setCenter(point,12);
		});
	}
}
//----------Centrer la carte sur point courant
function cdcgeoloc_prive_center(latlng,zoom) {
	if (document.cdcgeoloc_googlemap.latlng.value==null) co=latlng.split(',');
	co=document.cdcgeoloc_googlemap.latlng.value.split(',');
	point=new GLatLng(co[0],co[1]);
	map.setCenter(point,zoom);
}

//----------Recherche d'adresse
function cdcgeoloc_prive_search() {
	adresse=document.cdcgeoloc_googlemap.adresse.value;
	geocode.getLatLng(adresse,function(point){
		if (point) {
			map.setCenter(point);
			cdcgeoloc_current_center=point;
			document.cdcgeoloc_googlemap.latlng.value=point.toUrlValue();
			cdcgeoloc_prive_setMarker();
		}
		else {
			return;
		}
	});
}
//----------Le marker courant
function cdcgeoloc_prive_createMarker(latlng) {
	if (document.cdcgeoloc_googlemap.latlng.value==null) co=latlng.split(',');
	co=document.cdcgeoloc_googlemap.latlng.value.split(',');
	point=new GLatLng(co[0],co[1]);
	markerOptions={draggable:true};
	cdcgeoloc_marker=new GMarker(point,markerOptions);
	map.addOverlay(cdcgeoloc_marker);
	//Evenements du marker
	GEvent.addListener(cdcgeoloc_marker, "drag", function(new_point) {
		document.cdcgeoloc_googlemap.latlng.value=new_point.toUrlValue();
	});
}
function cdcgeoloc_prive_setMarker(latlng) {
	if (document.cdcgeoloc_googlemap.latlng.value==null) co=latlng.split(',');
	co=document.cdcgeoloc_googlemap.latlng.value.split(',');
	point=new GLatLng(co[0],co[1]);
	cdcgeoloc_marker.setLatLng(point);
}
//----------Suppression d'un enregistreemnt
function cdcgeoloc_prive_supprimer() {
	if (document.cdcgeoloc_googlemap.id_localisation.value==null) return;
	if (window.confirm('Supprimer ce lieu ?')) {
		document.cdcgeoloc_googlemap.elements['__supprimer__'].value=1;
		document.cdcgeoloc_googlemap.submit();
	}
}




