// Fix IE
if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
  }
}

var gMap;
var arrMapMarkers = Array();
var curIdPage = 2;
var totBoxPers;
var boxToChange;
var boxNum;
var boxTochangeTop;
var boxToMoveDown = Array();
var boxPersCollapsedHeight = 60;
var boxPersExpandedHeight = 100;
var boxPersInitImage;

/*
 * Gestione mappe google
 *
 **/


function initializeGMap(pinLocations, targetDiv ) {

  if (GBrowserIsCompatible()) {

    // Crea mappa
    var latlng = new google.maps.LatLng(43.551099, 11.586199);

    // crea la mappa
    gMap = new google.maps.Map2(document.getElementById(targetDiv));

    //
    gMap.setCenter(latlng, 7);

    // setta l'interfaccia della mappa (senza non si vedono lo zoom, il navigatore, eccetera)
    gMap.setUIToDefault();
    setMarkers(pinLocations);

   
  }
  else {
    alert ("Browser non compatibile");
  }

}

function addMarkerOnMap(point, i, pointTitle, htmlText)
{
        var icon3 = new GIcon();
        icon3.image = '../grafica/pin-googlemap.png';
        icon3.infoWindowAnchor = new GPoint(15, 40);
        icon3.iconAnchor = new GPoint(0, 0);
        //    icon3.infoWindowAnchor = new GPoint(0, 0);
        //    icon3.iconAnchor = new GPoint(15, 40);
        icon3.iconSize  = new GSize(38,29);


        markerOption = {
          icon: icon3
          // , shape: shape
          ,
          title: pointTitle
        //, zIndex: location[2]
        };

        arrMapMarkers[i] = new GMarker(point, markerOption);
        GEvent.addListener(arrMapMarkers[i], 'click', function() {
          infoWin = gMap.getInfoWindow();
          arrMapMarkers[i].openInfoWindowHtml(htmlText);
        });

        gMap.addOverlay(arrMapMarkers[i]);
}

/**
 * Funzione closure per la gestione delle risposte asincrone
 * data dalla funzione di risoluzione della longitudine e latitudine
 * a partire dall'indirizzo.
 * @param address - indirizzo usato per risolvere le coordinate geografiche
 * @param i - indice del marcatore nella variabile generale array
 * @param text - parametro della funzione openInfoWindowHtml
 */
function setPointMarker(address,i, pointTitle, htmlText) {
  // alert (text);
  var geocoder = new GClientGeocoder();



  geocoder.getLatLng(
    address,
    function(point) {
      //alert("mi");
      if (point) {

            addMarkerOnMap(point, i, pointTitle,htmlText);
      }
      else
        {
          alert("couldn't find: " + address);
        }
    }
    );
}

/**
 * Date le locazioni, posiziona i marcatori sulla mappa.
 * @param locations vettore
 */
function setMarkers(locations) {

  for (var i = 0; i < locations.length; i++) {

    var title = locations[i][3];
    var htmlText = locations[i][4];

    if (locations[i][0] >0 && locations[i][1] > 0 )
      {
        var point = new GLatLng(locations[i][0],locations[i][1]);
        addMarkerOnMap(point, i, title,htmlText)


      }
      else {

          var address = locations[i][6] +', '+locations[i][7]+', '+locations[i][8]+', '+locations[i][5];
          var country = locations[i][3];
          setPointMarker(address, i, title,htmlText);
      }

  }
}


