
/* Error messages for possible errors */
		var error_address_empty 	= 'Inserici un indirizzo valido.';
		var error_invalid_address 	= 'Indirizzo non valido'; 
		var error_google_error 		= 'Prova di nuovo.';
		var error_no_map_info		= 'Non sono disponibili informazioni per questo indirizzo.';

	
	//var default_address = 'Via del Fattorone 1, Castel del Piano';
		
		
		
		var current_address = null; /* Current address we are displaying, we save it here for directions */
		var map				  = null; /* Instance of Google Maps object */
		var geocoder		  = null; /* Instance of Google Deocoder object */
		var gdir				  = null; /* Instance of Google Directions object */
		var map_compatible  = false; /* Whether or not user's browser is compatible to show the map */
		
		/* Check if the browser is compatible */
		if( GBrowserIsCompatible() ) {
			map_compatible = true; 
		}
		
		function initialize_map(default_address, info) {
			if( map_compatible ) {
				map 	  	= new GMap2(document.getElementById('map_canvas'));        
				geocoder = new GClientGeocoder();
				show_address(default_address, info);
				
				/* This displays the zoom controls for the map. If you don't want them just delete the line */
				map.addControl(new GSmallMapControl());
				
				/* This displays the map type. If you don't want that feature then just delete this */
				map.addControl(new GMapTypeControl());
				
			}
		}
		function show_address(address, info) {
			if( map_compatible && geocoder ) {
				/* Save this address in current_address value to use later if user wants directions */
				current_address = address;		
				geocoder.getLatLng(
				address,
				function( point ) {
					if( !point ) {
						alert(error_no_map_info);
					} else {
						map.setCenter(point, 13);
						var marker = new GMarker(point);
						map.addOverlay(marker);
						marker.openInfoWindowHtml(info);
					}
				}
				);
			}
			
			return false;
		}
		
		function get_directions() {
			if( map_compatible ) {
				if( document.direction_form.from_address.value == '' ) {
					alert(error_address_empty);
					return false;
				}
				/**
				 * Delete the contents of 'directions' DIV first 
				 * because user might ask for directions more than once.
				**/
				document.getElementById('directions').innerHTML = '';
				
				gdir = new GDirections(map, document.getElementById('directions'));
				
				/* Setup to event handlers, one: when the directions are loaded, two: if there was any error */
				GEvent.addListener(gdir, 'load',  onGDirectionsLoad);
				GEvent.addListener(gdir, 'error', handleErrors);
				
				/* Show the directions */
				set_directions(document.direction_form.from_address.value, current_address);			
			}
			return false;
		}
		function set_directions(fromAddress, toAddress) {
      	gdir.load("from: " + fromAddress + " to: " + toAddress,
                	{ "locale": "it" });
    	}
		function handleErrors(){
			if( gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS )
				alert(error_invalid_address);
			else if( gdir.getStatus().code == G_GEO_SERVER_ERROR )
				alert(error_google_error);
			else if( gdir.getStatus().code == G_GEO_MISSING_QUERY )
				alert(error_address_empty);
			else 
				alert(error_invalid_address);
		}
		function onGDirectionsLoad(){
			/* We will simple scroll down to the directions, but with a little delay so it's loaded */
			setTimeout('eval(\'window.location = "#directions_table"\;\')', 500);
		}
		
 function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
