
var map = null;
var geocoder = null;
var point = null;
function $(id){
	try{
		return document.getElementById(id);
	}catch(err){
		return null;
	}
}

function myzoom(a) {
	a > 0 ? map.zoomIn('', false, true): map.zoomOut('', true);
}

function mymove(a,b) {
	map.panDirection(a,b);
}


function affiche_marker(i) {
	map.panTo(aMarkers[i].getPoint());
	aMarkers[i].openInfoWindowHtml(aMarkersHtml[i]);
}

function wheelZoom(a) {
	if(a.cancelable){
		a.preventDefault();
	}
	(a.detail || -a.wheelDelta) < 0 ? map.zoomIn('', false, true): map.zoomOut('', true);
	return false;
}


function objConnexion(){
	//on prepare un attribut pour les arguments passé par 
	//la fonction appelante
	this.argument = null;
	
	//on definie la fonction de traitement de la réponse
	this.callBack = function() {
		//Debug(this.obj.readyState)
		if (this.obj.readyState==4){
			if (this.obj.status == 200){
				if (this.argument) {
						//rollover
								$(this.argument[1]).innerHTML = this.obj.responseText;
								if (this.argument[1] == "detailgrp")
								{
									//$(this.argument[1]).style.visibility="visible";
									var info = map.getInfoWindow();
									var point = info.getPoint();
									map.openInfoWindowHtml(point, this.obj.responseText,{autoScroll:true}); 
									
								}
								
								if( this.argument[1] == "tableauResultats" )
								{
									//regenCarto();
								}
					//try{eval(httpRequest.responseText);}catch(er){Debug(httpRequest.responseText);}
					//console.log(httpRequest.argument[2])
				}				
			}else{
				//gestion de l'echec
				alert('Problème de connexion : verifiez votre connexion au web\n erreur : '+this.obj.status+'\n'+this.obj.responseText);
			 }	
			//vidage du statut
			//statutOff();	
			$("wait").style.display = 'none';
		}
	}	
	
	try{
		this.obj = new ActiveXObject("Msxml2.XMLHTTP")
	}
	catch(e){
		try{
			this.obj = new ActiveXObject("Microsoft.XMLHTTP")
		}
		catch(oc){
			this.obj = null
		}		
	}
	if(!this.obj&&typeof XMLHttpRequest!="undefined")
		this.obj = new XMLHttpRequest()
		
	return this
}

function ouvre_panneau(comment)
{
		if (comment==1)
		{
			$('panneau').style.height = "100px";
			$('panneau').style.visibility = "visible";
		}
		else
		{
			$('panneau').style.visibility = "hidden";
		}
}

function connexion_ascdd(url,ladiv) {
	var methode = "GET";
	var asynchrone = true;
	var httpRequest = new objConnexion();
	
	httpRequest.argument = arguments

	if (httpRequest) {
    try{
	    var _this = httpRequest;
			//Appel de la fonction de callback quand l'objet XMLHTTPRequest change de status
			httpRequest.obj.onreadystatechange = function() {
				//alert(_this);
				_this.callBack();
			}
	  	httpRequest.obj.open(methode, url, asynchrone);
    	httpRequest.obj.send(null);
			
    }catch(oe){
    }
	}
}


function createMarker(point,html) {
/*var icon = new GIcon();
icon.image = "/images/punaise.gif";
icon.shadow = "";
icon.iconSize = new GSize(43, 28);
icon.shadowSize = new GSize(38, 42);
icon.iconAnchor = new GPoint(19, 37);
icon.infoWindowAnchor = new GPoint(19, 19);
*/
var marker = new GMarker(point);
/*GEvent.addListener(marker, "mouseover", function() {
  marker.openInfoWindowHtml(html);
});*/
/*GEvent.addListener(marker, "click", function() {
  marker.openInfoWindowHtml(html);
});*/
GEvent.addListener(marker, "click", function() {
	marker.openInfoWindowHtml("Chargement en cours..."+html); 
	connexion_asc('execute_list_site.php?commune_cle='+html,'detailgrp');
});

return marker;
}


function position2(x,y,cp,ville){
	
	var point = new GLatLng(y,x);
	
	var marker = createMarker(point,ville.replace("\'http://","http://"));
	
	map.addOverlay(marker);

}

function position3(x,y,cp,ville){
	
	var point = new GLatLng(y,x);
	
	var detail =  ville.split("|");
	var ch="";
	//ch="<table cellpadding='2' cellspacing='0' width='200px' style='border:solid 2px #" + detail[0] + ";background-color:white;font-family:Tahoma,Arial,Helvetica;font-size:11px; font-style:normal;font-variant:normal;font-weight:bold;'>";
	//ch.="<tr><td align=center colspan=2 style='background:#"+ detail[0] +";color:#FFFFFF;'>"+ detail[2] + "<br>"+ detail[3] +"</td></tr>";
	//ch.="<tr><td width=81px><img src='http://www.lafrancedunordausud.fr/affiche_photo.php?filename="+ detail[4] +"&tailleX=80'></td>";
	//ch.="<td></td></tr>";
	//ch.='</table>';
	
	var marker = createMarker(point,ch);
	
	map.addOverlay(marker);

}


