var __CONTAINERS_TABBED_AJAX = new Object;

var ContainerTabbedAjaxCollection = {
    containers: {},

    addContainer: function(id) {
        this.containers[id] = new ContainerTabbedAjax(id);
    },

    getContainer: function(id, autocreate) {
        if (!this.containers[id] && autocreate) {
            this.addContainer(id);
        }

        return this.containers[id];
    }
};

var ContainerTabbedAjax = Class.create();

ContainerTabbedAjax.prototype = {
    id: false,
    tabs: {},
    current_tab_id: false,
    element_id: false,

    initialize: function(id) {
        this.updateOptions(arguments[0] || {});
        this.id = id;
        this.element_id = 'container_tabbed_ajax_content_' + this.id;
    },

    updateOptions: function(options) {
        this.options = Object.extend(
                            {'onselect' : false},
                            (options));
    },

    selectTab: function(tab_id) {
        if (this.tabs[tab_id] && tab_id != this.current_tab_id) {
            var element_id = this.element_id;

            var overlay = new Avalon.Ajax.Overlay(element_id);

            var updater = function(transport, result) {
                Element.update(element_id, result);
                overlay.remove();
            }

            var tabs = document.getElementsByClassName('control-container-tabbed-tab-' + this.id);

            for (var i = 0; i < tabs.length; i++) {
                tabs[i].removeClassName('control-container-tabbed-tab-selected');
            }

            overlay.show();

            if (this.options.onselect)
                this.options.onselect(this.tabs[tab_id]);

            this.tabs[tab_id].render(null, updater);

            this.current_tab_id = tab_id;
        }
    },

    addTab: function(block, block_params, tab_id) {
        this.tabs[tab_id] = new ContainerTabbedAjaxTab(block, block_params, tab_id);

        if (!this.current_tab_id
            && Element.hasClassName(
                    'control_container_tabbed_block_id_' + tab_id,
                    'control-container-tabbed-tab-selected'))
        {
            this.current_tab_id = tab_id;
        }
    },

    reloadCurrentTab: function(block_params) {
        var element_id = this.element_id;

        var overlay = new Avalon.Ajax.Overlay(element_id);

        var updater = function(transport, result) {
            Element.update(element_id, result);
            overlay.remove();
        }

        overlay.show();

        this.tabs[this.current_tab_id].render(block_params, updater);
    },

    getCurrentTab: function() {
        return this.tabs[this.current_tab_id];
    }
}

var ContainerTabbedAjaxTab = Class.create();
ContainerTabbedAjaxTab.prototype = {
    initialize: function(block, block_params, tab_id) {
        this.block = block;
        this.block_params = block_params;
        this.tab_id = tab_id;
    },

    render: function(block_params, updater) {
        var tab = $("control_container_tabbed_block_id_" + this.tab_id);

        tab.addClassName('control-container-tabbed-tab-selected');

        this.block_params = $H(this.block_params).merge(block_params || {});

        oAvalonRPC.remoteRender_async(
            updater, this.block, this.block_params
        );
    },

    getParams: function() {
        return this.block_params;
    }
}

