labels = new Array();
numerosPlaces = new Array();
fonctionsMouseOut= new Array();
fonctionsMouseOver= new Array();
fonctionsClick= new Array();
var geocoder = new GClientGeocoder();
      
   //FONCTIONS GOOGLE MAPS
            //<![CDATA[
//==========================================            
//initialisation de la carte
//==========================================            
function load()
{

 //custom markers
  Mculture = new GIcon();
  Mculture.image = "./sitefiles/images/culture.png";
  Mculture.iconSize=new GSize(16,16);
  Mculture.iconAnchor=new GPoint(8,8);
  Mnature = new GIcon();
  Mnature.image = "./sitefiles/images/nature.png";
  Mnature.iconSize=new GSize(16,16);
  Mnature.iconAnchor=new GPoint(8,8);
  Mloisirs = new GIcon();
  Mloisirs.image = "./sitefiles/images/loisirs.png";
  Mloisirs.iconSize=new GSize(16,16);
  Mloisirs.iconAnchor=new GPoint(8,8);
  
//creation carte avec controles
  if (GBrowserIsCompatible())
  { 
    map = new GMap2(document.getElementById("gcarte"));
   if ( typeof( window[ 'start_lat' ] ) != "undefined" ) {
    map.setCenter(new GLatLng(start_lat,start_lon),14);
  }
  else
  {
    map.setCenter(new GLatLng(47,2),6);
  }
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());
    map.enableDoubleClickZoom();
    map.enableScrollWheelZoom();
    map.enableContinuousZoom();
    map.setMapType(G_SATELLITE_MAP);
   } 

//evenement de clic sur la carte
  GEvent.addListener(map, "click", function(clickedOverlay, point) 
{
closeView();
//mode record
 if ( document.forms["params"].mode.value=="record")
 {
  	if (clickedOverlay) {
  		return;
  	}

  	var marker = new GMarker(point);
  	GEvent.addListener(marker, "infowindowclose", function() {
  		map.removeOverlay(this);
  		
  		document.forms['params'].lon.value=0;
  		document.forms['params'].lat.value=0;
  	});
  	GEvent.addListener(marker, "click", function() {
  		var p = new GLatLng(this.getPoint().lat(), this.getPoint().lng());
  		var html = '<span style="font-family:Arial;">Placez moi sur le site à pr&eacute;senter<br><i>Place me on the place you want to introduce</i></span>';
  	document.forms['params'].lat.value=this.getPoint().lat();
  	document.forms['params'].lon.value=this.getPoint().lng();
  		this.openInfoWindowHtml(html);
  	});
  	map.addOverlay(marker);
	GEvent.trigger(marker, "click");	
  }
 
}    );       

//chargement des lieux
GDownloadUrl("./sitefiles/pages/getplaces.php", function(data, responseCode) 
    {  
      var xml = GXml.parse(data);
      var markers = xml.documentElement.getElementsByTagName("place");      
      for (var i = 0; i < markers.length; i++) 
      {
      
          //read xml
          var placeName= xml.documentElement.getElementsByTagName("place")[i].getElementsByTagName("nom")[0].childNodes[0].nodeValue;
          var placeId= xml.documentElement.getElementsByTagName("place")[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
          var placeType= xml.documentElement.getElementsByTagName("place")[i].getElementsByTagName("type")[0].childNodes[0].nodeValue;
        
          //le marqueur
          var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lon")));
          
          switch (placeType)
          {
            case '1':
              var Mymarker=new GMarker(point,{icon:Mnature});    
             break;
             case '2':
              var Mymarker=new GMarker(point,{icon:Mculture});    
             break;
             case '3':
              var Mymarker=new GMarker(point,{icon:Mloisirs});    
             break;
            default:
              //Instruction_par_défaut
             break;
          } 

          
       //    markers[placeId]=Mymarker;
           // Le label 
          var label = new ELabel(point, "<nobr>"+placeName+"<nobr>", "labels", new GSize(0,0),90,false );
          map.addOverlay(label); 
          numerosPlaces.push(placeId);        
          labels[placeId]=label;
          label.hide();
            map.addOverlay(Mymarker);  

      //fonction dynamique mouseover
      fonctionsMouseOver[placeId] = new Function("","if (document.params.show.value=='0'){labels["+placeId+"].show();}");
     //fonction dynamique mouseout
      fonctionsMouseOut[placeId] = new Function("","if (document.params.show.value=='0'){labels["+placeId+"].hide();}");
      //fct click
      fonctionsClick[placeId] = new Function("","showPlace("+placeId+")");
      
      //evenement de mouseover          
      GEvent.addListener(Mymarker, "mouseover",  fonctionsMouseOver[placeId] );
      //evenement de mouseout            
      GEvent.addListener(Mymarker, "mouseout",  fonctionsMouseOut[placeId] );
      //evenement de click        
      GEvent.addListener(Mymarker, "click",  fonctionsClick[placeId] );

}       
});                       
                      
  }         
  
