// JavaScript Document
$(document).ready(function()
{

//affiche la carte
// $(map_big);	
 

 
 map_big("","","");

 //deplace la liste
$("div#right_content").append("<ul id=\"menu1\" class=\"menu collapsible\">"+$("ul.menu").html()+"</ul>");
 $("ul.menu:first").remove();

 //masque les fiches
 $("ul.fiche_mj").hide();

 //GESTION DU CLIC SUR LA LISTE DEROULANTE
 $("li.nom_mj").click(function()
	{ 
		//décible le précédent
		$(".nom_mj_cible").removeClass("nom_mj_cible").addClass("nom_mj");

		//cible celui ci
		$(this).removeClass("nom_mj").addClass("nom_mj_cible");
		
		//cache l'ancienne fiche affichée
		$("li.nom_mj ul.fiche_mj").slideUp("slow");
		
		//affiche la nouvelle fiche
		$("li.nom_mj_cible ul.fiche_mj").slideDown("slow");
	});
	
	//GESTION DU CLIC POUR LOCALISER SUR LA CARTE
  $(".localiser").click(function()
	{     
    
    
    
    
    //décible le précédent
$("li.localiser_cible").removeClass("localiser_cible").addClass("localiser");

		//cible celui ci
		$(this).removeClass("localiser").addClass("localiser_cible");
		
var nouvelle_adresse=$("li.localiser_cible input.adresse_localisation").val();
	  
		//affiche le nouveau point
		map_big(nouvelle_adresse,nouvelle_adresse,"");
	});
 
 //lance les menus
 $(initMenus);	

 
 
 
  
 
});
	
		
	function initMenus() 
	{
	$('ul.menu ul').hide();
	$.each($('ul.menu'), function(){
		$('#' + this.id + '.expandfirst ul:first').show();
	});
	$('ul.menu li a').click(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			if($('#' + parent).hasClass('noaccordion')) {
				$(this).next().slideToggle('normal');
				return false;
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('normal');
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('normal');
				checkElement.slideDown('normal');
				return false;
			}
		}
	);
}


