// JavaScript Document

var map = null;
var markers = [];
var marker = null;
var geocoder = null;

$(document).ready(function() {
	if (GBrowserIsCompatible()) {
	// Initialize the map.
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.setCenter(new GLatLng(42,12), 5);
		geocoder = new GClientGeocoder();
		loadMarkers("/include/googlemaps/markers.asp");
	}
});

function clearmarkers() {
	var markers = [];
	for (var i=0;i<markers.length;i++) {
  	map.removeOverlay(markers[i].marker);
	}
}


function showAddress(address) {
	if (geocoder) {		

		geocoder.getLatLng(address, function(point) {
			if (!point) {
				alert("Indirizzo non trovato");
			} else {
				map.setCenter(point, 13);
				marker = new GMarker(point, {draggable: true});
				map.addOverlay(marker);
				//marker.openInfoWindowHtml(address);
			}});
	}
}


function loadMarkers(str) {

	// Create our "tiny" marker icon
	var tinyIcon = new GIcon();
		tinyIcon.image = "/include/googlemaps/icon.png";
		//tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		tinyIcon.iconSize = new GSize(26, 26);
		//tinyIcon.shadowSize = new GSize(22, 20);
		tinyIcon.iconAnchor = new GPoint(6, 20);
		tinyIcon.infoWindowAnchor = new GPoint(5, 1);

	// carica il file esterno con i punti ... 
	$.ajax({type:"GET",url:str,cache:false,dataType:"xml",

		success:function(data,textStatus) {
			$("marker",data).each(function() {		
				
				// Attributes for each marker.
				var lat = parseFloat($(this).attr("lat"));
				var lng = parseFloat($(this).attr("lng"));
				var icon = $(this).attr("icon");
				var point = new GLatLng(lat,lng);
				var html = $("html",this).text();
								
				// Set up our GMarkerOptions object literal
				switch(icon)
				{														
					default:markerOptions = { icon:tinyIcon };  
				}
	
				// Create the marker.
				var marker = new GMarker(point,markerOptions);							
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(html);
				});
				map.addOverlay(marker);
			});
		}, // end success

		error:function(XMLHTTPRequest,textStatus,errorThrow){
			alert("Errore nel file XML");
		} // emd error
		
	});
}



$(document.body).unload(function() {
	if (GBrowserIsCompatible()) {
		GUnload();
	}
});

