


////////////////////////////////////////////////////////////////////////////////////////////////////
// Tab constructor
////////////////////////////////////////////////////////////////////////////////////////////////////
function Tab(iId, iGroup, iIndex)
{
    var tab = this;

    this.id = iId;
    this._super = Widget;
    this._super(iId);
    
    this.group = iGroup;
    this.index = iIndex;
    this.buttons = new Array();
    this.enabled = true;
    
    this.onActivation = new EventHandler();


    this.activate = function()
        {
            if (tab.group)
                tab.group.activateTab(tab.index);
            else
                tab._activate();
        }

    this.initButton = function(iButtonId, iBaseClassName)
        {
            var button = new Button(iButtonId, tab.activate);
            if (!tab.enabled)
                button.disable();
            tab.buttons[tab.buttons.length] = button;
            return button;
        }
        
    this.enable = function()
        {
            tab.enabled = true;
            for (var i = 0; i < tab.buttons.length; i++)
                tab.buttons[i].enable();
        }
        
    this.disable = function()
        {
            tab.enabled = false;
            tab.hide();
            for (var i = 0; i < tab.buttons.length; i++)
                tab.buttons[i].disable();
        }

    this._activate = function()
        {
            tab.onActivation.invoke();
            tab.show();
            for (var i = 0; i < tab.buttons.length; i++)
                tab.buttons[i].setState(true);
        }

    this._deactivate = function()
        {
            tab.hide();
            for (var i = 0; i < tab.buttons.length; i++)
                tab.buttons[i].setState(false);
        }    

}

////////////////////////////////////////////////////////////////////////////////////////////////////
// TabGroup contructor
////////////////////////////////////////////////////////////////////////////////////////////////////
function TabGroup(iId, iTabCount, iInitButtons)
{
    var tabGroup = this;
    
    this.id = iId;
    this.tabs = new Array();
    
    this.onActivation = new EventHandler();
    this.activeTab = -1;
    
    
    this.initTab = function(iEnabled, iInitButton)
        {
            var index = tabGroup.tabs.length;
            var tabId = tabGroup.id + '-' + index;
            var tab = new Tab(tabId, tabGroup, index);
            
            tabGroup.tabs[index] = tab;
 
            if (!iEnabled)
                tab.disable();
 
            if (iInitButton)
                tab.initButton(tabGroup.id + '-button-' + index)
            
            return tab;
        }

    this.initTabs = function(iTabCount, iInitButtons)
        {
            if (!iInitButtons)
                iInitButtons = false;
                
            for (var i = 0; i < iTabCount; i++)
                tabGroup.initTab(true, iInitButtons);
        }       

    this.activateTab = function(iIndex)
        {
            tabGroup.activeTab = iIndex;
            tabGroup.onActivation.invoke();

            for (var i = 0; i < tabGroup.tabs.length; i++)
            {
                if (i == iIndex)
                    tabGroup.tabs[i]._activate();
                else
                    tabGroup.tabs[i]._deactivate();
            }   
        }

    
    if (iTabCount)
        this.initTabs(iTabCount, iInitButtons);
    
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Button contructor
////////////////////////////////////////////////////////////////////////////////////////////////////
function Button(iId, iOnClick, iBaseClassName)
{
    var button = this;
    
    this.id = iId;
    this._super = Widget;
    this._super(iId);

    this.onClick = new EventHandler();

    this.enabled = true;
    this.pushed = false;
    
    if (iOnClick)
        this.onClick.add(iOnClick);    
    
    if (iBaseClassName)
        this.baseClassName = iBaseClassName;
    else
        this.baseClassName = button.getElement().className;


    this.enable = function()
        {
            button.enabled = true;
            button.update();
        }
        
    this.disable = function()
        {
            button.enabled = false;
            button.update();
        }

    this.setState = function(iPushedState)
        {
            if (button.enabled)
            {
                button.pushed = iPushedState;
                button.update();
            }
        }

    this.update = function()
        {
            if (button.enabled)
            {
                if (button.pushed)
                    button.getElement().className = button.baseClassName + '-' + 'on';
                else
                    button.getElement().className = button.baseClassName;
            }
            else
                button.getElement().className = button.baseClassName + '-' + 'disable';
        }

    this.click = function()
        {
            button.onClick.invoke();            
        }

    this._onclick = function()
        {
            if (button.enabled)
                button.click();
            return false;
        }


    this.getElement().onclick = this._onclick;
    
}


////////////////////////////////////////////////////////////////////////////////////////////////////
// TabLoader constructor
////////////////////////////////////////////////////////////////////////////////////////////////////
function TabLoader(iTab, iUrl, iLoad)
{
    var tabLoader = this;
    
    
    this.tab = iTab;
    this.url = iUrl;
    this.loaded = false;
    
    
    this.load = function()
        {
            if (tabLoader.loaded)
                return;
         
            var loader = new AsyncContentLoader(tabLoader.tab.id);
            tabLoader.loaded = true;
            loader.load(tabLoader.url);
        }
        

    iTab.onActivation.add(this.load);
    
    if (iLoad)
        this.load();
    
}

////////////////////////////////////////////////////////////////////////////////////////////////////