function map_big(adresse,titre,description) 
{	
	

	if (GBrowserIsCompatible()) {
	
/* ################################################################## */
/*                                    IDENTIFIANT DE LA CARTE (ID donné à l'élément HTML div)                                          */
/*                                                   exemple : <div id="nom_de_la_carte></div>                                                            */
/* ################################################################## */	
	
		var nom_de_la_carte = "ma_carte";
		var map = new GMap2(document.getElementById(nom_de_la_carte));	

/* ################################################################## */
/*                     CENTRAGE DE LA CARTE SUR UN POINT DONNE et paramétrage niveau de zoom                             */
/*                              Latitude et Longitude (données disponible sur googlemap ou googleearth)                                      */
/* ################################################################## */

		var latitude 	= "50.853209";
		var longitude 	= "4.361572";
		var niveau_zoom = 7;
		
		map.setCenter(new GLatLng(latitude,longitude), niveau_zoom);	

/* ################################################################## */
/*                                                             AFFICHAGE DES CONTROLES                                                             */
/* ################################################################## */

		/* Contrôle de ZOOOM */
		map.addControl(new GLargeMapControl());

		/* Contrôle de VUES - PLAN,SATELLITE,MIXTE */
		map.addControl(new GMapTypeControl());

                /* */
                map.addMapType(G_PHYSICAL_MAP);

		/* Contrôle de la vue général (situé en bas à gauche de la carte) */
		//map.addControl(new GOverviewMapControl());

		/* Contrôle de ZOOM avec la roulette */
		map.enableScrollWheelZoom();

		/* Contrôle de ZOOM au double click */
		map.enableDoubleClickZoom();

                /* pas de cache */
		geo = new GClientGeocoder();
		geo.setCache(false);

                /* Vue par default*/
                map.setMapType(G_PHYSICAL_MAP);

/* ################################################################## */
/*                                                                      VARIABLES                                                                              */
/* ################################################################## */	

		var bounds = new GLatLngBounds();
		var mt = map.getMapTypes();		

/* ################################################################## */
/*                                                                           ICONES                                                                             */
/* ################################################################## */	

	   	var appartement = new GIcon();
		appartement.image = "../layout/icone/appartement.png";
		appartement.iconSize = new GSize(48, 65);  
		appartement.iconAnchor = new GPoint(48, 65);
		appartement.infoWindowAnchor = new GPoint(48, 65); 
		
		var maison = new GIcon();
		maison.image = "../layout/icone/maison.png";
		maison.iconSize = new GSize(48, 49);  
		maison.iconAnchor = new GPoint(48, 49);
		maison.infoWindowAnchor = new GPoint(48, 49); 
		
		var terrain = new GIcon();
		terrain.image = "../layout/icone/terrain.png";
		terrain.iconSize = new GSize(59, 50);  
		terrain.iconAnchor = new GPoint(59, 50);
		terrain.infoWindowAnchor = new GPoint(59, 50); 

/* ################################################################## */
/*                                                   PLACEMENT DES ELEMENTS SUR LA CARTE                                                   */
/* ################################################################## */

		geocoder = new GClientGeocoder();	
		function creatgooglemap(g_adress,g_title,g_photoURL,g_description,g_pageURL) {
		
			if (geocoder) {	

				/* Création du détail de l'affiche de l'élément */
				var PARAM_content	= '<div class="tooltip"><div class="image"><IMG id="Photo" SRC="'+g_photoURL+'"> </div><div class="donnees1">'+g_title+'</div><div class="donnees2">'+g_description+'</div></div>';

				/* Génération du point de géolocalisation */
				geocoder.getLatLng(
					g_adress,
					function(point) {
						if (!point) {		
							var tooltip = document.createElement("div");
							document.getElementById(nom_de_la_carte).appendChild(tooltip);
							tooltip.style.visibility="hidden";  
							return false;
						} 
						else {					
							var tooltip = document.createElement("div");
							document.getElementById(nom_de_la_carte).appendChild(tooltip);
							tooltip.style.visibility="hidden";  
							var marker = new GMarker(point);             		   
							map.addOverlay(marker);    
							marker.tooltip = PARAM_content;

							GEvent.addListener(marker,'click', function() {  
								location.href=g_pageURL; 
							});

							GEvent.addListener(marker,'mouseover', function() {
								tooltip.innerHTML = marker.tooltip;    
								var pointeur=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
								var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
                                                                var anchor=marker.getIcon().iconAnchor;
								var width=marker.getIcon().iconSize.width;
								var height=marker.getIcon().iconSize.height;
								var PosX = offset.x - pointeur.x - anchor.x + width;
								var PosY = - offset.y + pointeur.y +anchor.y;
								if ( PosX > 355) PosX -= 250 + width + anchor.x/3;
								if ( PosY > 145) PosY -= 145 -  anchor.y + height;
								if ( PosY > 80) PosY -= 65 ;
								var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(PosX,PosY)); 
								pos.apply(tooltip);
								tooltip.style.visibility="visible";
							});        
							GEvent.addListener(marker,'mouseout', function() { 
								tooltip.style.visibility='hidden' 
							});   					  						   
						}
					}
				);
			}
		}

		
/* ################################################################## */
/*                                                PARAMETRAGE DES ELEMENTS SUR LA CARTE                                                  */
/* ################################################################## */
		
		/* ATTENTION, concernant l'ICONE à affiché sur la carte : SI maison=maison, SI appartement=appartement, SI terrain=terrain */
		/* INFO : idéalement, la description ne doit pas être trop longue. Il vaut mieux metter ici une description courte et rapide */
 creatgooglemap(adresse,titre,"",description,"");



	}

  
 
	
	
	
}

		

	