function connexion_asc(url,contexte) {
	var methode = "GET";
	var asynchrone = true;
	var httpRequest = new objConnexion();
	
	httpRequest.argument = arguments
	
	if (httpRequest) {
    try{
	    var _this = httpRequest;
			//Appel de la fonction de callback quand l'objet XMLHTTPRequest change de status
			httpRequest.obj.onreadystatechange = function() {
				//alert(_this);
				_this.callBack();
			}
	  	httpRequest.obj.open(methode, url, asynchrone);
    	httpRequest.obj.send(null);
			
    }catch(oe){
    }
	}
}

function appelle_script(quoi,qui,onzoome)
{
	remote.location.href="execute_script.php?region_cle=" + quoi +"&station_cle="+ qui +"&onzoome="+onzoome;
}
function init_map(x,y,zoom){
	

	if (!map){
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GMapTypeControl());
		map.addControl(new GLargeMapControl());
		map.addControl(new GOverviewMapControl());
		GEvent.addDomListener(document.getElementById("map"), "DOMMouseScroll", wheelZoom);
		GEvent.addDomListener(document.getElementById("map"), "mousewheel", wheelZoom);
		map.addControl(new GScaleControl());
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
			  //map.setMapType(G_HYBRID_MAP);

		//geocoder = new GClientGeocoder();
		//showAddress(cp+ ' '+ville+', France');		
  }else{
		map.clearOverlays();
		//showAddress(cp+ ' '+ville+', France');		
	}

	var point = new GLatLng(x,y);
	 map.setCenter(point, zoom);
	 map.setMapType(G_HYBRID_MAP);

}
function zoom_map(x,y,zoom){
	var point = new GLatLng(x,y);
	 map.setCenter(point, zoom);
}
function init_map_zoom_old(letab){
	

	var bounds = new GLatLngBounds();
	data =  letab.split("||");
	for ( i in data ) 
	{
		if (data[i]!='')
		{
			detail =  data[i].split("|");
			bounds.extend(new GLatLng(detail[0],detail[1]));
		}
	}
	var lezoom = map.getBoundsZoomLevel(bounds);
	//alert(lezoom);
	if (lezoom>15)
		lezoom = 15;
		//map.setZoom(lezoom);
	var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
	var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
	
	init_map(clat,clng,lezoom);
}
function init_map_zoom(letab){
	

	if (!map){
		map = new GMap2(document.getElementById("map"));
		//map = new GMap2(document.getElementById("map"));
		//map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GLargeMapControl());
		map.addControl(new GOverviewMapControl());
		GEvent.addDomListener(document.getElementById("map"), "DOMMouseScroll", wheelZoom);
		GEvent.addDomListener(document.getElementById("map"), "mousewheel", wheelZoom);
		map.addControl(new GScaleControl());
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
			  //map.setMapType(G_HYBRID_MAP);

		//geocoder = new GClientGeocoder();
		//showAddress(cp+ ' '+ville+', France');		
  }else{
		if (letab =="")
			map.clearOverlays();
		//showAddress(cp+ ' '+ville+', France');		
	}
	map.clearOverlays();

	if (letab!="")
	{
		var bounds = new GLatLngBounds();
		data =  letab.split("||");
		for ( i in data ) 
		{
			if (data[i]!='')
			{
				detail =  data[i].split("|");
				bounds.extend(new GLatLng(detail[0],detail[1]));
			}
		}
		var lezoom = map.getBoundsZoomLevel(bounds);
		//alert(lezoom);
		if (lezoom>15)
			lezoom = 15;
			//map.setZoom(lezoom);
		var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
		var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
	
		var point = new GLatLng(clat,clng);
		 map.setCenter(point, lezoom);
	}
	 map.setMapType(G_HYBRID_MAP);
}
function changeType(a){
	if(a=='satellite'){
		map.setMapType(G_SATELLITE_TYPE);
	}

	if(a=='map'){
		map.setMapType(G_MAP_TYPE);
	}

	if(a=='hybrid'){
		map.setMapType(G_HYBRID_MAP);
	}
}
function valideprofil(qui)
{
	$("wait").style.display = 'inline';
	document.form_recherche.le_combo.value=qui;
	document.form_recherche.submit();
}
function resetmoteur()
{
	$("wait").style.display = 'inline';
	remote.location.href='moteur.php?reset=1';
}
function resetall()
{
	$("wait").style.display = 'inline';
	remote.location.href='moteur.php?reset=2';
}

