google.load("maps", "2.x");
google.setOnLoadCallback(initialize);

var map;
var mapC;
var mapSv;
var panorama;
var svClient;
var geocoder;
var iconaRealigro = new Array();
var points = null;
var minZoom = 20;
var lastMarkerLocation = null;
var svAttiva = false;

function initialize() {	
	$("mappa").addClassName("attiva");	
	
	// container della mappa:
	mapC = $("mapcontainer");
	
	// Inizializza la mappa:
	map = new google.maps.Map2(mapC);
	map.setCenter(new google.maps.LatLng(41.9, 12.49), 5);
	map.addControl(new GSmallMapControl());
	
	// Mappa Street View:
	mapSv = $("mapcontainerStreetView");
	
	// Street view stuff:
	svClient = new GStreetviewClient();              
			
	// Customizza le icone:		
	iconaRealigro[0] = createIcon(0);
	iconaRealigro[1] = createIcon(1);	
		
	// Inizializza i punti selezionati:	
	initializePoint();
	
	// Inizializza i pulsanti:
	GEvent.addDomListener($("mapinfoMappa"), "click", function() {
		map.setMapType(G_NORMAL_MAP);
		closeStreetView();
	});
	
	GEvent.addDomListener($("mapinfoSatellite"), "click", function() {
		map.setMapType(G_SATELLITE_MAP);
		closeStreetView();
	});
	
	GEvent.addDomListener($("mapinfoIbrida"), "click", function() {
		map.setMapType(G_HYBRID_MAP);
		closeStreetView();
	});	
	
	// Pulsante street view:		
	GEvent.addDomListener($("mapinfoStreetView"), "click", function() {
		openStreetView();
	});	
}

function openStreetView() {
	mapC.hide();
	mapSv.show(); 		
}

function closeStreetView() {
	mapSv.hide();
	mapC.show();
}

function initializePoint() {
	var bounds = new GLatLngBounds(); 
	var defPoints = false;
	
	for (i in points) {	
		defPoints = true;	
		var point = points[i];		
		var latlng = new GLatLng(parseFloat(point.lat), parseFloat(point.lng));
		bounds.extend(latlng); 
		
		var tipo = 0;
		if (parseInt(point.tipo) == 1) { 
			tipo = 1;
		} 
		
		var marker = createMarker(latlng, point.msg, iconaRealigro[tipo]);
		map.addOverlay(marker);		
		
		// Richiama questa funzione per tutti i marker per 
		// assicurarsi che la view sia attivata sul primo disponibile:
		svClient.getNearestPanorama(latlng, initializeStreetView); 
	}
	
	if (defPoints) {
		// Setta la mappa per mostrare solo l'area
		// che include i marker:
		var zoom = map.getBoundsZoomLevel(bounds);
		if (zoom > minZoom) zoom = minZoom;
		
		var center = bounds.getCenter();
		
		map.setZoom(zoom);
		map.setCenter(center);
	} else {
		$("mappa").removeClassName("attiva");		
	}
}

function initializeStreetView(response) {
	if (response.code != 200) {
		return;
	}
	
	if (svAttiva) {
		return;
	} else {
		svAttiva = true;		
		$("mapinfoStreetViewContainer").show();
		openStreetView();
	}
	
	// Istanza della Street View Panorama:	
	panorama = new GStreetviewPanorama(mapSv);

	// Eventi:
	GEvent.addListener(panorama, "initialized", onNewLocation);
	GEvent.addListener(panorama, "yawchanged", onYawChange); 

	// Icona dell'omino per segnare la posizione sulla GMap:
	var guyIcon = new GIcon(G_DEFAULT_ICON);
	guyIcon.image = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-0.png";
	guyIcon.transparent = "http://maps.google.com/intl/en_us/mapfiles/cb/man-pick.png";
	guyIcon.imageMap = [
			26,13, 30,14, 32,28, 27,28, 28,36, 18,35, 18,27, 16,26,
			16,20, 16,14, 19,13, 22,8
	];
	guyIcon.iconSize = new GSize(49, 52);
	guyIcon.iconAnchor = new GPoint(25, 35);  
	guyIcon.infoWindowAnchor = new GPoint(25, 5);  

	// Mostra l'overlay con lo street view:
	AddSVOverlay();
	
	// Punto in cui posizionare l'omino:
	var latlng = new GLatLng(response.Location.lat, response.Location.lng);
	
	// Inserisce un marker sulla mappa con l'omino:
	marker = new GMarker(latlng, {icon: guyIcon, draggable: true, zIndexProcess: svMarkerZIndex});
	marker.setLatLng(latlng);
	map.addOverlay(marker);
	
	// Aggiorna la posizione dell'omino a quella appena specificata per il marker sulla mappa:		
	lastMarkerLocation = latlng;	
		
	GEvent.addListener(marker, "dragend", onDragEnd);
	panorama.setLocationAndPOV(latlng, null);
}

function AddSVOverlay() {
    svOverlay = new GStreetviewOverlay();
    map.addOverlay(svOverlay);
}

// Setta lo z-index del marker dello Street View
// per fare in modo che sia sempre in primo piano rispetto agli altri marker.
function svMarkerZIndex() {
	return 100;
}

function createMarker(latlng, html, icona) {
	var marker = new GMarker(latlng, icona);
	GEvent.addListener(marker, 'click', function() {
		var markerHTML = html;
		marker.openInfoWindowHtml(markerHTML);
	});
	
	return marker;
}


function createIcon(tipo) {
	var icon = new GIcon();
	
	switch (parseInt(tipo)) {
		case 1:
			icon.image = "/img/marker2.png";
			break;		
		default:
			icon.image = "/img/marker.png";
			break;
	}
	icon.iconSize = new GSize(22,26);
	icon.iconAnchor = new GPoint(11,26);
	icon.infoWindowAnchor = new GPoint(11,13);
	
	return icon;
}


function onYawChange(newYaw) {
	var GUY_NUM_ICONS = 16;
	var GUY_ANGULAR_RES = 360/GUY_NUM_ICONS;
	if (newYaw < 0) {
		newYaw += 360;
	}
	guyImageNum = Math.round(newYaw/GUY_ANGULAR_RES) % GUY_NUM_ICONS;
	guyImageUrl = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-" + guyImageNum + ".png";
	marker.setImage(guyImageUrl);
}

function onNewLocation(locationObj) {
	marker.setLatLng(locationObj.latlng);
}

function onDragEnd() {
	var latlng = marker.getLatLng();
	if (panorama) {
		svClient.getNearestPanorama(latlng, onResponse);
	}
}

function onResponse(response) {
	if (response.code != 200) {
		marker.setLatLng(lastMarkerLocation);
	} else {
		var latlng = new GLatLng(response.Location.lat, response.Location.lng);
		marker.setLatLng(latlng);
		lastMarkerLocation = latlng;
		panorama.setLocationAndPOV(latlng, null);
	}
}