function tabify(tabsets){
    $$(tabsets).each(
        function(container){
            var tabs  = container.getElements('li.tab');
            var pages = container.getElements('li.page');
            var activePair;

            var Pair = function (tab, page) {
                tab.addEvent('click', function() {
                    this.activate();
                    return false;
                }.bind(this));

                this.activate = function () {
                    if (this != activePair) {
                        tab.addClass('activeTab');
                        page.addClass('activePage');
                        if (activePair) activePair.deactivate();
                        activePair = this;
                    }
                };

                this.deactivate = function () {
                    if (this == activePair) {
                        tab.removeClass('activeTab');
                        page.removeClass('activePage');
                    }
                };
            };

            var pairs = tabs.map( function(tab, index) {
                var page = pages[index];
                return new Pair(tab, page);
            });

            pairs[0].activate();

            if (Browser.Engine.trident4){
                // Internet Explorer 6
                tabs.addEvents({
                    'mouseenter': function(e){
                        this.addClass('hover');
                    },
                    'mouseleave': function(e){
                        this.removeClass('hover');
                    }
                });
            }
        });

};

window.addEvent('domready', tabify.pass('div.tabContainer'));
