wax = wax || {};
wax.g = wax.g || {};

// Adds legends to a google Map object.
wax.g.legend = function(map, options) {
    options = options || {};
    var legend = {
        add: function() {
            var url;
            this.legend = new wax.Legend(map.getDiv(), options.container);
            // Ideally we would use the 'tilesloaded' event here. This doesn't seem to
            // work so we use the much less appropriate 'idle' event.
            google.maps.event.addListener(map, 'idle', wax.util.bind(function() {
                if (url) return;

                // Get a tile URL for each relevant layer, from which legend URLs
                // are derived.
                url = [];
                for (var i in map.mapTypes) {
                    if (!map.mapTypes[i].interactive) continue;
                    var mapType = map.mapTypes[i];
                    for (var key in mapType.cache) {
                        url.push(mapType.cache[key].src);
                        break;
                    }
                };
                url.length && this.legend.render(url);
            }, this));
            return this;
        }
    };
    return legend.add(map);
};


@@ -32,16 +32,52 @@
 %7C%7C %7B%7D;%0A%0A
+// Legend Control%0A// --------------%0A
 // Adds 
@@ -132,21 +132,16 @@
 unction(
-map, 
 options)
@@ -185,30 +185,65 @@
 ar l
-egend = %7B%0A        add:
+, // parent legend%0A        legend = %7B%7D;%0A%0A    legend.add =
  fun
@@ -264,322 +264,119 @@
     
-    var url;%0A            this.legend = new
+l =
  wax.
-L
+l
 egend(
-map.getDiv(), options.container);%0A            // Ideally we would use the 'tilesloaded' event here. This doesn't seem to%0A            // work so we use the much less appropriate 'idle' event.%0A            google.maps.event.addListener(map, 'idle', wax.util.bind(
+)%0A            .content(options.legend %7C%7C '');%0A        return this;%0A    %7D;%0A%0A    legend.element = 
 func
@@ -396,600 +396,126 @@
     
-        if (url) return;%0A%0A                // Get a tile URL for each relevant layer, from which legend URLs%0A                // are derived.%0A                url = %5B%5D;%0A                for (var i in map.mapTypes) %7B%0A                    if (!map.mapTypes%5Bi%5D.interactive) continue;%0A                    var mapType = map.mapTypes%5Bi%5D;%0A                    for (var key in mapType.cache) %7B%0A                        url.push(mapType.cache%5Bkey%5D.src);%0A                        break;%0A                    %7D%0A                %7D;%0A                url.length && this.legend.render(url);%0A            %7D, this));%0A    
+return l.element();%0A    %7D;%0A%0A    legend.appendTo = function(elem) %7B%0A        wax.util.$(elem).appendChild(l.element());%0A
     
@@ -535,25 +535,16 @@
 is;%0A
-        %7D%0A
     %7D;%0A
+%0A
     
@@ -565,14 +565,10 @@
 add(
-map
 );%0A%7D;%0A
-%0A

Update legend control for Google Maps.