var map;
var geocoder;
var marker;

function adrcode() {
	
    var localisation = $('#localisation').val();
    geocoder = new google.maps.Geocoder();

    geocoder.geocode( {'address': localisation}, function(results, status) {

      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);

		/*
		marker = new google.maps.Marker({
            position: results[0].geometry.location,
            draggable:true
        });
        */
        marker=new google.maps.Marker({draggable:false});

        
        marker.setMap(null);
        
        marker.position=results[0].geometry.location;
	    
		marker.setMap(map);  

		google.maps.event.addListener(marker, 'click', function() {
			window.open('http://www.google.fr/maps?q='+localisation);
		});
/*
		google.maps.event.addListener(marker, 'drag', function() {
		    geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
		      if (status == google.maps.GeocoderStatus.OK) {
        		if (results[0]) {
        		  $('#latitude').val(marker.getPosition().lat());
		          $('#longitude').val(marker.getPosition().lng());
		          
        		}
		      }
	    });
	  });
*/

      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
   
    }
    );
}


function initialize() {

    var latlng = new google.maps.LatLng(50.51167994970682,2.9388427734375);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    map = new google.maps.Map(document.getElementById("terrains_map"),myOptions);
    adrcode();
}

function affiche(idligne,max,id)
{
	for (i=1;i<=max;i++)
	{
	    var val = $("#terrain_"+i);
		if (i==idligne)
		{
			$(val).attr('style','background-color:#B5E655;')
		}
		else
		{
			$(val).attr('style','background-color:transparent;')
		}
	}
	
	$.post('/terrains/get/'+id, {  
                     	   }, 
                      	   function(data){  
                          	   	$('#terrains_detail').html(data);
                          	   	initialize();
								
                          	   	$("a.fb").fancybox();
                          	   	return false;
                    	   }
    	  );
}

function majmodeles()
{
  $.post('/modeles/majmodeles/', {
                     	   }, 
                      	   function(data){  
  								$('#pmodeles').html(data);	
  								$('#pmodeles').effect("highlight", {'color':'#59951f'}, 500);
			
  						   });
  $.post('/offres/pnbr/', {
                     	   }, 
                      	   function(data){  
  								$('#pnbr').html(data);	
  						   });
  						  			
}


function majoffres()
{
  $.post('/offres/majoffres/', {
                     	   }, 
                      	   function(data){  
  								$('#poffres').html(data);	
  								$('#poffres').effect("highlight", {'color':'#59951f'}, 500);
			
  						   });
  $.post('/offres/pnbr/', {
                     	   }, 
                      	   function(data){  
  								$('#pnbr').html(data);	
  						   });
  						  						  
  						
}

function majterrains()
{
  $.post('/terrains/majterrains/', {
                     	   }, 
                      	   function(data){  
  								$('#pterrains').html(data);	
  								$('#pterrains').effect("highlight", {'color':'#59951f'}, 500);
			
  						   });

  $.post('/offres/pnbr/', {
                     	   }, 
                      	   function(data){  
  								$('#pnbr').html(data);	
  						   });
  						  						  
  						
}

function p_offre(id)
{
	 
	  $.post('/offres/retenir/'+id, {
                     	   }, 
                      	   function(data){  
  											
  						   var $dialog = $('<div></div>').html(data).dialog({
								autoOpen: true,
								title: 'Offre',
								draggable: false,
								resizable: false,
								width:400,
								buttons: {
										"Fermer": function() {
												$( this ).dialog( "close" );
													
										}
								}
									
						   });
						   majoffres();
                      	   });
		
}

function r_offre(id,titre)
{
		var texte = "<br />Retirer cette offre: <br /><br /><b>"+titre+"</b><br /><br />";
		var $dialog = $('<div id="dg"></div>').html(texte).dialog({
								autoOpen: true,
								title: 'Confirmation',
								draggable: false,
								resizable: false,
								width:400,
								buttons: {
										"Retirer": function() {
											$.post('/offres/retirer/'+id, {
					                     	   }, 
                    					  	   function(data){  
  													window.location = "/fr/Contactez-nous,34.html";
  											   // Mise à jour <span id="poffres">
                    				  	   });	
										
										$('#dg').dialog( "close" );
			
											
										},
										"Annuler": function() {
												$( this ).dialog( "close" );
										}
								}
						});


}

function p_terrain(id)
{

	  $.post('/terrains/retenir/'+id, {
                     	   }, 
                      	   function(data){  
  											
  						   var $dialog = $('<div></div>').html(data).dialog({
								autoOpen: true,
								title: 'Terrain',
								draggable: false,
								resizable: false,
								width:400,
								buttons: {
										"Fermer": function() {
												$( this ).dialog( "close" );
													
										}
								}
									
						   });
						   majterrains();
                      	   });

}

