﻿var TabControlRotations = new Array();

// finds the next tab that needs to be shown,
// then calls the toggleTabControl() method to actually show the next tab,
// then calls startRotationTabControl() to set the next time this method should be called again.
function rotateTabControl(tabId, tabContentId, tabTotal, seconds)
{
    var tabIdTemplate = tabId.substring(0, tabId.length -1);
    var tabContentIdTamplate = tabContentId.substring(0, tabContentId.length -1);
    var nextTab = 0;
    
    for (var i=1; i <= tabTotal; ++i)
    {
        var actualTab = document.getElementById(tabIdTemplate+i);
        var actualTabContainer = document.getElementById(tabContentIdTamplate+i);
        
        if (actualTab.className == 'tbc-active')
        {
            if (i == tabTotal)
            {
                nextTab = 1;
            }
            else
            {
                nextTab = i +1;
            }   
            nextTabId = document.getElementById(tabIdTemplate+nextTab);
            nextTabContentId = document.getElementById(tabContentIdTamplate+nextTab);
            
            toggleTabControl(nextTabId.id, nextTabContentId.id, tabTotal)
            startRotationTabControl(nextTabId.id, nextTabContentId.id, tabTotal, seconds)
            break;
        }
        else if (i == tabTotal)
        {
            nextTab = 1;
            nextTabId = document.getElementById(tabIdTemplate+nextTab);
            nextTabContentId = document.getElementById(tabContentIdTamplate+nextTab);

            toggleTabControl(nextTabId.id, nextTabContentId.id, tabTotal)
            startRotationTabControl(nextTabId.id, nextTabContentId.id, tabTotal, seconds)
        }
    }
}

// set the next time that function rotateTabControl() should be called.
function startRotationTabControl(tabId, tabContentId, tabTotal, seconds)
{
    var cmd = 'rotateTabControl("' + tabId + '", "' + tabContentId + '", "' + tabTotal + '", "' + seconds + '");';
    var tabTemplate = getTabTemplateId(tabId);
    TabControlRotations[tabTemplate] = window.setTimeout(cmd, seconds * 1000);
}

// stops the rotateTabControl() function from being automatically executed.
function stopRotationTabControl(tabIdTemplate)
{
    if (TabControlRotations[tabIdTemplate] != null)
    {
        window.clearTimeout(TabControlRotations[tabIdTemplate]);
        TabControlRotations[tabIdTemplate] = null;
    }
}

// this method changes the class names and sytles to reflect what tab the user has clicked on
function toggleTabControl(tabId, tabContentId, tabTotal)
{
    var tabIdTemplate = getTabTemplateId(tabId);
    var tabContentIdTamplate = getTabTemplateId(tabContentId);

    hideAllTabs(tabIdTemplate, tabContentIdTamplate, tabTotal)
    
    document.getElementById(tabId).className = 'tbc-active';
    document.getElementById(tabContentId).style.display = 'block';
    
    if (document.getElementById(tabIdTemplate +"Close") != null)
    {
        document.getElementById(tabIdTemplate +"Close").className = 'tbc-tabsMaximized';
    }
    
    stopRotationTabControl(tabIdTemplate);
}

// hides and shows the tabs
function toggleTabVisibility(tabCloseId, tabId, tabContentId, tabTotal, defaultTabId)
{
    var tabCloseElement = document.getElementById(tabCloseId);
    var flag = false;
    
    var tabIdTemplate = getTabTemplateId(tabId);
    var tabContentIdTamplate = getTabTemplateId(tabContentId);
    
    for (var i=1; i <= tabTotal; ++i)
    {
        var currentTab = document.getElementById(tabIdTemplate+i);
        var currentTabContainer = document.getElementById(tabContentIdTamplate+i);
        
        if (currentTab.className == 'tbc-active')
        {
            flag = true;
        }
    }
    
    if (flag)
    {
        hideAllTabs(tabIdTemplate, tabContentIdTamplate, tabTotal);
        tabCloseElement.className = 'tbc-tabsMinimized';
    }
    
    else
    {
        document.getElementById(tabIdTemplate+defaultTabId).className = 'tbc-active';
        document.getElementById(tabContentIdTamplate+defaultTabId).style.display = 'block';
        tabCloseElement.className = 'tbc-tabsMaximized';
    }
}

////////////////////
// helper methods //
////////////////////

// this method hides all tabs and sets changes the tab className
function hideAllTabs(tabIdTemplate, tabContentIdTamplate, tabTotal)
{
    for (var i=1; i <= tabTotal; ++i)
    {
        var currentTab = document.getElementById(tabIdTemplate+i);
        var currentTabContainer = document.getElementById(tabContentIdTamplate+i);

        currentTab.className = 'tbc-inactive';
        currentTabContainer.style.display = 'none';
    }
}

// this returns the id template for the tab / tab content divs
function getTabTemplateId(elementID)
{
    return elementID.substring(0, elementID.length -1);
}

