    var map, route;
    var directions;
    var baseIcon;
    var points = [];
	var gmarkers = [];
	var count = 0;
	var stopClick = false;
    var url = '#';
    var geocoder = new GClientGeocoder();
    
    
    function addLoadEvent(func) {
	  var oldonload = window.onload;
	  if (typeof window.onload != 'function') {
	    window.onload = func;
	  } else {
	    window.onload = function() {
	      //oldonload();
	      func();
	    }
	  }
	}
	
	function showAddress(address,html) {
	  geocoder.getLatLng(
	    address,
	    function(point) {
	      if (!point) {
	        //alert(address + " not found");
	      } else {
	        var marker = new GMarker(point);
	        map.addOverlay(marker);
	        marker.openInfoWindowHtml(address);        
	      }
	    }
	  );
	}
	
	function showMarkerHtml(address, name,href) {
       //baseIcon.image = '../images/'+image;	   
       var marker = null; 
	   var html = '';
	   if(href!=null){
	   	html += '<a target=\'_blank\' href=\''+href+'\'>'+name+'</a>';
	   }else{
	   	html += '<b>'+name+'</b>'
	   }
	   html += '<br/>'+address;
	   geocoder.getLatLng(
	    address,
	    function(point) {
	      if (!point) {
	        //alert(address + " not found");
	      } else {
	      	marker = new GMarker(point,baseIcon);
	        GEvent.addListener(marker, "click", function() {
	        marker.openInfoWindowHtml(html); });   	       
	        map.addOverlay(marker);	             
	      }
	    }
	  );
             
       return marker;
     }
	
	
	//addLoadEvent(initialize);
	//addLoadEvent(function() {
	//  		initialize(); 
	//	});
    
    function initialize() {
    	
    
      if (GBrowserIsCompatible()) {
     
        reset();
        map = new GMap2(document.getElementById("map_canvas"));

        map.setCenter(new GLatLng(43.4703333, 11.0437592), 9);
        map.setZoom(5);
        map.addControl(new GLargeMapControl());
        map.addControl(new GOverviewMapControl());
    	map.addControl(new GMapTypeControl());
    	map.addControl(new GScaleControl()); 

    	map.setMapType(G_HYBRID_MAP);
    	map.addMapType(G_HYBRID_MAP);
    	map.enableScrollWheelZoom();
    	
        // Add 10 markers to the map at random locations
        var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
        var lngSpan = northEast.lng() - southWest.lng();
        var latSpan = northEast.lat() - southWest.lat();
        
        
        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        baseIcon = new GIcon();
        baseIcon.image = '../images/general/pallone.gif';
        baseIcon.iconAnchor = new GPoint(0, 0);
        baseIcon.infoWindowAnchor = new GPoint(0, 0);
        
        
        //GMap_PostInitialize();
      }
    }
    function reset(){
    	if(directions){
        	directions.clear();
        }            
    }
    
     // Creates a marker whose info window displays the letter corresponding
     // to the given index.
     function createMarker(lat, lng, id, description,image) {
     
	   if(image!=null) {
	    baseIcon.image = '../images/'+image;
	    
	   }
	  
	 

       var point = new GLatLng(lng,lat);
       var marker = new GMarker(point,baseIcon);

       GEvent.addListener(marker, "click", function() {
         marker.openInfoWindow(description);        
       });
       map.addOverlay(marker);

       return marker;
     }
     
      
     
      
     function createMarkerHtml(lat, lng, type, label, address,azienda,rifiuto,image) {
      
       baseIcon.image = null;
	   var html = '<b>'+label+'</b>'; 
	   if(image!=null) html += ' <img src=\'../images/'+image+'\'/> '; 
	   html += '<b>'+type+'</b>';
	   html += '<br/>'+address;
       if(azienda!=null)	html += '<br/>' + '<b>Azienda</b>: '+azienda;
 	   if(rifiuto!=null)	html += '<br/>' + '<b>Rifiuto</b>: '+rifiuto;
       var point = new GLatLng(lng,lat);
       var marker = new GMarker(point,baseIcon);
       marker.content = html;
       GEvent.addListener(marker, "click", function() {
         marker.openInfoWindowHtml(html);         
       });     
       map.addOverlay(marker);	     
       points[gmarkers.length] = point;
       gmarkers.push(marker);
       return marker;
      
     }
     
     function createMarkerHtmlWithImage(lat, lng, type, label, address,azienda,rifiuto,image) {
       baseIcon.image = '../images/'+image;	    
	   var html = '<b>'+label+'</b>'; 
	   if(image!=null) html += ' <img src=\'../images/'+image+'\'/> '; 
	   html += '<b>'+type+'</b>';
	   html += '<br/>'+address;
       if(azienda!=null)	html += '<br/>' + '<b>Azienda</b>: '+azienda;
 	   if(rifiuto!=null)	html += '<br/>' + '<b>Rifiuto</b>: '+rifiuto;
       var point = new GLatLng(lng,lat);
       var marker = new GMarker(point,baseIcon);
       marker.content = html;
       GEvent.addListener(marker, "click", function() {
         marker.openInfoWindowHtml(html);         
       });     
       
       map.addOverlay(marker);	           
       return marker;
     }
     
     var markerGroups = { "storage": [], "weigh": [], "client": [], "server": []};
	
     
     function createMarkerHtmlType(lat, lng, name, label, address,azienda,rifiuto,image,type) {
       baseIcon.image = '../images/'+image;	    
	   var html = '<b>'+label+'</b>'; 
	   if(image!=null) html += ' <img src=\'../images/'+image+'\'/> '; 
	   html += '<b>'+name+'</b>';
	   html += '<br/>'+address;
       if(azienda!=null)	html += '<br/>' + '<b>Azienda</b>: '+azienda;
 	   if(rifiuto!=null)	html += '<br/>' + '<b>Rifiuto</b>: '+rifiuto;
       var point = new GLatLng(lng,lat);
       var marker = new GMarker(point,baseIcon);
       marker.content = html;
       GEvent.addListener(marker, "click", function() {
         marker.openInfoWindowHtml(html);         
       });     
       
       markerGroups[type].push(marker);     
        map.addOverlay(marker);	    
        marker.hide();            
       return marker;
     }
     
     function toggleGroup(type) {
      for (var i = 0; i < markerGroups[type].length; i++) {
        var marker = markerGroups[type][i];
        if (marker.isHidden()) {
          marker.show();
        } else {
          marker.hide();
        }
      } 
    }

