﻿function ZksTabsControls(tabId, useMouseMove, enableLink,selectedColumnid) {
    var _this = this;
    var tabsPanel = document.getElementById(tabId + "_Tabs");
    var tabArray = tabsPanel.getElementsByTagName("ul")[0].getElementsByTagName("a");
    var vTabsDivArray = tabsPanel.getElementsByTagName("div");
    var selectedIndex = 0;

    //失去焦点后选中的索引(当前固定选中的索引)
    _this.BlurBySelectedIndex = 0;

    //选择选项卡
    _this.SelectTabByIndex = function (index) {
        if (parseInt(index) != null) {
            selectedIndex = parseInt(index);
        }
        if (selectedIndex < 0) {
            return;
        }
        var i = 0;
        for (i = 0; i < tabArray.length; i++) {
            if (i != selectedIndex) {
                tabArray[i].className = "";
            }
            else {
                tabArray[i].className = "SelectedTab";
                selectedIndex = i;
            }
        }
        viewTabContent();
    };

    _this.SelectTabByColumnId = function (cid) {
        if (cid.length < 1) {
            return;
        }
        var i = 0;
        for (i = 0; i < tabArray.length; i++) {
            if (tabArray[i].id.toLowerCase() != "atab_" + cid.toLowerCase()) {
                tabArray[i].className = "";
            }
            else {
                tabArray[i].className = "SelectedTab";
                selectedIndex = i;
                _this.BlurBySelectedIndex = i;
            }
        }
        viewTabContent();
    };

    var viewTabContent = function () {
        //切换选项卡时显示内容
        var vTabsContentArray = new Array();
        for (i = 0; i < vTabsDivArray.length; i++) {
            if (vTabsDivArray[i].className == "divTabsContent") {
                vTabsDivArray[i].style.display = "none";
                vTabsContentArray.push(vTabsDivArray[i]);
            }
        }
        if (vTabsContentArray[selectedIndex] != null) {
            vTabsContentArray[selectedIndex].style.display = "block";
        }
    };

    //重写指定选项卡单击方法
    _this.ReWriteOnClick = function (index, fun) {
        tabArray[index].onclick = function () {
            if (fun != null) {
                try {
                    eval(fun)(index);
                }
                catch (e) {
                    window.alert("该方法因发生异常未被执行：\n" + fun);
                }
            }
            tabArray[index].href = "#all";
        }
    };
    //重命名选项卡
    _this.ReNameTabByIndex = function (index, newTabTitle) {
        tabArray[index].innerHTML = newTabTitle;
    };
    //显示或隐藏指定索引选项卡（以;分割的索引号）
    _this.DisplayIndex = function (indexList, display) {
        //#region 使指定索引隐藏（以;分割的索引号）
        var indexArray = indexList.split(';');
        for (var i = 0; i < indexArray.length; i++) {
            if (tabArray[parseInt(indexArray[i])] != null) {
                tabArray[parseInt(indexArray[i])].parentNode.style.display = display ? "" : "none";
            }
        }
        //#endregion
    };

    var init = function () {
        //#region 初始化单击方法
        for (var i = 0; i < tabArray.length; i++) {
            if (useMouseMove) {
                tabArray[i].onmouseover = function () {
                    _this.SelectTabByIndex(this.parentNode.id.split("_")[1]);
                    return false;
                };
            }
            else {
                tabArray[i].onclick = function () {
                    this.blur();
                    _this.SelectTabByIndex(this.parentNode.id.split("_")[1]);
                    return false;
                };
            }
            if (!useMouseMove || !enableLink) {
                tabArray[i].href = "#all";
            }
        }

        var iCount = 0;
        for (i = 0; i < vTabsDivArray.length; i++) {
            if (vTabsDivArray[i].className == "divTabsContent") {
                vTabsDivArray[i].style.display = "none";
                if (iCount == _this.BlurBySelectedIndex) {
                    vTabsDivArray[i].style.display = "block";
                }
                iCount++;
            }
        }

        if (tabArray[_this.BlurBySelectedIndex] != null) {
            tabArray[_this.BlurBySelectedIndex].className = "SelectedTab";
        }

        //#endregion
        document.onclick = function () {
            if (event.srcElement.className == "") {
                //_this.SelectTabByIndex(_this.BlurBySelectedIndex);
            }
        };

        if (selectedColumnid != null) {
            _this.SelectTabByColumnId(selectedColumnid);
        }

    };

    init();

}
