function tabSwitch(tabarea, tab_count, active_tab_number)
{
  // Active tab is null if this is the first ever time the page is loaded
  // by this user. Default to tab 1---which should be Greenstone 3.
  if (active_tab_number == null)
  {
    active_tab_number = 1;
  }      
	//alert("tabSwitch(" + tabarea + "," + tab_count + "," + active_tab_number + ")");

    for (var i = 1; i < tab_count + 1; i++)
    {
	if (i != active_tab_number)
	{
            document.getElementById('t' + tabarea + '_' + i).className = '';
            document.getElementById('tc' + tabarea + '_' + i).style.display = 'none';
	    _tabTOCEntries('tc' + tabarea + '_' + i, false);
        }
    }
    document.getElementById('t' + tabarea + '_' + active_tab_number).className = 'active';
    document.getElementById('tc' + tabarea + '_' + active_tab_number).style.display = 'block';
    _tabTOCEntries('tc' + tabarea + '_' + active_tab_number, true);
    // Store the default tab for later page loads
    _setCookie('dokuwiki_gs_default_tab', active_tab_number, 365);
}

function initTabArea()
{
    // Get the default tab cookie
    var active_tab_number = _getCookie("dokuwiki_gs_default_tab");

    // Switch tabs appropriately
    var tab_area_counter = 1;
    var possible_tab_area_id = 't' + tab_area_counter;
    // Sometimes - for unknown reasons - t1 is not defined, so lets just try
    // and update for the first tab tabs
    for (tab_area_counter = 1; tab_area_counter < 5; tab_area_counter++)
    {
      if (document.getElementById('t' + tab_area_counter) != null)
      {
        var tab_count = 0;
        var possible_tab_id = 't' + tab_area_counter + '_' + (tab_count + 1);
        while (document.getElementById(possible_tab_id) != null)
        {
            tab_count++;
            possible_tab_id = possible_tab_area_id + '_' + (tab_count + 1);
        }
        tabSwitch(tab_area_counter, tab_count, active_tab_number);
      }
    }
    // Carry on while we can still find more tabs
    while (document.getElementById('t' + tab_area_counter) != null)
    {
        var tab_count = 0;
        var possible_tab_id = 't' + tab_area_counter + '_' + (tab_count + 1);
        while (document.getElementById(possible_tab_id) != null)
        {
            tab_count++;
            possible_tab_id = possible_tab_area_id + '_' + (tab_count + 1);
        }
        tabSwitch(tab_area_counter, tab_count, active_tab_number);
        tab_area_counter++;
    }
}


/**
 *  source: http://www.w3schools.com/js/js_cookies.asp
 */
function _getCookie(c_name)
{
    var c_value = document.cookie;
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1)
    {
        c_start = c_value.indexOf(c_name + "=");
    }
    if (c_start == -1)
    {
        c_value = null;
    }
    else
    {
        c_start = c_value.indexOf("=", c_start) + 1;
        var c_end = c_value.indexOf(";", c_start);
        if (c_end == -1)
        {
            c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start, c_end));
    }
    return c_value;
}
/** _getCookie() **/


/**
 *  source: http://www.w3schools.com/js/js_cookies.asp
 */
function _setCookie(c_name, value, exdays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays==null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}
/** _setCookie() **/

/**
 *  Toggle the visibility of TOC entries based upon what tab is selected.
*/
function _tabTOCEntries(tab_id, make_visible)
{
    // Find all the heading in the indicated tab
    var the_tab = document.getElementById(tab_id);
    var headers = the_tab.getElementsByTagName('a');
    for (var i = 0; i < headers.length; i++)
    {
        var id = headers[i].getAttribute('id');
        var toc_id = 'toc_' + id;
        var toc_li = document.getElementById(toc_id);
        if (toc_li)
        {
            if (make_visible)
            {
                toc_li.style.display = 'block';
            }
            else
            {
                toc_li.style.display = 'none';
            }
        }
    }
}
/** _tabTOCEntries(tab_id, make_visible) **/

/** Register new init function - my first ever use of jQuery!
 *  - sigh. Doesn't work in Rincewind
 */
/*
jQuery(function() {
    initTabArea();
});
*/

/** Old code */
addInitEvent(function(){
    initTabArea();
});