function loadPercorso() {
//	icon = new GIcon();
	icon = new google.maps.Icon();
	icon.image = 'http://www.'+percorso_dati['dominio']+'/puntatore_mappa.gif';
	icon.iconSize = new GSize(percorso_dati['icon_width'], percorso_dati['icon_height']);
	icon.iconAnchor = new GPoint(20, percorso_dati['icon_height']);
	var Lat = percorso_dati['Lat'];
	var Lon = percorso_dati['Lon'];
	var map = new GMap2(document.getElementById("percorso_mappa"));
	var center = new GLatLng(Lat, Lon);
	map.setCenter(center, percorso_dati['zoom']);
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	var marker = new GMarker(new GLatLng(Lat, Lon), icon);
	map.addOverlay(marker);
}

function setDirections(indirizzo) {
	if (typeof( mapcanvas ) != 'object') {
		mapcanvas = new GMap2(document.getElementById("percorso_mappa"));
		gdir = new GDirections(mapcanvas, document.getElementById("percorso_testuale"));
		mapcanvas.addControl(new GSmallMapControl());
		mapcanvas.addControl(new GMapTypeControl());
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
	}
	if (typeof( indirizzo ) == 'object') {
			indirizzo = indirizzo.value;
			var scelta = document.forms[0].elements['scelta'];
			for (i=0; i<scelta.length; i++) scelta[i].checked = false;
	}
	var fromAddress = indirizzo;
	var toAddress = percorso_dati['struttura'];
	marker_struttura_index = 1;
	marker_indirizzo_index = 0;
	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale" : percorso_dati['locale'] });
}

function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
		alert("The given key is either invalid or does not match the domain for which it was given.\n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	else
		alert("An unknown error occurred.");
}

function onGDirectionsLoad(){
	var icona_marker_struttura = gdir.getMarker(marker_struttura_index).getIcon();
	icona_marker_struttura.image = icon.image;
	icona_marker_struttura.iconSize = icon.iconSize;
	icona_marker_struttura.iconAnchor = icon.iconAnchor;
	var icona_marker_indirizzo = gdir.getMarker(marker_indirizzo_index).getIcon();
	icona_marker_indirizzo.image = "http://maps.google.com/mapfiles/kml/pal3/icon31.png";
	icona_marker_indirizzo.iconSize = new GSize(32, 32);
	icona_marker_indirizzo.iconAnchor = new GPoint(16, 32);
}

google.setOnLoadCallback(function(){
	$(".rollover").rollover();
	$('#slider').nivoSlider({
        effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
        slices:15, // For slice animations
        boxCols: 8, // For box animations
        boxRows: 4, // For box animations
        animSpeed:700, // Slide transition speed
        pauseTime:3500, // How long each slide will show
        startSlide:0, // Set starting Slide (0 index)
        directionNav:false, // Next & Prev navigation
        directionNavHide:false, // Only show on hover
        controlNav:false, // 1,2,3... navigation
        controlNavThumbs:false, // Use thumbnails for Control Nav
        controlNavThumbsFromRel:false, // Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', // Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
        keyboardNav:false, // Use left & right arrows
        pauseOnHover:false, // Stop animation while hovering
        manualAdvance:false, // Force manual transitions
        captionOpacity:0.8, // Universal caption opacity
        prevText: 'Prev', // Prev directionNav text
        nextText: 'Next', // Next directionNav text
        beforeChange: function(){}, // Triggers before a slide transition
        afterChange: function(){}, // Triggers after a slide transition
        slideshowEnd: function(){}, // Triggers after all slides have been shown
        lastSlide: function(){}, // Triggers when last slide is shown
        afterLoad: function(){} // Triggers when slider has loaded
    });
	$("a[rel=group]").fancybox({
		'transitionIn': 'none',
		'transitionOut': 'none',
		'titlePosition': 'none',
		'overlayColor' : '#333',
		'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
				return '';
			}
	});
	$(".various3").fancybox({
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe',
				'overlayColor'		: '#333'
			});
	var dates = $( "#checkin, #checkout" ).datepicker({
		minDate: 'today',
		dateFormat: 'dd-mm-yy',
		numberOfMonths: 1,
		onSelect: function( selectedDate ) {
			var option = this.id == "checkin" ? "minDate" : "maxDate",
				instance = $( this ).data( "datepicker" ),
				date = $.datepicker.parseDate(
					instance.settings.dateFormat ||
					$.datepicker._defaults.dateFormat,
					selectedDate, instance.settings );
			dates.not( this ).datepicker( "option", option, date );
		}
	});
	$(".bookingLink").fancybox({
		'type': 'iframe',
		'autoScale': false,
		'width': 660,
		'height': '100%',
		'overlayColor': '#333',
		'onStart': function() {
			$(".bookingLink").attr('href', $(".bookingLink").attr('href') + '&checkin=' + $("#checkin").val() + '&checkout=' + $("#checkout").val() + '&numRooms=' + $("#numRooms").val() + '&bform[reqRooms][1][adults]=1');
		}
	});
	
	if (typeof(google.maps)!='undefined') {
		var mapcanvas;
		var gdir;
		var marker_struttura_index;
		var marker_indirizzo_index;
		var icon;
		loadPercorso();
		$('#percorsi').submit(function () {
			setDirections($('#indirizzo').val());
			return false;
		});
	}
});
