﻿/*
Javascript WebTabCollection control
*/

var WebTabCollection = Class.create();

WebTabCollection.prototype =
{
    // Constructor
    initialize : function()
    {
        // Declare variable holding the TabControl's window title
        this.Title;
        
        //Declare variable holding the WebTab objects
        this.Tabs = new Hash();
        
        // Declare variable holding the title of the active tabs
        this._activeTab;
        
        // Declare variable holding the WebContainer
        this._htmlContainer;
        // Declare variable holding the tab button container
        this._tabButtonContainer;
        // Declare variable holding the tab content container
        this._tabContentContainer;
        
        // Call Build function to build the html structure
        this.Build();
    },
    
    Build : function()
    {
        try
        {
            // Build new content holder node
            this._htmlContainer = Builder.node('div');
            this._htmlContainer.className  = 'WebTabControl';
            
            // Create tab button container
            this._tabButtonContainer = Builder.node('div');
            this._tabButtonContainer.className = 'WebTabButtons'
            // Append container to htmlContainer
            this._htmlContainer.appendChild(this._tabButtonContainer);
            
            // Create tab content container
            this._tabContentContainer = Builder.node('div', { style: 'clear:both;' });
            this._tabContentContainer.className = 'WebTabContent'
            // Appendtab content container to htmlContainer
            this._htmlContainer.appendChild(this._tabContentContainer);
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'Build', arguments, 'WEBTABCOLLECTION', null, e, false)
        }
    },
    
    GetHtml : function()
    {
        try
        {
            // Check if the WebTabControl contains any WebTab objects
            if(this.Tabs.length > 0)
            {
                // Return the markup
                return this._htmlContainer.GetHtml();
            }
            
            // Iterate through the Tabs Hash
            for(var i = 0; i < this.Tabs.keys().length; i++)
            {
                // If this this the first iteration, set the current tab as the active tab.
                // If this is not the first iteration, set the current tab's content container invisible
                if(i == 0)
                {
                    this._activeTab = this.Tabs[this.Tabs.keys()[i]].Title;
                }
                else
                {
                    this.Tabs[this.Tabs.keys()[i]]._htmlContainer.style.display = 'none';
                }
                
                // Get the button of the current WebTab
                var currentTabButton = this.Tabs[this.Tabs.keys()[i]].GetButton();
                // Get the container of the current WebTab
                var currentTabContainer = this.Tabs[this.Tabs.keys()[i]].GetContainer();
                
                // Add the tab button to the button container
                this._tabButtonContainer.appendChild(currentTabButton);
                // Declare event handler for the tab button
                Event.observe(currentTabButton, 'click', this.SwitchTab.bindAsEventListener(this));
                // Add the tab container to the content container
                this._tabContentContainer.appendChild(currentTabContainer);
            }
            
            return this._htmlContainer;
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'GetHtml', arguments, 'WEBTABCOLLECTION', null, e, false)
        }
    },
    
    SwitchTab : function(piEvent)
    {
        try
        {
            // Get the element being the source of the event and return if it is null
            var targetElem = Event.element(piEvent);
            if(!targetElem)
                return;
                
            // Get the parent element of the target as long as its parent's name is not WebTabButtons
            while((targetElem.parentNode != null) && (targetElem.parentNode.className.indexOf('WebTabButtons') < 0))
            {
                targetElem = targetElem.parentNode;
            }
                
            // Get the tab title
            var tabTitle = targetElem.Title;
            
            // Check if the selected tab is already active and return if so
            if(this._activeTab == tabTitle)
            {
                return;
            }
            
            // Check if tabTitle is filled and if the tab exists in the tab Hash
            if((tabTitle != null) && (!tabTitle.blank()) &&(this.Tabs[tabTitle] != null))
            {
                this.Tabs[tabTitle]._htmlContainer.style.display = 'block';
                this.Tabs[this._activeTab]._htmlContainer.style.display = 'none';
                this._activeTab = tabTitle;
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'SwitchTab', arguments, 'WEBTABCOLLECTION', null, e, false)
        }
    },
    
    AddTab : function(piTabTitle)
    {
        try
        {
            // Check if a tab title has been provided and throw exception if not
            if((piTabTitle == null) || (piTabTitle.blank()))
            {
                throw 'A tab title must be provided';
            }
            
            // Create new WebTab object
            var newTab = new WebTab(piTabTitle);
            
            // Add WebTab object to Tabs Hash
            this.Tabs[piTabTitle] = newTab;
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'AddTab', arguments, 'WEBTABCOLLECTION', null, e, false)
        }
    }
}