﻿var TABS_ACTIVE = true;
function createTabSequence(activeTab, isShort)
{
    var returnTable = '<table id="tabTable" tId="none" active="' + activeTab + '" cellpadding="0" cellspacing="0" onclick="clickTab(event)"><tr>';

    var tabCount = 0;
    var lastActive = false;
    for(tabCount = 0; tabCount < tabs.length; tabCount++)
    {
        var theTab = tabs[tabCount].split('|');
        var isActive = (activeTab == theTab[2] ? 'Active' : 'InActive');
        var isNextActive = (tabCount + 1 < tabs.length ? activeTab == tabs[tabCount+1].split('|')[2] : false);

        returnTable += '<td tId="' + theTab[2] + '" class="leftSide ';
        
        if (activeTab == theTab[2])
            returnTable += 'leftTabActive';
        else
            returnTable += 'leftTabInActive';
            
        returnTable += '" valign="top">';
        returnTable += '<div style="position:relative;"><div class="left' + isActive + '"></div></div></td>';
        returnTable += '<td title="' + theTab[0] + '" tId="' + theTab[2] + '" class="middle' + isActive + ' ' + theTab[2] + 'Tab ' + isActive + 'Tab" onclick="clickTab(event, this)">';
        returnTable += '<div class="insideTabTop insideTabTopFont">' + (isActive == 'Active' ? theTab[0] : theTab[1]) + '</div></td>';
        returnTable += '<td tId="' + theTab[2] + '" class="slope' + isActive + '" valign="top"></td>';
        
        if (tabCount + 1 < tabs.length)
        {
            if (isActive == 'Active')
                returnTable += '<td tId="' + theTab[2] + '" valign="top" style="display:inline; z-index:2;"><div style="position:relative; vertical-align:top; z-index:2;"><div class="rightActive rightactiveNoEnd"></div></div></td>';
            else
                if (!isNextActive)
                    returnTable += '<td tId="' + theTab[2] + '" valign="top" style="display:inline; z-index:2;"><div style="position:relative; vertical-align:top; z-index:2;"><div class="rightInActive rightactiveNoEnd"></div></div></td>';
        }

        lastActive = (isActive == 'Active');
    }

    if (theTab != undefined && theTab[2] != null)
    {
        if (lastActive)
            returnTable += '<td tId="' + theTab[2] + '" class="rightActive" valign="top"></td>';
        else
            returnTable += '<td tId="' + theTab[2] + '" class="rightInActive" valign="top"></td>';
    }

    returnTable += '</tr></table>';
    
    return returnTable;
}

function clickTab(event)
{
    if (!TABS_ACTIVE)
        return;

    if ( !event )
        event = window.event;

    var theSource = event.target ? event.target : event.srcElement;
    
    // Fixes a Safari bug
    while ( theSource.nodeType != 1 )
        theSource = theSource.parentNode;

    while ( theSource.getAttribute('tId') == null )
        theSource = theSource.parentNode;
    
    if (theSource.getAttribute('tId') == 'none')
    {
        event.cancelBubble = true;
        return;
    }
    
    selectTab(theSource.getAttribute('tId'));
    
    event.cancelBubble = true;
    return false;
}

function selectTab(tabId)
{
    if (!TABS_ACTIVE)
        return;
        
    var previousActive = activeTab();
    
    var tabTable = document.getElementById('tabTable');
    tabTable.parentNode.innerHTML = createTabSequence(tabId, useShort);
    
    if (window.onTabClick)
        onTabClick(tabId, previousActive);
}

function disableTabs()
{
    TABS_ACTIVE = false;
}

function enableTabs()
{
    TABS_ACTIVE = true;
}

function activeTab()
{
    return document.getElementById('tabTable').getAttribute('active');
}

function activeName()
{
    var count = 0;
    var activeT = activeTab();
    
    for(; count < tabs.length; count++)
    {
        var theTab = tabs[count].split('|');
        if (theTab[2] == activeT)
            return theTab[0];
    }
    
    return '';
}

function activeShortName()
{
    var count = 0;
    var activeT = activeTab();
    
    for(; count < tabs.length; count++)
    {
        var theTab = tabs[count].split('|');
        if (theTab[2] == activeT)
            return theTab[1];
    }
    
    return '';
}
