﻿/*TabSwitch (通用TabSwitch封装类) Ver 1.01*\
jQuery:1.4
应用说明:页面包含<script type="text/javascript" src="TabSwitch.js"></script>	
参数说明:
tabID		Tab容器ID		(必选)
targetID	容器ID	(可选)
eventName		触发事件	(mouseover或者click,mouseover是指鼠标移到目标上就发生变化)
subTargetID		Tab sub 容器ID
funs		触发函数	选中各个Tab时触发的事件
cssName		Tab被选中时的样式定义名称 默认为"active"
isDouble	Tab容器ID下是否还是子项，如(<li><a class="on">编辑</a></li>)
splitID     Tab容器中各个选项是否有分隔符

\***程序制作/版权所有:Deep E-Mail:lfpsoft@163.com***/
(function($) {
    $.fn.tabswitch = function(options) {
        var me = this;
        var _timeout = null;
        options = $.extend({
            tag: 'li',
            tabID: null,
            targetID: null,
            eventName: 'click',
            subTargetID: null,
            funs: null,
            cssName: '',
            OncssName: 'on',
            DoubleChild: false,
            splitID: null,
            activeIndex: 0
        }, options);
        if (typeof options.tabID == "undefined") {
            alert("参数错误。\nTabID为空。");
            return;
        }
        var tabs = [];
        var targets = [];
        var subTargets = [];
        var splitTargets = [];
        var activeTabIndex = options.activeIndex;
        var o = document.getElementById(options.tabID).childNodes;
        for (var e = 0; e < o.length; e++) {
            if (o[e].nodeType == 1) {
                tabs.push(o[e]);
                o[e].className = options.cssName;
            }
        }
        tabs[activeTabIndex].className = options.OncssName;
        var activeTab = tabs[activeTabIndex];
        var activeTarget = null;
        var activeSubTarget = null;
        var disabledSplit = null;
        var activeCallback = null;
        if (options.targetID) {
            var a = document.getElementById(options.targetID).childNodes;
            for (var c = 0; c < a.length; c++) {
                if (a[c].nodeType == 1) {
                    a[c].style.display = "none";
                    targets.push(a[c])
                }
            }
            activeTarget = targets[activeTabIndex];
            activeTarget.style.display = "";
        }
        if (options.subTargetID) {
            subTargets = [];
            var p = document.getElementById(options.subTargetID).childNodes;
            for (var f = 0; f < p.length; f++) {
                if (p[f].nodeType == 1) {
                    subTargets.push(p[f])
                }
            }
            activeSubTarget = subTargets[activeTabIndex]
        }
        if (options.splitID) {
            splitTargets = [];
            var p = document.getElementById(options.splitID).childNodes;
            for (var f = 0; f < p.length; f++) {
                if (p[f].nodeType == 1) {
                    splitTargets.push(p[f]);
                }
            }
            disabledSplit = splitTargets[activeTabIndex];
        }

        if (options.funs) {           
            var jID = '#' + options.tabID + ' ' + options.tag;

            $(jID).bind("click", function() {
                var clickIndex = $(jID).index(this);
                me.showTab(clickIndex, options.funs)
            });

        } else {
            var jID = '#' + options.tabID + ' ' + options.tag;
            if (options.eventName != "mouseover") {
                $(jID).bind("click", function() {
                    var clickIndex = $(jID).index(this);
                    me.showTab(clickIndex)
                });
            } else {
                $(jID).bind("mouseover", function() {
                    var clickIndex = $(jID).index(this);
                    me._hover(clickIndex)
                });
                $(jID).bind("mouseout", function() {
                    var clickIndex = $(jID).index(this);
                    me._hide(clickIndex)
                });
            }
        }
        var showTab = this.showTab = function(a, b) {
            if (tabs[a] == activeTab) { return }
            if (options.DoubleChild && activeTab) {
                var _child = me.getFirstChild(activeTab);
                if (_child == null) _child = activeTab;
                activeTab.child = _child;
            } else {
                activeTab.child = activeTab;
            }

            if (activeTab) {
                activeTab.child.className = options.cssName
            }
            activeTab = tabs[a];
            activeTabIndex = a;

            if (options.DoubleChild && activeTab) {
                var _child = me.getFirstChild(activeTab);
                if (_child == null) _child = activeTab;
                activeTab.child = _child;
            } else {
                activeTab.child = activeTab;
            }

            activeTab.child.className = options.OncssName;

            if (targets.length > 0) {
                if (activeTarget) {
                    activeTarget.style.display = "none"
                }
                activeTarget = targets[a];
                activeTarget.style.display = ""
            }
            if (subTargets.length > 0) {
                activeSubTarget.style.display = "none";
                activeSubTarget = subTargets[a];
                activeSubTarget.style.display = ""
            }
            if (splitTargets.length > 0) {
                if (a == splitTargets.length) {
                    for (var i = 0; i < splitTargets.length; i++) {
                        splitTargets[i].style.display = "";
                    }
                    splitTargets[a - 1].style.display = "none";
                }
                else {
                    for (var i = 0; i < splitTargets.length; i++) {
                        if (a == i) {
                            splitTargets[i].style.display = "none";
                            if (i > 0) {
                                splitTargets[i - 1].style.display = "none";
                            }
                        } else {
                            splitTargets[i].style.display = "";
                        }
                    }
                }
            }
            if (b) { me.activeCallback = b; b(activeTabIndex, activeTarget, activeTab) } return false
        }
        var getFirstChild = this.getFirstChild = function(a) {
            var p = a.childNodes;
            for (var f = 0; f < p.length; f++) {
                if (p[f].nodeType == 1) {
                    return p[f];
                }
            }
            return null;
        }
        var _hover = this._hover = function(a, b, c) {
            me._timeout = window.setTimeout(function() { me.showTab(a, b, c) }, 200)
        }
        var _hide = this._hide = function(a) {
            if (me._timeout) { window.clearTimeout(me._timeout) }
        }
        if (options.funs) {
            setTimeout(function() { options.funs(activeTabIndex, activeTarget, activeTab) }, 200)
        }
        return this;
    };
})(jQuery);