function r_terrain(id,titre)
{
		var texte = "<br />Retirer ce terrain: <br /><br /><b>"+titre+"</b><br /><br />";
		var $dialog = $('<div id="dg"></div>').html(texte).dialog({
								autoOpen: true,
								title: 'Confirmation',
								draggable: false,
								resizable: false,
								width:400,
								buttons: {
										"Retirer": function() {
											$.post('/terrains/retirer/'+id, {
					                     	   }, 
                    					  	   function(data){  
  													window.location = "/fr/Contactez-nous,34.html";
  											   // Mise à jour <span id="poffres">
                    				  	   });	
										
										$('#dg').dialog( "close" );
			
											
										},
										"Annuler": function() {
												$( this ).dialog( "close" );
										}
								}
						});


}



function p_modele(id)
{
	 
	  $.post('/modeles/retenir/'+id, {
                     	   }, 
                      	   function(data){  
  											
  						   var $dialog = $('<div></div>').html(data).dialog({
								autoOpen: true,
								title: 'Modèle',
								draggable: false,
								resizable: false,
								width:400,
								buttons: {
										"Fermer": function() {
												$( this ).dialog( "close" );
													
										}
								}
									
						   });
						   majmodeles();
                      	   });
		
}

function r_modele(id,titre)
{
		var texte = "<br />Retirer ce modèle: <br /><br /><b>"+titre+"</b><br /><br />";
		var $dialog = $('<div id="dg"></div>').html(texte).dialog({
								autoOpen: true,
								title: 'Confirmation',
								draggable: false,
								resizable: false,
								width:400,
								buttons: {
										"Retirer": function() {
											$.post('/modeles/retirer/'+id, {
					                     	   }, 
                    					  	   function(data){  
  													window.location = "/fr/Contactez-nous,34.html";
  											   // Mise à jour <span id="poffres">
                    				  	   });	
										
										$('#dg').dialog( "close" );
			
											
										},
										"Annuler": function() {
												$( this ).dialog( "close" );
										}
								}
						});


}

function detailterrains(id)
{
//	alert(dpt+"\n"+surface+"\n"+prix);

	

}

function listeterrains(dpt, surface, prix)
{
//	alert(dpt+"\n"+surface+"\n"+prix);	

	$.post('/terrains/liste/'+dpt+'/'+surface+'/'+prix, {  
                     	   }, 
                      	   function(data){  
                      	   
                      	   $('#tab_body').html(data);
						//	initialize();

                    	   }
    	  );

}

function tri(choix,ordre)
{
	
	$.post('/terrains/tri_head/'+choix+'/'+ordre, {  
														   							},
														   							function(dt)
														   							{
														   								$('#head_resultats').html(dt);
														   								//$('#terrains_detail').html("");
														   								$('#tdetail').html("<a href''><< Cliquez sur l'un des terrains de la liste</a>");
																						$('#res_fiche').html('');

																					}
														   		  );

	
	$.post('/terrains/tri/'+choix+'/'+ordre+'/'+$('#dpt').val()+'/'+$('#surf').val()+'/'+$('#pr').val(), {  
														   							},
														   							function(dt)
														   							{
														   								$('#tab_body').html(dt);
//														   								$('#terrains_detail').html("");
														   								$('#tdetail').html("<a href''><< Cliquez sur l'un des terrains de la liste</a>");
																						$('#res_fiche').html('');

																					}
														   		  );

}



$(document).ready(function(){

	

	$('#dpt').click(function(){
		listeterrains(this.value,$('#surf').val(),$('#pr').val());
		$('#tab_body').html('');
//		$('#terrains_details').html('');
		$('#tdetail').html("<< Cliquez sur l'un des terrains de la liste");
		$('#res_fiche').html('');
	});
	
	
	$('#surf').click(function(){
		listeterrains($('#dpt').val(),this.value,$('#pr').val());
		$('#tab_body').html('');
//		$('#terrains_details').html('');
		$('#tdetail').html("<< Cliquez sur l'un des terrains de la liste");
		$('#res_fiche').html('');

	});
	
	$('#pr').click(function(){
		listeterrains($('#dpt').val(),$('#surf').val(),this.value);
		$('#tab_body').html('');
//		$('#terrains_details').html('');
		$('#tdetail').html("<< Cliquez sur l'un des terrains de la liste");
		$('#res_fiche').html('');

	});
	
	listeterrains($('#dpt').val(),$('#surf').val(),$('#pr').val());
	//initialize();


   	
});
