InepexWebUiCallback = function(context, func)
{
    return function() 
    {
        return func.apply(context, arguments);
    };
};

InepexWebUiTabPanelSet = function(containerElement, btn_Previous, btn_Next, isDynamic, baseId)
{
    this.containerElement = containerElement;
    this.panel = null;
    this.tabElements = new Array();
    this.tabElementIds = new Array();
    this.tabElementIds_Flipped = new Array();
    this.tabElementTargetTabForms = new Array();
    this.tabElementOnClickEventHandlers = new Array();
    this.actualTabForm = null;
    this.actualTabFormId = "";
    this.actualTabFormIdNr = -1;
    this.defaultTabForm = null;
    this.initialized = false;
    if (btn_Previous != null)
        btn_Previous.onclick = InepexWebUiCallback(this, this.OnPreviousClick);
    this.btn_Previous = btn_Previous;
    if (btn_Next != null)
        btn_Next.onclick = InepexWebUiCallback(this, this.OnNextClick);
    this.btn_Next = btn_Next;
    
    if (isDynamic)
    {
        var i = 0;
        var tabElement = null;
        var panelElement = null;
        var tabForm = null;
        var exit = false;
        while (!exit)
        {
            tabElement = document.getElementById(baseId + "_tab" + i);
            panelElement = document.getElementById(baseId + "_panel" + i);
            if ((tabElement != null) && (panelElement != null))
            {
                tabForm = InepexWebUiTabPanel.CreateEmpty(panelElement);
                this.AddTabElement(tabElement, tabForm);
            }
            else
                exit = true;
            i++;
        }
    }
};

InepexWebUiTabPanelSet.TabElementOnClick_Static = function(e)
{
    if (this.srcInepexTabFormSet && this.srcInepexTabFormSet.TabSwap)
        this.srcInepexTabFormSet.TabSwap(this.id);
};

InepexWebUiTabPanelSet.prototype =
{
    AddTabElement: function(tabElement, targetTabForm)
    {
        var defaultIsSetNow = false;
        if (tabElement == null)
            return;
        if (targetTabForm == null)
            return;
        if (this.defaultTabForm == null)
        {
            this.defaultTabForm = targetTabForm;
            defaultIsSetNow = true;
            // this.defaultTabForm.Show();
        }
        var tEId = tabElement.id;
        if (this.actualTabFormId == "")
            this.actualTabFormId = tEId;
        if (this.actualTabFormIdNr == -1)
            this.actualTabFormIdNr = 0;
        this.tabElementIds[this.tabElementIds.length] = tEId;
        this.tabElementIds_Flipped[tEId] = this.tabElementIds.length - 1;
        tabElement.srcInepexTabFormSet = this;
        this.tabElements[tEId] = tabElement;
        this.tabElementTargetTabForms[tEId] = targetTabForm;
        this.tabElementTargetTabForms[tEId].Hide();
        this.tabElementOnClickEventHandlers[tEId] = tabElement.onclick;
        tabElement.onclick = InepexWebUiTabPanelSet.TabElementOnClick_Static;
        if (defaultIsSetNow)
            this.TabSwap(tEId);
    },

    OnPreviousClick: function()
    {
        if (this.actualTabFormIdNr <= 0)
            return;
        this.TabSwap(this.tabElementIds[this.actualTabFormIdNr - 1]);
    },
    
    OnNextClick: function()
    {
        if (this.actualTabFormIdNr >= this.tabElementIds.length - 1)
            return;
        this.TabSwap(this.tabElementIds[this.actualTabFormIdNr + 1]);
    },
    
    TabSwap: function(tabElementId)
    {
        if ((tabElementId == null) || (this.tabElementTargetTabForms[tabElementId] == null))
            return;

        this.OnTabElementSwap(tabElementId, false);
        this.actualTabForm = this.tabElementTargetTabForms[tabElementId];
        this.actualTabFormId = tabElementId;
        this.actualTabFormIdNr = this.tabElementIds_Flipped[tabElementId];
    },
    
    OnTabElementSwap: function(tabElementId, save)
    {
        for (var tmpTEId in this.tabElementTargetTabForms)
            if (tmpTEId != tabElementId)
            {
                this.tabElementTargetTabForms[tmpTEId].Hide();
                this.tabElements[tmpTEId].className = this.tabElements[tmpTEId].className.replace(/_on/, "_off");
            }

        this.tabElementTargetTabForms[tabElementId].Show();
        this.tabElements[tabElementId].className = this.tabElements[tabElementId].className.replace(/_off/, "_on");

        if (typeof this.tabElementOnClickEventHandlers[tabElementId] == "function")
            this.tabElementOnClickEventHandlers[tabElementId]();
    },
    
    Show: function()
    {
        if (this.containerElement == null)
            return;
        this.containerElement.style.display = "block";
    },
    
    Hide: function()
    {
        if (this.containerElement == null)
            return;
        this.containerElement.style.display = "none";
    }
};

InepexWebUiTabPanel = function(containerElement)
{
    this.containerElement = containerElement;
};

InepexWebUiTabPanel.CreateEmpty = function(containerElement)
{
    if (containerElement === undefined)
        containerElement = null;
    return new InepexWebUiTabPanel(containerElement);
};

InepexWebUiTabPanel.prototype = 
{
    Show: function()
    {
        if (this.containerElement == null)
            return;
        this.containerElement.style.display = "block";
    },

    Hide: function()
    {
        if (this.containerElement == null)
            return;
        this.containerElement.style.display = "none";
    }
};

