var map;
function initialize(lat,long,zoom) {
	//alert(zoom);
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_home"));
		map.setCenter(new GLatLng(lat, long), zoom);

		updateMarkers();
		GEvent.addListener(map,'zoomend',function() {
			updateMarkers();
		});
		GEvent.addListener(map,'moveend',function() {
			updateMarkers();
		});
		
	}
}

function updateMarkers() {
	//remove the existing points
	map.clearOverlays();
	//create the boundary for the data
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var getVars = 'ne=' + northEast.toUrlValue()
	+ '&sw=' + southWest.toUrlValue()

	//log the URL for testing
	var urlserver ='server.php?'+getVars 
	//alert(urlserver);
	//GLog.writeUrl(urlserver);
	//retrieve the points using Ajax
	var request = GXmlHttp.create();
	request.open('GET', 'server.php?'+getVars, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var jscript = request.responseText;
			var points;
			eval(jscript);
			//create each point from the list
			for (i in points) {
//				var point = new GLatLng(points[i].lat,points[i].lng);
//				var marker = createMarker(point,points[i].testo);
//				map.addOverlay(marker);
				addMarker(points[i].lat, points[i].lng, points[i].testo,points[i].tipologia);
			}
		}
	}

	request.send(null);

}
function createMarker(point, html) {
	var marker = new GMarker(point);
//	GEvent.addListener(marker, 'click', function() {
//		var markerHTML = html;
//		marker.openInfoWindowHtml(markerHTML);
//	});
	return marker;
}
function addMarker(latitude, longitude, description,tipologia) {

	//alert(tipologia);
	
	var marcatore = typeof(tipologia) != 'undefined' ? tipologia : "standard";

	if(marcatore=="standard"){
		var legaIcon = new GIcon();
		legaIcon.iconSize = new GSize(34,29);
		legaIcon.iconAnchor = new GPoint(14,25);
		legaIcon.infoWindowAnchor = new GPoint(14,14);
		legaIcon.image = "http://mappe.centralemobilita.it/img/markers/seiqui.png";
		// var legaicon = new GIcon(G_DEFAULT_ICON);
	}
	else if(marcatore=="centrale"){
		var legaIcon = new GIcon();
		legaIcon.iconSize = new GSize(44,42);
		legaIcon.iconAnchor = new GPoint(14,25);
		legaIcon.infoWindowAnchor = new GPoint(14,14);
		legaIcon.image = "http://mappe.centralemobilita.it/img/markers/centrale.png";
	}
	else if(marcatore=="legambiente"){
		var legaIcon = new GIcon();
		legaIcon.iconSize = new GSize(34,29);
		legaIcon.iconAnchor = new GPoint(14,25);
		legaIcon.infoWindowAnchor = new GPoint(14,14);
		legaIcon.image = "http://reporter.puliamoilmondo.it/la.png";
	}
	else{
		var legaIcon = new GIcon();
		legaIcon.iconSize = new GSize(34,29);
		legaIcon.iconAnchor = new GPoint(14,25);
		legaIcon.infoWindowAnchor = new GPoint(14,14);
		legaIcon.image = "http://mappe.centralemobilita.it/img/markers/"+ tipologia;
	}

    var marker = new GMarker(new GLatLng(latitude, longitude),legaIcon);
    // var marker = new GMarker(new GLatLng(latitude, longitude));

//    GEvent.addListener(marker, 'click',
//        function() {
//            marker.openInfoWindowHtml(description);
//        }
//    );

    map.addOverlay(marker);
}