function affiche_carte()
{
	$("wait").style.display = 'inline';
	remote.location.href='execute_script.php';
}
function affichedetail(lacle)
{
	$("wait").style.display = 'inline';
	connexion_asc('annuaire_departs_residences.inc.php?GRP_RES_CLE='+lacle,'detailgrp');
}
function fermedetail(comment)
{
		
			$('detailgrp').style.visibility = "hidden";
		
}
function xmlpoint()
{
	var map = new GMap2(document.getElementById("map"));
	//map.addControl(new GSmallMapControl());
	//map.addControl(new GMapTypeControl());
	var bounds = new GLatLngBounds();
	map.setCenter(new GLatLng(46.9502622421856,2.373046875), 6);
	// Download the data in data.xml and load it on the map. The format we
	// expect is:
	// <markers>
	//   <marker lat="37.441" lng="-122.141"/>
	//   <marker lat="37.322" lng="-121.213"/>
	// </markers>
	GDownloadUrl("execute_script_xml.php", function(data, responseCode) 
	{  
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("marker");
		for (var i = 0; i < markers.length; i++) 
		{    var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),  parseFloat(markers[i].getAttribute("lng")));
			map.addOverlay(new GMarker(point));  
			//bounds.extend(new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng"))));
	
		}
	});
	/*var lezoom = map.getBoundsZoomLevel(bounds);
	var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
	var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;

	var point = new GLatLng(clat,clng);
	 map.setCenter(point, lezoom);
	 map.setMapType(G_HYBRID_MAP);*/
}
function ChangePage(pas)
{
	top.connexion_asc('resultat.php?pas='+pas,'tableauResultats');
}
function affichedetail(commune_cle,lat,lng)
{
	//$("wait").style.display = 'inline';
	var point = new GLatLng(parseFloat(lat),  parseFloat(lng));
	map.openInfoWindowHtml(point, "Chargement en cours..."); 
	connexion_asc('execute_list_site.php?commune_cle='+commune_cle,'detailgrp');
}
function ChangePageDetail(commune_cle,pas)
{
	connexion_asc('execute_list_site.php?commune_cle='+ commune_cle +'&pas='+ pas,'detailgrp');
}
function ResetMap()
{
	init_map_zoom('');
	zoom_map('43.612216768175','1.461181640625',7);
	$('div_liste').innerHTML = "";
}
function Efface()
{
	map.clearOverlays();
}


/***************************************************************/
	function regenCarto()
	{
		var divIframeCarto = document.getElementById( "divIframeCarto" );
		//var divIframeGMap = document.getElementById( "divIframeGMap" );
		
		var iframeCarto = document.getElementById( "iframeCarto" );
		//var iframeGMap = document.getElementById( "iframeGMap" );
		
		/***********************************
		********** POUR GOOGLE MAP *********
		***********************************/
		/*if( ville > 0 )
		{
			divIframeCarto.style.display = "none";
			divIframeGMap.style.display = "block";
			
			iframeGMap.src = "./carto/gmap.php";
		}
		else
		{
			divIframeCarto.style.display = "block";
			divIframeGMap.style.display = "none";
			
			iframeCarto.contentWindow.document.getElementById( "carto" ).SetVariable( "regen" , "regen" );
		}*/
		
		iframeCarto.contentWindow.document.getElementById( "carto" ).SetVariable( "regen" , "regen" );
	}
    
    function resetCarto()
	{
		var divIframeCarto = document.getElementById( "divIframeCarto" );
		//var divIframeGMap = document.getElementById( "divIframeGMap" );
		
		var iframeCarto = document.getElementById( "iframeCarto" );
		
		divIframeCarto.style.display = "block";
		//divIframeGMap.style.display = "none";
		
		iframeCarto.src = "./carto/carto.php?reset=1";
	}
	
	function changeValue( id , value , valid )
	{
		var element = document.getElementById( id );
		
		if( element )
		{
			var elementOptions = element.getElementsByTagName( "option" );
			
			for( var i = 0 ; i < elementOptions.length ; i++ )
			{
				if( elementOptions[i].selected )
				{
					elementOptions[i].selected = false;
				}
				else
				{
					if( elementOptions[i].value == value )
					{
						elementOptions[i].selected = true;
					}
					else
					{
						elementOptions[i].selected = false;
					}
				}
			}
			
			if( id == "destination" )
			{
				changeValue( "commune" , -1 , false );
			}
			
			if( valid )
			{
				valideprofil( id );
			}
		}
	}
	
	function viewCommune( insee )
	{
		var iframeCarto = document.getElementById( "iframeCarto" );
		iframeCarto.contentWindow.document.getElementById( "carto" ).SetVariable( "insee" , insee );
	}
