﻿
    TabContainerBehavior = function() {
        this._tabCollection = new Array();
        this._tabControls = new Array();
    }

    TabContainerBehavior.prototype = {

        initialize: function() { },
        dispose: function() { },
        get_tabCollection: function() { return this._tabCollection; },
        get_tabControls: function() { return this._tabControls; },
        get_activeCssForControl: function(controlId) {
            for (i = 0; i < this._tabControls.length; i++) {
                if (controlId == this._tabControls[i].control)
                    return this._tabControls[i].active;
            }
        },
        get_activeCssForTab: function(controlId, tabId) {
            for (i = 0; i < this._tabCollection.length; i++) {
                if (controlId == this._tabCollection[i].control && tabId == this._tabCollection[i].tab)
                    return this._tabCollection[i].active;
            }
            return get_activeCssForControl(controlId);
        },
        get_inactiveCssForControl: function(controlId) {
            for (i = 0; i < this._tabControls.length; i++) {
                if (controlId == this._tabControls[i].control)
                    return this._tabControls[i].inactive;
            }
        },
        get_inactiveCssForTab: function(controlId, tabId) {
            for (i = 0; i < this._tabCollection.length; i++) {
                if (controlId == this._tabCollection[i].control && tabId == this._tabCollection[i].tab)
                    return this._tabCollection[i].inactive;
            }
            return get_inactiveCssForControl(controlId);
        },
        get_tabValue: function(controlId, tabId) {
            for (i = 0; i < this._tabCollection.length; i++) {
                if (controlId == this._tabCollection[i].control && tabId == this._tabCollection[i].tab)
                    return this._tabCollection[i].tabValue;
            }
            return get_inactiveCssForControl(controlId);
        },
        registerControl: function(controlId, activeCss, inactiveCss) {
            this._tabControls[this._tabControls.length] = { control: controlId, active: activeCss, inactive: inactiveCss };
        },
        registerTab: function(controlId, tabId, targetId, activeCss, inactiveCss, selectedIndexHidField, tabValue) {
            if (activeCss == '') activeCss = this.get_activeCssForControl(controlId);
            if (inactiveCss == '') inactiveCss = this.get_inactiveCssForControl(controlId);
            this._tabCollection[this._tabCollection.length] = { control: controlId, tab: tabId, target: targetId, active: activeCss, inactive: inactiveCss, selectedIndexHidField: selectedIndexHidField, tabValue: tabValue };
            //document.getElementById(tabId).onclick = this.switchTab(controlId, tabId);
        },
        resetTabs: function(controlId) {
            for (i = 0; i < this._tabCollection.length; i++) {
                if (this._tabCollection[i].control == controlId) {
                    var elementRef = document.getElementById(this._tabCollection[i].tab);
                    if (!elementRef) continue;

                    elementRef.className = this._tabCollection[i].inactive;
                    document.getElementById(this._tabCollection[i].target).style.display = 'none';

                }
            }
        },
        switchTab: function(controlId, tabId) {
            //check to ensure the control is available
            var controlRef = document.getElementById(controlId);
            if (!controlRef)
                return; //could not find a reference to the tab container control. Perhaps it was made invisible

            this.resetTabs(controlId);
            activeCss = this.get_activeCssForControl(controlId);
            tabValue = this.get_tabValue(controlId, tabId);
            var tabIndex = 0;
            for (i = 0; i < this._tabCollection.length; i++) {
                if (this._tabCollection[i].control != controlId)
                    continue;

                if (this._tabCollection[i].tab == tabId) {
                    document.getElementById(this._tabCollection[i].tab).className = this._tabCollection[i].active;
                    document.getElementById(this._tabCollection[i].target).style.display = 'block';
                    document.getElementById(this._tabCollection[i].selectedIndexHidField).value = tabIndex;
                    return;
                }

                tabIndex++;
            }
        }

    }

