﻿function Tab(name, isDefault, eventID) {
    this.Name = name;
    this.IsDefault = isDefault;
    this.EventID = eventID;
}

function TabSet(Tabs, UpdateHash) {
    var i;
    
    this.Tabs = Tabs;
    this.ActiveTab = "";
    this.PageTitle = document.title;
    if (typeof UpdateHash == 'undefined')
        this.UpdateHash = false;
    else
        this.UpdateHash = UpdateHash;

    for (i = 0; i < Tabs.length; i++) {
        if (Tabs[i].IsDefault) {
            this.ActiveTab = Tabs[i].Name;
            break;
        }
    }

    this.logEvent = function(tab) {
        var i;
        for (i = 0; i < this.Tabs.length; i++) {
            if (this.Tabs[i].Name == tab)
                if (this.Tabs[i].EventID > 0)
                    logEvent(this.Tabs[i].EventID);
        }
    };

    this.click = function(tab) {
        var ok = true;
        if (typeof x_tabs_click == 'function')
            ok = x_tabs_click(tab);

        this.logEvent(tab);

        if (ok)
            this.switchToTab(tab);
    };

    this.switchToTab = function(tab) {
        var foundTab = false;
        var bDefault = false;

        for (i = 0; i < Tabs.length; i++) {
            if (Tabs[i].Name == tab) {
                foundTab = true;
                bDefault = Tabs[i].IsDefault;
                break;
            }
        }

        if (!foundTab)
            return;

        var oTab, oBtn;
        for (i = 0; i < Tabs.length; i++) {
            oTab = $("c_tab_" + Tabs[i].Name);
            oBtn = $("c_tab_button_" + Tabs[i].Name);
            if (Tabs[i].Name == tab) {
                this.ActiveTab = tab;
                oBtn.className = "c_tabs_button c_tabs_button_selected";
                if (oTab)
                    oTab.style.display = "block";
            } else {
                oBtn.className = "c_tabs_button";
                if (oTab)
                    oTab.style.display = "none";
            }
        }

        if (this.UpdateHash) {
            if (location.hash.length != 0 || !bDefault) {
                location.replace("#" + tab);
            }
        }

        if (typeof x_tabs_switch == 'function')
            x_tabs_switch(tab);

        if (typeof floatingChatBox_init == 'function')
            floatingChatBox_init();

        document.title = this.PageTitle;
    };

    this.init = function() {
        var i, oButton, tabSet = this, tabName;
        for (i = 0; i < this.Tabs.length; i++) {
            tabName = this.Tabs[i].Name;
            oButton = $("c_tab_button_" + tabName);
            if (oButton) {
                oButton.tabName = tabName;
                oButton.onclick = function() {
                    eval("tabSet.click(this.tabName)");
                };
            }
        }

        this.parseQueryString();
    };

    this.parseQueryString = function() {
        var hash = location.hash;
        var parts, parameters, i, param;

        parameters = new Array();
        if (hash) {
            hash = hash.substr(1);
            parts = hash.split("&");
            hash = parts[0];

            if ($("c_tab_button_" + hash))
                this.switchToTab(hash);

            for (i = 1; i < parts.length; i++) {
                param = parts[i].split("=");
                if (param.length > 1) {
                    parameters[param[0]] = param[1];
                }
            }

            if (typeof x_tabs_init == 'function') {
                var oldfun = window.onload;
                window.onload = function() {
                    if (typeof oldfun == 'function')
                        oldfun();
                    x_tabs_init(hash, parameters);
                };
            }
        }
    };

    this.init();
}

function c_tab_isInitiallyVisible(tabSet, tab, isDefault) {
    if (tabSet) {
        if (tabSet.ActiveTab == tab) {
            tabSet.switchToTab(tab);
        }
    }
}