/*
	var step = 5; // metres
    var tick = 100; // milliseconds
    var poly;
    var eol;
    var car = new GIcon();
    car.image="../images/lorry.png"
    car.iconSize=new GSize(16,16);
    car.iconAnchor=new GPoint(16,9);
    var marker;
    var k=0;
    var stepnum=0;
    var speed = "";  

	function animate(d) {
       
        if (d>eol) {
          document.getElementById("step").innerHTML = "<b>Viaggio concluso</b>";
          document.getElementById("distance").innerHTML =  "Km: "+d;
          return;
        }
        var p = poly.GetPointAtDistance(d);
        if (k++>=180/step) {
          map.panTo(p);
          k=0;
        }
        marker.setPoint(p);
        document.getElementById("distance").innerHTML =  "Km: "+d+speed;
        if (stepnum+1 < directions.getRoute(0).getNumSteps()) {
          if (directions.getRoute(0).getStep(stepnum).getPolylineIndex() < poly.GetIndexAtDistance(d)) {
            stepnum++;
            var steptext = directions.getRoute(0).getStep(stepnum).getDescriptionHtml();
            document.getElementById("step").innerHTML = "<b>Prossimo:</b> "+steptext;
            var stepdist = directions.getRoute(0).getStep(stepnum-1).getDistance().meters;
            var steptime = directions.getRoute(0).getStep(stepnum-1).getDuration().seconds;
            var stepspeed = ((stepdist/steptime) * 2.24).toFixed(0);
            step = stepspeed/2.5;
            speed = "<br>Velocità corente: " + stepspeed +" kmh";
          }
        } else {
          if (directions.getRoute(0).getStep(stepnum).getPolylineIndex() < poly.GetIndexAtDistance(d)) {
            document.getElementById("step").innerHTML = "<b>Prossimo: Arrivo a destinazione</b>";
          }
        }
        setTimeout("animate("+(d+step)+")", tick);
      }


      GEvent.addListener(directions,"load", function() {
        //document.getElementById("controls").style.display="none";
        poly=directions.getPolyline();
        eol=poly.Distance();
        map.setCenter(poly.getVertex(0),17);
        map.addOverlay(new GMarker(poly.getVertex(0),G_START_ICON));
        map.addOverlay(new GMarker(poly.getVertex(poly.getVertexCount()-1),G_END_ICON));
        marker = new GMarker(poly.getVertex(0),{icon:car});
        map.addOverlay(marker);
        var steptext = directions.getRoute(0).getStep(stepnum).getDescriptionHtml();
        document.getElementById("step").innerHTML = steptext;
        setTimeout("animate(0)",2000);  // Allow time for the initial map display
      });
     
      function startanimate() {
        alert(directions);
        poly=directions.getPolyline();
        eol=poly.Distance();
        map.setCenter(poly.getVertex(0),17);
        map.addOverlay(new GMarker(poly.getVertex(0),G_START_ICON));
        map.addOverlay(new GMarker(poly.getVertex(poly.getVertexCount()-1),G_END_ICON));
        marker = new GMarker(poly.getVertex(0),{icon:car});
        map.addOverlay(marker);
        var steptext = directions.getRoute(0).getStep(stepnum).getDescriptionHtml();
        document.getElementById("step").innerHTML = steptext;
        setTimeout("animate(0)",2000);  // Allow time for the initial map display
      }
      
      function start() {
        //var startpoint = document.getElementById("startpoint").value;
        //var endpoint = document.getElementById("endpoint").value;
        //dirn.loadFromWaypoints([startpoint,endpoint],{getPolyline:true,getSteps:true});
      	directions = new GDirections(map, document.getElementById("panel"));
  		directions.loadFromWaypoints(points,{getPolyline:true,getSteps:true});
     	
      }
*/
	function overlayAnimated(){
		
		
		
		directions = new GDirections(map, document.getElementById("panel"));
  		directions.loadFromWaypoints(points);
  		
		
		
		// Open infowindow of first marker
		//for mozzilla 0
		// for internetexplorer 1?
		//gmarkers[0].openInfoWindowHtml( gmarkers[0].content);
		
		//start animation with delay
		route = setTimeout("anim()", 3600);
    
	}
	
	
	function haltAnim() {
	 if(route) {
	  clearTimeout(route);
	  stopClick = true;
	 }
	}
	
	
	function carryOn() {
	
	 if(stopClick == true) anim();
	 stopClick = false;
	}
	
	
	function anim() {
	
	 count++;
	 if(count < points.length) {
	  map.panTo(points[count]);
	  gmarkers[count].openInfoWindowHtml( gmarkers[count].content);
	  var delay = 3400;
	  if((count+1) != points.length)
	   var dist = points[count].distanceFrom(points[count+1]);
	
	  // Adjust delay
	  if( dist < 10000 ) {
	   delay = 2000;
	  }
	  if( dist > 80000 ) {
	   delay = 4200;
	  }
	  route = setTimeout("anim()", delay);
	 }
	  else {
	  clearTimeout(route);
	  count = 0;
	  route = null;
	 }
	}
	
	
	function playAgain() {
	
	 GUnload();
	 if(route) clearTimeout(route);
	 stopClick = false;
	 count = 0;
	 initialize();
	 overlay();
	}
	
	/*
	 * Returns a new XMLHttpRequest object, or false if this browser
	 * doesn't support it
	 */
	function newXMLHttpRequest() {
	
	  var xmlreq = false;
	
	  if (window.XMLHttpRequest) {
	
	    // Create XMLHttpRequest object in non-Microsoft browsers
	    xmlreq = new XMLHttpRequest();
	
	  } else if (window.ActiveXObject) {
	
	    // Create XMLHttpRequest via MS ActiveX
	    try {
	      // Try to create XMLHttpRequest in later versions
	      // of Internet Explorer
	
	      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
	
	    } catch (e1) {
	
	      // Failed to create required ActiveXObject
	
	      try {
	        // Try version supported by older versions
	        // of Internet Explorer
	
	        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
	
	      } catch (e2) {
	
	        // Unable to create an XMLHttpRequest with ActiveX
	      }
	    }
	  }
	
	  return xmlreq;
	}
	
	function getElementById(id_elemento) {
	 // elemento da restituire
	 var elemento;
	 
	 // se esiste il metodo getElementById
	 // questo if sarà diverso da false, null o undefined
	 // e sarà quindi considerato valido, come un true
	 if(document.getElementById)
	  elemento = document.getElementById(id_elemento);
	 
	 // altrimenti è necessario usare un vecchio sistema
	 else
	  elemento = document.all[id_elemento];
	
	 // restituzione elemento
	 return elemento;
	}
	
	
	// funzione da chiamare immediatamente quando arriva la paginba
	function loadIndicators(url) {
	
	  // variabili di funzione
	  var ajax = newXMLHttpRequest(),
	  
	  // assegnazione elemento del documento
	  eventsBody = getElementById("indicatorsBody"),
	    
	  // risultato booleano di funzione
	  usaLink = true;
	  
	  // se l'oggetto XMLHttpRequest non è nullo
	  if(ajax) {
	    // il link al file non deve essere usato
	    usaLink = false;
	
	    // impostazione richiesta asincrona in GET
	    ajax.open("get", url, true);
	
	    // rimozione dell'header "connection" come "keep alive" 
	    ajax.setRequestHeader("connection", "close");
	
	    // impostazione controllo e stato della richiesta
	    ajax.onreadystatechange = function() {
	      
	      // verifica dello stato
	      if(ajax.readyState === 4) {
	
	        // verifica della risposta da parte del server
	        if(ajax.status === 200)
	          // operazione avvenuta con successo
	
	          eventsBody.innerHTML = ajax.responseText;
	        else {
	
	          // errore di caricamento
	          eventsBody.innerHTML = "Impossibile effettuare l'operazione richiesta.<br />";
	          eventsBody.innerHTML += "Errore riscontrato: " + ajax.status;
	
	        }
	      } 
	    }
	
	    // invio richiesta
	    ajax.send(null);
	  }
	   
	  return usaLink;
	}
