/*
  --------------
  Module Cartographie -- v 0.3 -- Copyright Altimax NB
  dernière modif : Jerome le 14/05/2010
  --------------
*/

//initialisation de la gmap
function init_gmap(itemid, langue, tab, commune, nom, categorie)
{   

	if (GBrowserIsCompatible()) 
	{
		map =new GMap2(document.getElementById("map_carto"));
		// initialisation de la gmap
		map.setUIToDefault();
		map.disableScrollWheelZoom();
		map.setMapType(G_HYBRID_MAP);
 		//map.setCenter(new GLatLng(0,0),0); 
 		map.setCenter(new GLatLng(45.3432176,5.8166599),9); 

/*		$j.ajax({
			  url: "/php/ajax/add-marker-carto.ajax.php",
			  type: "POST",
			  dataType:"script",
			  data: ({itemid : itemid, tab : tab, commune : "", nom : "", categorie : ""}),
			  beforeSend :function(){$j("#loading").fadeIn(200);},
			  complete : function(){$j("#loading").fadeOut(200);}
		});
*/
	$j.ajax({
		  url: "/php/carte-interactive/ajax/genereXML.php",
		  type: "POST",
		  data: ({itemid : itemid, tab : tab, commune : commune, nom : nom, categorie : categorie}),
		  dataType: "xml",
		  beforeSend :function(){$j("#loading").fadeIn(500);},
		  complete: function(msg)
		  {
			if(msg.responseText != "")
			{
				  var xmlDoc = GXml.parse(msg.responseText);
				  // obtain the array of markers and loop through it
				  var markers = xmlDoc.documentElement.getElementsByTagName("marker");
				  var nb = markers.length;
				  if(nb > 0)
				  {
					  for (var i = 0; i < nb; i++) 
					  {
							// obtain the attribues of each marker
							var lat 			= 	parseFloat(markers[i].getAttribute("lat"));
							var lng 			=	parseFloat(markers[i].getAttribute("lng"));
							var point 			= 	new GLatLng(lat,lng);
							var name 			= 	markers[i].getAttribute("name"); 
							var label 			= 	markers[i].getAttribute("label"); 
							var adresse 		= 	markers[i].getAttribute("adresse");
							adresse 			=	adresse.replace(/\|/g,'<br />');
							var tel 			= 	markers[i].getAttribute("tel");
							tel 				=	tel.replace('|','<br />');
							var url 			= 	markers[i].getAttribute("url");
							var photo 			= 	markers[i].getAttribute("photo");
							var circ 			= 	markers[i].getAttribute("circ");
							var lieu		 	= 	markers[i].getAttribute("lieu");
							// create the marker
							var marker = createMarker(point,name,label,adresse,tel,url,photo,circ,lieu);
							map.addOverlay(marker);
							bounds.extend(point);
				  		}					
						//map.setCenter(bounds.getCenter(), 10);
						map.setZoom(map.getBoundsZoomLevel(bounds));
 						map.setCenter(bounds.getCenter());

						$j("#loading").fadeOut(500);
				}
				else
				{
					$j("#loading").empty().html("<h3>Pas de résultat pour cette recherche.</h3>");
					
				}
				if (nb == 1) { map.setZoom(map.getBoundsZoomLevel(bounds)-4); }
			 }
			
		  },
		  error: function(msg){
			 //alert(msg.responseText); 
		  }
		});
	
    }
    else 
	{
      alert("Désolé, votre navigateur n'est pas compatible avec Gmap !!!");
    }
	
}//init_gmap

// A function to create the marker and set up the event window
function createMarker(point,name,label,adresse,tel,url,photo,circ,lieu) {
	var marker = new GMarker(point,Icon);
	
	var infoBulle = '<div class="infobulle">'
	if(photo != "")
	{
		infoBulle += '<img src="'+photo+'" alt="'+name+'" width="100" class="left"/>';
	}
	infoBulle += "<h3>" + name + "</h3>";
	infoBulle += "<p class='puceVert'><strong>" + label + "</strong></p>";
	//infoBulle += "<p>" + lieu + "</p>";
	infoBulle += "<p>" + adresse + "</p>";
	infoBulle += "<p>" + tel + "</p>";
	if(circ != "")
	{
		infoBulle += '<p><br />circonscription : '+circ+'</p>';
	}
	if(url != "")
	{
		infoBulle += '<a href="'+url+'" title="'+name+'" class="plus_infos">+ d\'infos<br />';
	}
	infoBulle += '</div>';

	GEvent.addListener(marker, "click", function() 
	{
	  marker.openInfoWindowHtml(infoBulle);
	  //removeSelect();
	});
	
	
	gmarkers[i] = marker;
	htmls[i] = infoBulle;
	if(openInfoBulle){gmarkers[i].openInfoWindowHtml(htmls[i])};
	
	i++;
	return marker;
}

// click sur un item de la liste = on affiche l'info bulle correspondante
function myclick(i) 
{
	gmarkers[i].openInfoWindowHtml(htmls[i]);
}


function importanceOrder (marker,b) 
{
	return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
}


var k =0;
//Cette fonction ajoute les pictos parking et autres point d'intérêt via Ajax
function addCartoMarker  (contenu,lat,lng,marker,total,id_picto) { 
  var markerPerso = {};

	if(marker  !=  "")
  {
    var iconePerso = new GIcon(G_DEFAULT_ICON);
    iconePerso.iconSize = new GSize(26, 26);
    iconePerso.iconAnchor = new GPoint(14, 26);
    iconePerso.infoWindowAnchor = new GPoint(14, 13);
    iconePerso.image= marker;
    markerPerso = { icon:iconePerso, zIndexProcess:importanceOrder };
  }
  var point = new GLatLng(lat,lng);
  var mMarker = new GMarker(point,markerPerso);
  mMarker.maCategorie = id_picto;
  mMarker.importance = 2;
  aMarkers.push(mMarker);

  k++;
  map.addOverlay(mMarker);
  bounds.extend(point);
    
  GEvent.addListener(mMarker,"click",function() {
    var rand  = Math.round(Math.random()*100);
    mMarker.openInfoWindowHtml("<div class='infowindow_gmap'>"+contenu+"</div>");
  });
  
  //gestion auto du zoom et du centre
  map.setZoom((map.getBoundsZoomLevel(bounds)-2));
  map.setCenter(bounds.getCenter());
  
}

function removeMarkers (category) {
	
	for (var i=0; i<aMarkers.length; i++) 
	{
		if (aMarkers[i].maCategorie == category) 
		{
			aMarkers[i].closeInfoWindow();
			aMarkers[i].hide();
		}
	}
	bounds = new GLatLngBounds();

}
