function createTabs() {
    forEach(['tabs','graphSwitcher'], function(tabs_class) {
        forEach(getElementsByTagAndClassName('DIV', tabs_class), function(head) {
            var i = 0;
            forEach(getElementsByTagAndClassName("A", null, head), function (a) {
                //body_id = $(a.href.replace(/.*#(.+)$/, '$1')).id;
                body_id = a.href.replace(/.*#(.+)$/, '$1');
                setNodeAttribute(a, 'onclick', "return showTab('"+head.id+"', '"+body_id+"');");
                i++;
            });
        });
    });
}

var cur_active_tabs = {};
function showTab(head_id, body_id, auto) {
    var body_active, a_active;

    if (!auto) cur_active_tabs[head_id] = body_id;
    else if (!body_id) body_id = cur_active_tabs[head_id];

    forEach(getElementsByTagAndClassName("A", null, head_id), function(a) {
        var href = getNodeAttribute(a, 'href');
        var id = href.replace(/.*#(.+)$/, '$1');
        var body = $(id);
        if (id != body_id) {
            if (body) setStyle(body, {'display':'none'});
            removeElementClass(a, 'selected');
        } else {
            body_active = body;
            addElementClass(a, 'selected');
            a_active = a;
        }
    });
    if (body_active) setStyle(body_active, {'display':'block'});
    if (a_active) {
        var exec = getNodeAttribute(a_active, 'exec');
        if (exec) eval(exec);
    }
    return false;
}

function getActiveTab(head_id) {
    var active_tab_id = '';
    forEach(getElementsByTagAndClassName("A", null, head_id), function(a) {
        if (hasElementClass(a, 'selected')) {
            var href = getNodeAttribute(a, 'href');
            var id = href.replace(/.*#(.+)$/, '$1');
            active_tab_id = id;
        }
    });
    return active_tab_id;
}

addLoadEvent(createTabs);
