/**
 * Inizializza la google map
 * @param latitude
 * @param longitude
 * @param zoom
 * @param location
 */
function initMap(mapElement, latitude,longitude,zoom,location) {
    map = new GMap2(mapElement);
    map.addControl(new GMapTypeControl());
    map.addControl(new GSmallMapControl());
    //map.addControl(new GOverviewMapControl());

    if (latitude != null && longitude != null) {
        var point = new GLatLng(latitude,longitude);
                  // Center the map on this point
        map.setCenter(point, zoom);
    } else {
        var geocoder = new GClientGeocoder();
        geocoder.getLocations(location, function( response ) {
            // Retrieve the object
            var place = response.Placemark[0];

                      // Retrieve the latitude and longitude
            var point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);

                      // Center the map on this point
            map.setCenter(point,zoom );
        } );
    }
    checkMapStatus();

}

function initBigMap(latitude,longitude,zoom,location) {

    map = new GMap2(document.getElementById("map"));
    map.addControl(new GMapTypeControl());
    map.addControl(new GLargeMapControl());
    map.addControl(new GOverviewMapControl());

    if (latitude != null && longitude != null) {
        var point = new GLatLng(latitude,longitude);
                  // Center the map on this point
        map.setCenter(point, zoom);
    } else {
        var geocoder = new GClientGeocoder();
        geocoder.getLocations(location, function( response ) {
            // Retrieve the object
            var place = response.Placemark[0];

                      // Retrieve the latitude and longitude
            var point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);

                      // Center the map on this point
            map.setCenter(point,zoom );
        } );
    }
    checkMapStatus();

}



/**
 * Si assicura che la mappa di google sia arrivata
 */
function checkMapStatus() {
    if (map.isLoaded())
        loadPoints();
    else window.setTimeout("checkMapStatus()",1000);
}

function loadApartmentDetailPoint(id,idOwnerInstance,latitude,longitude,address,contextPath) {
   var location = new Array();
   location["marker"] = new GMarker(new GLatLng(latitude,longitude));
   location["description"] = '<table cellpadding="0" cellspacing="0">' +
                                 '<tr><td>' +
                                 '<b  style="text-transform:uppercase;"><a href="'+contextPath+'/search/ad.action?aid='+id+'&bid='+idOwnerInstance+'">'+address+'</a></b>'+
                                 '</td></tr>' +
                             '</table>';
   GEvent.addListener( location["marker"], 'click', function() {
      showOnMap(id+"_"+idOwnerInstance);
   });
   locations[id+"_"+idOwnerInstance] = location;
   map.addOverlay( location["marker"] );

}

function loadSimilarApartmentPoint(id,idOwnerInstance,latitude,longitude,markerIconUrl,address,contextPath) {
   var tinyIcon = new GIcon();
   tinyIcon.image = markerIconUrl;
   tinyIcon.image = markerIconUrl;
   tinyIcon.iconAnchor = new GPoint(6, 20);
   tinyIcon.infoWindowAnchor = new GPoint(5, 1);

   // Set up our GMarkerOptions object literal
   markerOptions = { icon:tinyIcon };

   var location = new Array();
   location["marker"] = new GMarker(new GLatLng(latitude,longitude),markerOptions);
   location["description"] = '<table cellpadding="0" cellspacing="0">' +
                                 '<tr><td>' +
                                 '<b  style="text-transform:uppercase;"><a href="'+contextPath+'/search/ad.action?aid='+id+'&bid='+idOwnerInstance+'">'+address+'</a></b>'+
                                 '</td></tr>' +
                             '</table>';
   GEvent.addListener( location["marker"], 'click', function() {
      showOnMap(id+"_"+idOwnerInstance);
   });
   locations[id+"_"+idOwnerInstance] = location;
   map.addOverlay( location["marker"] );

}


function loadExtendedApartmentPoint(id,idOwnerInstance,latitude,longitude,address,contextPath,
                                    apartmentTitle, postiLetto, urlPhoto, camereLetto, shortDescription, onlineBooking, btnLabel, postiLettoLbl, camereLbl) {
   var location = new Array();
   location["marker"] = new GMarker(new GLatLng(latitude,longitude));
   var descr = '';
   descr +=    '<table width="380px" border="0" cellspacing="3" cellpadding="2" id="fumetto">' +
	                                '<tr>' +
		                                '<td colspan="3" class="titoloFumetto"><a href="' + contextPath + '/search/ad.action?aid=' + id + '&bid=' + idOwnerInstance + '">' + address + '</a></td>' +
	                                '</tr>' +
	                                '<tr>' +
		                                '<td style="border-bottom:1px solid #ccc"><strong>'+ postiLettoLbl +'</strong>:' + postiLetto + '</td>' +
		                                '<td width="112" rowspan="3" valign="top"><a href="' + contextPath + '/search/ad.action?aid=' + id + '&bid=' + idOwnerInstance + '"><img src="' + urlPhoto + '" width="146" height="98" class="opacityit photo"/></a></td>' +
	                                '</tr>' +
	                                '<tr>' +
		                                '<td style="border-bottom:1px solid #ccc;"><strong>'+ camereLbl +'</strong>:' + camereLetto + '</td>' +
	                                '</tr>' +
	                                '<tr>' +
		                                '<td>' + shortDescription +
		                                '<form action="'+ contextPath +'/booking/book.action">'+
                '<input type="hidden" value="' + id + '" name="apartmentKey.id"/>' +
                '<input type="hidden" value="'+ idOwnerInstance +'" name="apartmentKey.idOwnerInstance"/>';
                if(onlineBooking == 1) {
                    descr += '<input type="hidden" name="type" value="book_online">' +
                    '<input type="submit" value="' + btnLabel + '" class="btn-prenota">'
                }
                else {
                    descr += '<input type="hidden" name="type" value="request">';
                }

                  descr +=   '<input type="submit" value="' + btnLabel + '" class="btn-prenota">'

                descr += '</form>' +
		                                '</td>' +
	                                '</tr>' +
                                '</table>';
   location["description"] = descr;
   GEvent.addListener( location["marker"], 'click', function() {
      showOnMap(id+"_"+idOwnerInstance);
   });
   locations[id+"_"+idOwnerInstance] = location;
   map.addOverlay( location["marker"] );

}


/**
 * Mostra il blob del marker sulla mappa
 * @param locationId
 */
function showOnMap( locationId  ) {
    window.location = '#1';
    if(!mapIsOpen) toggleMap(true);
    var location = locations[locationId];
    location["marker"].openInfoWindowHtml( location["description"] );
}
