/* global variables */

//the google map
var map = null;
var urlPoi = '/data-poi.php';

$(document).ready(function(){
	initialize();
});


//initialize map
function initialize() {
    if (GBrowserIsCompatible()) {
    	map = new GMap2(document.getElementById("ggmap"));
		//center to europe
        map.setCenter(new GLatLng(46.9479726, 7.4481264), 4);
        map.addControl(new GSmallZoomControl3D());
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		processXML(urlPoi);
	}
}


/* 
Add a POI on the map
Params :
lat : latitude
lng : longitude
name : title name in the bubble info
text : text in the bubble info
*/
function addMarker(lat, lng, name, address, desc, links){
	var point = new GLatLng(lat,lng);
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
    		$("#refresh-contact").html(name+ " "+address);	
		$("#refresh-infos").html(desc);	
		$("#refresh-links").html(links);	
  	});
	map.addOverlay(marker);
	
}

/*
Get the xml poi data, parse and add POI on the map
Call on the init of the map
*/
function processXML(url){
	$.ajax({ 
			type: "GET", 
			url: url, 
			dataType: "xml", 
			success: function(xml){
				$(xml).find('poi').each(function(){
					var lat = $(this).attr('lat');	
					var lng = $(this).attr('lng');	
					var name = $(this).find('name').text();	
					var address = $(this).find('address').text();
					var desc = $(this).find('desc').text();
					var links = $(this).find('links').text();
					var selected= $(this).attr('selected')==1?true:false;
					addMarker(lat,lng,name,address,desc,links);
					if(selected)
					{
						$("#refresh-contact").html(name+ " "+address);	
						$("#refresh-infos").html(desc);	
						$("#refresh-links").html(links);
					}
				});	
			}
	});
	
	  	

	
}
