// Requires prototype.js
function tabify(element, tab_content_pairs)
// element = tab container/parent element
// tab_content_pairs = JSON, whose key-value pairs are tab_id -> content_id
//
// tabify will store the tab_conent_pairs in the parent element
// User needs to select default tab after tabifying. (To be fixed later.)
{
    element = $(element);
    element.tab_pairings = tab_content_pairs;
    children = element.childElements();
    found_default_tab = false;
    for(i = 0; i < children.length; i++)
    {
	children[i].onclick = function() {
	    select_tab(this);
	}
	children[i].tab_container = element;
	
	if(!found_default_tab && children[i].hasClassName('selected'))
	{
	    element.selected_tab = children[i];
	}
    }
}

function select_tab(tab)
// tab = tab to be selected
{
    container = tab.tab_container;
    oldtab = container.selected_tab;
    if (oldtab != null)
    {
	oldtab.toggleClassName('selected');
	$(container.tab_pairings[oldtab.id]).toggleClassName('selected');
    }
    tab.toggleClassName('selected');
    container.selected_tab = tab;
    $(container.tab_pairings[tab.id]).toggleClassName('selected');
}