/*

# ------------------------------------------
# PublishExec/interface/skins/HtmlHelp/HtmlHelp.js
# (c) publishexec.com all rights reserved
# Author: Cezary Tomczak
# created 2005-07-15 modified 2005-08-02
# ------------------------------------------

  TODO:
  - load first top root node

  BUGS:
  - (opera) tree state is not saved on browser refresh. Save state on every change? not only on window.onunload?

*/

var selectedTab;
var tree;

// order is important,
// ex. bookmarks load state must be called after the tree is initiated
// ex. loadTab must be called after bookmarks load state is done
onLoad(function()
{
    // init tree
    tree = new DynamicTree("tree");
    tree.path = TREE_IMAGES_PATH;
    tree.cookieID = "HtmlHelp-tree-opened";
    tree.init();

    // bookmarks, bookmarks state need to be loaded before calling Options.init()
    Bookmarks.loadState();

    // document loaded directly OR load first top root node
    if (DOCUMENT_ID)
    {
        tree.openToNodeId(DOCUMENT_ID);
        tree.setActiveNodeId(DOCUMENT_ID);
        Options.init();
    }

    // last loaded tab
    if (getCookie('HtmlHelp-lastLoadedTab')) loadTab(getCookie('HtmlHelp-lastLoadedTab'));
    else loadTab('topics');
});

onUnload(function()
{
    // last loaded tab
    setCookie('HtmlHelp-lastLoadedTab', selectedTab);

    // bookmarks
    Bookmarks.saveState();
});

function loadTab(id)
{
    switch (id)
    {
        case 'topics':
            selectTab('topics');
            Topics.init();
            break;
        
        case 'index':
            selectTab('index');
            Index.init();
            break;
        
        case 'search':
            selectTab('search');
            Search.init();
            break;

        case 'bookmarks':
            selectTab('bookmarks');
            Bookmarks.init();
            break;
    }
}

function selectTab(id)
{
    resetTabs();
    $('tab-'+id).className = 'tab-active';
    toggleOn('window-'+id);
    selectedTab = id;
}

function resetTabs()
{
    var tabs = ['topics', 'index', 'search', 'bookmarks'];
    for (var i = 0; i < tabs.length; i++)
    {
        if (!$('tab-'+tabs[i])) throw 'Element "'+tabs[i]+'" is missing';
        $('tab-'+tabs[i]).className = 'tab';
        toggleOff('window-'+tabs[i]);
    }
}