function Tabset (container) {
    container = $(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.id = page.get('id') || false;

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

                if(this.events.active) {
                    this.events.active.call(this);
                }
            }
        };

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

                if(this.events.inactive) {
                    this.events.inactive.call(this);
                }
            }
        };

        this.addEvent = function (when, fn) {
            if (when === 'active') {
                this.events.active = fn;
            } else if ( when === 'inactive') {
                this.events.inactive = fn;
            }
        };

        this.events = {
            'active': false,
            'inactive': false
        };
    };

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

    this.getPair = function (id) {
        try {
            return this.pairs.filter( function(p) { return p.id === 'map'; } )[0];
        } catch (e) {
            return false;
        }
    };

    this.pairs[0].activate();

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


function tabify (tabsets) {
    return $$(tabsets).map(
        function(item, index, array){
            return new Tabset(item);
        });
};

