/**
 * Fonctions personnalisées pour idresto, sur le traitement des cartes GoogleMaps
 *
 * /

/**
 * Calcule automatiquement le centre et le zoom approprié pour une zone donnée
 */
 GMap2.prototype.centerAndZoomOnBounds = function(bounds) {
     var largerBounds = bounds.extendByRatio(0.05);
     var center_lat =
         (largerBounds.getNorthEast().lat() +
         largerBounds.getSouthWest().lat())
         / 2.0;
     var center_lng =
         (largerBounds.getNorthEast().lng() +
         largerBounds.getSouthWest().lng())
         / 2.0;
     var zoom = map.getBoundsZoomLevel(largerBounds);
     map.setCenter(new GLatLng(center_lat, center_lng), zoom)
 }

/**
 * Elargi la zone donnée selon un ratio
 */
 GLatLngBounds.prototype.extendByRatio = function(ratio) {
     // On prend les mêmes tailles que la zone originale
     var largerBounds = new GLatLngBounds(
         this.getSouthWest(), this.getNorthEast());
     // récupère lat, lng du nord est et du sud ouest
     var northEastLat = this.getNorthEast().lat();
     var northEastLng = this.getNorthEast().lng();
     var southWestLat = this.getSouthWest().lat();
     var southWestLng = this.getSouthWest().lng();
     var diffLat = northEastLat - southWestLat;
     var diffLng = northEastLng - southWestLng;
     // on multiplie le tout avec le ration
     northEastLat += diffLat * ratio;
     southWestLat -= diffLat * ratio;
     northEastLng += diffLng * ratio;
     southWestLng -= diffLng * ratio;
     // on étend le nord est
     largerBounds.extend(new GLatLng(northEastLat, northEastLng));
     // on étend le sud ouest
     largerBounds.extend(new GLatLng(southWestLat, southWestLng));
     return largerBounds;
 }


/**
 * Contrôle qu'on reste bien dans la zone définie. Si on dépasse la limite, on est ramené à celle-ci
 */
function checkBounds() {
    if (allowedBounds.contains(map.getCenter())) {
        return;
    }
    var C = map.getCenter();
    var X = C.lng();
    var Y = C.lat();

    var AmaxX = allowedBounds.getNorthEast().lng();
    var AmaxY = allowedBounds.getNorthEast().lat();
    var AminX = allowedBounds.getSouthWest().lng();
    var AminY = allowedBounds.getSouthWest().lat();

    if (X < AminX) {
        X = AminX;
    }
    if (X > AmaxX) {
        X = AmaxX;
    }
    if (Y < AminY) {
        Y = AminY;
    }
    if (Y > AmaxY) {
        Y = AmaxY;
    }
    map.setCenter(new GLatLng(Y,X));
}

/**
 * Supprimer tous les marqueurs de la carte
 */
function clearMap(){
    idr_initMap();
    if (actual_resto != null) marker[actual_resto].closeExtInfoWindow(map);
    map.clearOverlays();
}


/**
 * Initialise l'affichage de la carte
 */

function idr_initMap() {
    if (initialized == false){

        $('#gmap_map').css("background",'#fff');
        max_map_height = null;
        resize_content();
        initialize();
        $('#quartiers').hide();
    }
    else
        resize_content();
}