var side_bar_html = "";
var gmarkers = [];
var htmls = [];
var i = 0;

function loadMap() {
   if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(lat,long),zoomLevel);
		map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());

        map.addControl(new GOverviewMapControl(new GSize(110, 110)));
        map.enableDoubleClickZoom();
        map.enableContinuousZoom();
        map.enableScrollWheelZoom();
        // gdir = new GDirections(map);
        //GEvent.addListener(gdir, "load", onGDirectionsLoad);
        //GEvent.addListener(gdir, "error", handleErrors);
        
        // creo i marker  se  perņ la lista e  vuto  prendo il setting   di  default
        if(officeList!=''){
            ma_setMarker()
            ma_setSibeBar()
        }
        else{
           point = new GLatLng(lat, long);
           map.addOverlay(createMarker(point,Indirizzo));
        }
     }
 }
 
function  ma_setSibeBar(){
      if(document.getElementById("mapBar"))document.getElementById("mapBar").innerHTML = side_bar_html;
}
 
function ma_setMarker(){
     var curPoint;
     var bounds = new GLatLngBounds();
     for (var i in officeList) {
          objPlaces=officeList[i]["places"]
          for (var j in objPlaces){
              curPoint="point"+j
              curPoint = new GLatLng(objPlaces[j]["posn"][0],objPlaces[j]["posn"][1]);
              htmlInfo="<b>"+objPlaces[j]["name"]+"</b><br>"+objPlaces[j]["address"]
              map.addOverlay(createMarker(curPoint,htmlInfo,j));
              bounds.extend(curPoint);
          }
      }
      map.setZoom(map.getBoundsZoomLevel(bounds));
      // ===== determine the centre from the bounds ======
      map.setCenter(bounds.getCenter());
 }
  
 
 //direzione
  
 function setDirections(fromAddress, toAddress) {
	  map.removeOverlay(point);
	  
      gdir.load("from: " + fromAddress + " to: " + toAddress);
 }

 function handleErrors(e){
	   alert('Indirizzo non corretto o sconosciuto')
 }

 function onGDirectionsLoad(){ 
         // Use this function to access information about the latest load()
          // results.

          // e.g.
	  // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
 }
   
 // Creates a marker at the given point with the given number label
 function createMarker(point,text,i) {
      // Create our "tiny" marker icon
      var blueIcon = new GIcon(G_DEFAULT_ICON);
      blueIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/marker1.png";
	   // Set up our GMarkerOptions object
	  markerOptions = { icon:blueIcon };

      var marker = new GMarker(point);
         GEvent.addListener(marker, "click", function() {
         marker.openInfoWindowHtml(text);
      });
      gmarkers[i] = marker;
      htmls[i] = text;
      //  crea  anche la  side  bar
      side_bar_html += '<a href="javascript:goToMap(' + i + ')">' + text + '<\/a><br><br>';
      return marker;
  }
 //   zomma   sul punto
 function goToMap(i) {
    panZoomLevel=zoomLevel
    gmarkers[i].openInfoWindowHtml(htmls[i]); 
    //map.setZoom (panZoomLevel);
 }

 
 //carico la mappa
    $(document).ready(function() {
       loadMap();
 });
    
 

