// Wax for Google Maps API v3
// --------------------------

// Wax header
var wax = wax || {};
wax.g = wax.g || {};

// Wax Google Maps MapType: takes an object of options in the form
//
//     {
//       name: '',
//       alt: '',
//       minZoom: 0,
//       minZoom: 18,
//       baseUrl: 'a url',
//     }
wax.g.MapType = function(options) {
    options = options || {};
    this.name = options.name || '';
    this.alt = options.alt || '';
    this.maxZoom = options.maxZoom || 18;
    this.minZoom = options.minZoom || 0;
    this.baseUrl = options.baseUrl ||
        'http://a.tile.mapbox.com/1.0.0/world-light';
    this.blankImage = options.blankImage || '';

    // non-configurable options
    this.interactive = true;
    this.tileSize = new google.maps.Size(256, 256);
};

// Get a tile element from a coordinate, zoom level, and an ownerDocument.
wax.g.MapType.prototype.getTile = function(coord, zoom, ownerDocument) {
  return $('<div></div>')
        .addClass('interactive-div-' + zoom)
        .width(256).height(256).append(
            $('<img />').attr('src', this.getTileUrl(coord, zoom))
            .width(256).height(256))[0];
};

// Remove a tile that has fallen out of the map's viewport.
//
// TODO: expire cache data.
wax.g.MapType.prototype.releaseTile = function(tile) {
    $(tile).remove();
};

// Get a tile url, based on x, y coordinates and a z value.
wax.g.MapType.prototype.getTileUrl = function(coord, z) {
    // Y coordinate is flipped in Mapbox, compared to Google
    var mod = Math.pow(2, z),
        y = (mod - 1) - coord.y,
        x = (coord.x % mod);
        x = (x < 0) ? (coord.x % mod) + mod : x;
    return (y >= 0)
        ? (this.baseUrl + '/' + z + '/' + x + '/' + y + '.png')
        : this.blankImage;
};

@@ -777,16 +777,63 @@
 6, 256);
+%0A%0A    // DOM element cache%0A    this.cache = %7B%7D;
 %0A%7D;%0A%0A// 
@@ -979,22 +979,106 @@
 nt) %7B%0A  
-return
+  var key = zoom + '/' + coord.x + '/' + coord.y;%0A    this.cache%5Bkey%5D = this.cache%5Bkey%5D %7C%7C
  $('%3Cdiv
@@ -1167,122 +1167,262 @@
 256)
-.append(%0A            $('%3Cimg /%3E').attr('src', this.getTileUrl(coord, zoom))%0A            .width(256).height(256))%5B0
+%0A        .data('gTileKey', key)%0A        .append(%0A            $('%3Cimg /%3E')%0A            .width(256).height(256)%0A            .attr('src', this.getTileUrl(coord, zoom))%0A            .error(function() %7B $(this).hide() %7D)%0A        )%5B0%5D;%0A    return this.cache%5Bkey
 %5D;%0A%7D
@@ -1513,16 +1513,35 @@
 che data
+ in the gridmanager
 .%0Awax.g.
@@ -1585,24 +1585,111 @@
 ion(tile) %7B%0A
+    var key = $(tile).data('gTileKey');%0A    this.cache%5Bkey%5D && delete this.cache%5Bkey%5D;%0A
     $(tile).

Add DOM element cache for image tile elements in g connector.