//======================================  
//gestionnaire de clic
//======================================                    
              

//======================================
//enregistrement d'un lieu              
//======================================   
function registerNode(lat,lon) 
{
  if ((lat==00)&&(lon==0))
  {
  alert("Choisissez d'abord un endroit sur la carte!\n\nClick the map first!");
  return;
  }
  
  if ((document.forms['ajout'].nom.value=="")||(document.forms['ajout'].desc.value==""))
  {
  alert("Entrez un nom et une description!\n\nEnter a name and a description!");
  return;
  }
  
  if (document.forms['ajout'].desc.value.length<300)
  {
  alert("Description du site trop courte.\n\nPlace description is too short.\n\n(min 300 caracteres)");
  return;
  }
  
  	if (map.getZoom() < 15) {
  		alert("Verifiez la position exacte du lieu a créer avant de valider\n\nCheck the position of the place before validate");
  		map.setCenter(new GLatLng(lat, lon));
  		map.setZoom(15);
  	} else {
  	
  	//controle du formulaire :
  	if (document.forms['ajout'].nom.value.length<10)
  	{alert("Nom du site trop court.\n\nPlace name is too short.\n\n(min 10 caracteres)");return;}

  if (document.getElementById("IDcadreImage").style.visibility == "hidden")
  {
  Preview(document.forms['ajout'].desc.value);
  return;
  }
  else
  {
  	closeView();
  //	enregistrent du site en etat non validé
    document.forms['ajout'].lon.value=lon;
    document.forms['ajout'].lat.value=lat;
  	document.forms['params'].mode.value='play';
  	document.getElementById("ze_status").innerHTML = "<img src='./sitefiles/images/load1.gif'>";
    document.forms['ajout'].bton.disabled = true;
    document.forms['ajout'].bton2.disabled = true;
    document.forms['ajout'].submit();
    
    return true;
 /* 	alert (document.forms['ajout'].nom.value);
  	var data="nom="+document.forms['ajout'].nom.value+"&desc="+document.forms['ajout'].desc.value+"&lon="+document.forms['params'].lon.value+"&lat="+document.forms['params'].lat.value;
  	sendData(data, "sitefiles/pages/submitplace.php", "POST",'desc_lieu');
  	document.forms['params'].mode.value='play';*/
  }	 
	}
}

function registerComment ()
{
 if (document.forms['ajout'].addcom.value.length<30)
  {
  alert("Le texte entré est trop court!\n\nYour text is too short\n\n(min 30 car)");
  return;
  }
  if (document.getElementById("IDcadreImage").style.visibility == "hidden")
  {
  Preview(document.forms['ajout'].addcom.value);
  return;
  }
  else
  {
  	closeView();
  document.forms['ajout'].bton.disabled = true;
  document.forms['ajout'].bton2.disabled = true;
  document.forms['ajout'].submit();
  }
}

function ShowNodes()
{
closeView();

 labels[1].hide();
}

function HideNodes()
{
}

//ajout d'un lieu
function AddPlace()
{
closeView();
document.getElementById('images_lieu').innerHTML="<div style='padding : 10px;font-size:10pt;color:#990099;'> <img src='./sitefiles/images/amp.png'><br>Vous pouvez copier coller une URL elle sera transformée en lien cliquable<br><i>You can paste an url, it will be changed into a link</i></div>";
document.forms['params'].mode.value='record';
	sendData(null, "sitefiles/pages/addplaceform.php", "GET","desc_lieu");
}

//ajout d'une image
function AddPicture(id)
{
closeView();
document.getElementById('images_lieu').innerHTML="<div style='padding : 10px;font-size:10pt;color:#990099;'><img src='./sitefiles/images/amp.png'><br>Recompressez vos images si nécessaire pour obtenir une taille < 300ko<br>Check your image size and recompress it if necessary</div>";
javascript:document.forms['params'].mode.value='play';
	sendData("idplace="+id, "sitefiles/pages/addimageform.php", "POST","desc_lieu");
}

