jQuery Tools - overlay test

I wanted to see if I could set up open close triggers for an overlay even if the button which launched the overlay was recreated.

by EveryoneMustWin

HTML

<script src="http://cdn.jquerytools.org/1.2.7/full/jquery.tools.min.js"></script>
<div id="container">

</div>
<button class='modalLaunch' rel='#modal'>
    Uncontained launch button
</button>
<div id="modal">
    Modal
    <button id="rebuildContainer">Rebuild container</button>
</div>

CSS

#modal {
    width: 100px,
    height: 100px
}

JavaScript

$(document).ready(function() {
    $("#modal").hide();

    var rebuildContainer = function() {
        $("#container").html("<button class='modalLaunch' rel='#modal'>Contained launch button 1</button><br><button class='modalLaunch' rel='#modal'>Contained launch button 2</button>");           

        var triggers = $(".modalLaunch").overlay({
            mask: {
                color: '#bbbbee',
                loadSpeed: 200,
                opacity: 0.9
            },
            closeOnClick: true,
            load: false
        });
    }

    $("#rebuildContainer").click(function() {
        console.log("rebuilding buttons inside the container div");
        
        $(".modalLaunch").each(function() {
        
            var o = $(this).overlay();
            if (o) {
                console.log("there is an overlay associated with this button");
                if (o.isOpened()) {
                        // I would expect this to be true once each time the handler is called
                        console.log("the overlay associated with this button is open");
                }
            }
        });
        
        $(".modalLaunch").overlay().close();
        rebuildContainer();
    });

    rebuildContainer();
});