var Geoportal = {
    GeoRM: {},

    Util: {
        getImagesLocation: function() {
            return "http://api.ign.fr/geoportail/api/js/1.0beta4/theme/geoportal/img/";
        }
    },

    cgl: function(bounds, extent, resolution) {
        var tilelon = resolution * this.tileSize.w;
        var tilelat = resolution * this.tileSize.h;

        var offsetlon = bounds.left - this.gridOrigin.lon;
        var tilecol = Math.floor(offsetlon/tilelon) - this.buffer;
        var tilecolremain = offsetlon/tilelon - tilecol;
        var tileoffsetx = -tilecolremain * this.tileSize.w;
        var tileoffsetlon = this.gridOrigin.lon + tilecol * tilelon;

        var offsetlat = bounds.top - (this.gridOrigin.lat + tilelat);
        var tilerow = Math.ceil(offsetlat/tilelat) + this.buffer;
        var tilerowremain = tilerow - offsetlat/tilelat;
        var tileoffsety = -tilerowremain * this.tileSize.h;
        var tileoffsetlat = this.gridOrigin.lat + tilerow * tilelat;

        return {
          tilelon: tilelon, tilelat: tilelat,
          tileoffsetlon: tileoffsetlon, tileoffsetlat: tileoffsetlat,
          tileoffsetx: tileoffsetx, tileoffsety: tileoffsety
        };
    },

    gfrs: function(newParams, altUrl) {
        var projectionCode = this.map.getProjection();
        this.params.SRS = (projectionCode == "none") ? null : projectionCode;
        if(Geoportal.GeoRM) {//IGN
          OpenLayers.Util.extend(newParams, Geoportal.GeoRM.token);
        }
        return OpenLayers.Layer.Grid.prototype.getFullRequestString.apply(
                                                    this, arguments);
    },

    moveTo: function(bounds, zoomChanged, dragging) {
        if(Geoportal.GeoRM) {
            if(!Geoportal.GeoRM.getToken()) {
                return;
            }
        }
        OpenLayers.Layer.Grid.prototype.moveTo.apply(this, arguments);
    }
};
