            (function($) {
				var map;
				var mapEl;
                $.fn.mappaContatti = function(options) {
                    
                    var opts = $.extend({}, $.fn.mappaContatti.defaults, options);
        
                    if (GBrowserIsCompatible()) {
						directionsEl=$('#'+opts.directionsEl+'');
						mapEl=$(this);
                        arrivo=opts.arrivo;
						locale=opts.locale;
						
						if(directionsEl.css('display')=='block'){
							larg_originale=(mapEl.width()+5)*2;
				
							directionsEl.hide();
							
							mapEl.css({float:'none'});
							mapEl.width(larg_originale);
						}

						map = new GMap2(mapEl.get(0));
       
                        map.addControl(new GSmallMapControl());
                        map.addControl(new GMapTypeControl());
                        
//						map.setCenter(new GLatLng(37.4419, -122.1419), 13);
						setCenter(map, opts.centro_lat, opts.centro_lon);

						addMarker(map, opts.centro_lat, opts.centro_lon, opts.testo_box);
                    
						map.setMapType(G_SATELLITE_MAP);
                        
						addDirections(map);
                    }
                }
				
				$.fn.mappaContatti.defaults = {
                        centro_lat:   '44.455058',
                        centro_lon:   '11.507149',
                        testo_box:    'sss',
						directionsEl: 'directions',
						arrivo:'',
						locale:'it'
				};
                
                
				function setCenter(map,centro_lat,centro_lon){
                    point=new GLatLng(centro_lat,centro_lon);
                    map.setCenter(point, 17);
				}
                
                function addMarker(map,centro_lat,centro_lon,testo_box){
                    point=new GLatLng(centro_lat,centro_lon);
                    var marker = new GMarker(point);
                    map.addOverlay(marker);
                    
					GEvent.addListener(map, "maptypechanged", function() {
						marker.openInfoWindowHtml(testo_box);
					});
                }
                
                function addDirections(map){
					gdir = new GDirections(map, directionsEl.get(0));
					GEvent.addListener(gdir, "load", onGDirectionsLoad);
					GEvent.addListener(gdir, "error", handleErrors);
					
					$('#submitDirections').click(function(){
						directionsEl.html('');
						alt_finale=mapEl.height();
						larg_finale=(mapEl.width()/2)-5;
					
						if(directionsEl.css('display')!='block'){
							mapEl.width(larg_finale);
							directionsEl.width(larg_finale);
							directionsEl.height(alt_finale);
							
							directionsEl.css({overflow:'auto',display:'block'});
							
							mapEl.css({float:'left'});
							
							directionsEl.show();
						}
						
						$('#map').mappaContatti.setDirections($('#indirizzo').val(), arrivo,locale);
					});
                }
                
                function handleErrors(){
                    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
                        directionsEl.html("<p class=\"mapError\">Non sono state trovate le coordinate geografiche dell'indirizzo inserito. Questo puo' dipendere dal fatto che l'indirizzo e' relativamente nuovo oppure non e' corretto.</p><p class=\"mapError\">Codice errore: " + gdir.getStatus().code+"</p>");
                    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
                        directionsEl.html("<p class=\"mapError\">La richiesta di indicazioni stradali o di coordinate geografiche non e' andata a buon fine, ma la ragione e' sconosciuta (???).</p><p class=\"mapError\">Codice errore: " + gdir.getStatus().code+"</p>");
                    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
                        directionsEl.html("<p class=\"mapError\">Manca il parametro HTTP 'q' oppure e' privo di valore. Nel caso di richieste di coordinate geografiche vuol dire che non e' stato immesso un indirizzo. Nel caso di richieste di indicazioni stradali che la rihiesta e' vuota.</p> Codice errore: " + gdir.getStatus().code+"</p>");
                    else if (gdir.getStatus().code == G_GEO_BAD_KEY)
                        directionsEl.html("<p class=\"mapError\">La Google Map Key non e' valida o non corrisponde al dominio corrente.</p><p class=\"mapError\">Codice errore: " + gdir.getStatus().code+"</p>");
                    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
                        directionsEl.html("<p class=\"mapError\">La richiesta di indicazioni stradali non e' stata letta correttamente.</p><p class=\"mapError\">Codice errore: " + gdir.getStatus().code+"</p>");
                    else 
                        directionsEl.html("<p class=\"mapError\">Errore sconosciuto.</p>");
                }
                
                function onGDirectionsLoad(){
                    map.checkResize();
                    map.setMapType(G_NORMAL_MAP);
                    map.closeInfoWindow();
                }
				
                $.fn.mappaContatti.setDirections=function(fromAddress, toAddress, locale) {
//                  alert("from: " + fromAddress + " to: " + toAddress)
                    gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
                }
            })(jQuery);
