fb.context = {

    tabs: null,
    content: null,
    slots: {},
    selected: null,
    observers: [],
    panelClass: 'full',

    init: function() {
        debug('fb.context.init');

        fb.observers.register('context', 'switch');
        fb.observers.register('context', 'activate');


        $('#side .switch').click(this.switchPanel);

        this.content = $('#side .tab-contents');
        this.content.children('.tab-content').each(function() {
            fb.context.slots[this.id.substr(4)] = $(this)
        });

        this.tabs = $('#context-tabs');
        this.tabs.find('li a').click(function() {
            var tab = $(this).attr('href').substr(5);
            fb.context.activate(tab);
            return false;
        });
    },

    getSlot: function(slot) {
        return this.slots[slot];
    },

    switchPanel: function() {
        $('#content').toggleClass('full')
                     .toggleClass(fb.context.panelClass);
        fb.observers.emit('context', 'switch');
        $('#images').trigger('scroll');
        $(window).trigger('resize');
    },

    activate: function(tab) {
        debug('fb.context.activate: tab = ' + tab);
        if (!this.slots[tab]) {
            return;
        }

        this.tabs.find('li.selected').removeClass('selected');

        $.each(this.slots, function() { $(this).hide() });
        this.slots[tab].show();
        this.tabs.find('li a[href=#tab-' + tab + ']').parent().addClass('selected');
        this.selected = tab;
        fb.observers.emit('context', 'activate', { tab: tab });
        return this;
    }
}