function AddComment(id)
{
closeView();
document.getElementById('images_lieu').innerHTML="<div style='padding : 10px;font-size:10pt;color:#990099;'><img src='./sitefiles/images/amp.png'><br>Vous pouvez copier coller une URL elle sera transformée en lien cliquable<br><i>You can paste an url, it will be changed into a link</i></div>";
javascript:document.forms['params'].mode.value='play';
	sendData("idplace="+id, "sitefiles/pages/addcommentform.php", "POST","desc_lieu");

}

function Contact()
{
closeView();
document.forms['params'].mode.value='play';
sendData(null, "sitefiles/pages/contact.php", "POST","desc_lieu");
sendData(null, "sitefiles/pages/cool.php", "POST","images_lieu");
}
function About()
{
closeView();
document.forms['params'].mode.value='play';
sendData(null, "sitefiles/pages/about.php", "POST","desc_lieu");
sendData(null, "sitefiles/pages/cool.php", "POST","images_lieu");

}
function Terms()
{
closeView();
document.forms['params'].mode.value='play';
sendData(null, "sitefiles/pages/terms.php", "POST","desc_lieu");
sendData(null, "sitefiles/pages/cool.php", "POST","images_lieu");

}

//chargement des infos sur le lieu
function showPlace(id)
{
closeView();
document.forms['params'].mode.value='play';
  	var data="idplace="+id;
  	sendData(data, "sitefiles/pages/getdetails.php", "POST","desc_lieu");
    sendData(data, "sitefiles/pages/getimages.php", "POST","images_lieu");
}

//chargement des infos sur le lieu
function showPlaceEN(id)
{
closeView();
document.forms['params'].mode.value='play';
  	var data="idplace="+id;
  	sendData(null, "http://www.google.com/translate?u=http%3A%2F%2Fwww.france2see.com%2Fsitefiles%2Fpages%2FgetdetailsEN.php%3Fidplace%3D2&langpair=fr%7Cen&hl=fr&ie=UTF8", "GET","desc_lieu");
    sendData(data, "sitefiles/pages/getimages.php", "POST","images_lieu");
}


//chargement des infos sur le lieu
function Preview(desc)
{
closeView();
desc=desc.replace(/&/gi,"##amp##");
desc=desc.replace(/\+/gi,"##plus##");
var data="desc="+desc;
document.getElementById("IDcadreImage").style.visibility = "visible";
document.forms['ajout'].bton2.disabled = false;
sendData(data, "sitefiles/pages/preview.php", "POST","IDcadreImage");
}

function SatView()
{
closeView();
map.setMapType(G_SATELLITE_MAP);
}

function MapView()
{
closeView();
map.setMapType(G_MAP_TYPE);
}

            //]]>
            
function zoomOUT()
{
closeView();
 map.zoomOut();  
}

function zoomIN()
{
closeView();
   map.zoomIn();
}                        
               
function zoomOn(zone)
{
closeView();
    if (zone=="France")
    {
    map.setCenter(new GLatLng(47,2),6);
    }
    else
    {
 geocoder.getLatLng(zone, function(point) 
  {
    if (!point) {
      alert(address + " not found");
    } else 
    {   
        map.setCenter(point, 9);    
        }    
    }  );
    
  }
}

function zoomByCoord(lon,lat)
{
closeView();
  map.setCenter(new GLatLng(lon,lat),14);
}

function SwitchLabels()
{
closeView();
if (document.params.show.value=="1")
{
for (var i = 0; i < numerosPlaces.length; i++)
{
labels[numerosPlaces[i]].hide();
}
document.params.show.value="0";
}
else
{
for (var i = 0; i < numerosPlaces.length; i++)
{
labels[numerosPlaces[i]].show();
}
document.params.show.value="1";
}


}

function Partenaires()
{
closeView();
document.forms['params'].mode.value='play';
sendData(null, "sitefiles/pages/partners.php", "POST","desc_lieu");
sendData(null, "sitefiles/pages/cool.php", "POST","images_lieu");
}

 function test()
  {
  mylimites=new GLatLngBounds();
  /*my p1=new GLatLng(0,0);
  my p2=new GLatLng(0,0);*/
  mylimites=map.getBounds();
  p1=mylimites.getSouthWest();
  p2=mylimites.getNorthEast();
  alert(p1.lat()+" à "+p2.lat());
  }


function GetFlickrPics(place)
{
resFlickr=AjaxGet("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=96f72684b3cd050b6840278d4203e3c1&text=Cathédrale%20Notre-Dame%20d'Amiens");
alert (resFlickr);
}
