/*
* author:MaoFangyun
* createTime:2015-01-15
* description:封装页面选项卡操作
* notice:需引用jquery
*/

/*
* 选项卡
* @relationGird 与选项卡关联的数据表
* @callback 回调函数[需要控制数据表及数据表对应的分页控件之外的元素时传入此回调函数]
*/
function Tab(relationGird, callback) {
    this.relationGird = relationGird;
    this.callback = callback ? callback : null;
}
function TabGroup() {
    this.activeTab = null;//激活的tab
    this.tabs = [];//选项卡数组[默认会激活数组第一元素对应的选项卡]
    this.tabControls = $(".pt-tab");
    this.tabActiveClass = "active";//选项卡激活时的样式名称
}
TabGroup.prototype = {
    add: function(tab) {
        this.tabs.push(tab);
    },
    bindEvents: function() {
        var self = this;
        if (this.tabs.length > 0) {
            this.activeTab = this.tabs[0];
        }
        this.tabControls.click(function(e) {
            var target = $(e.target);
            if (!target.hasClass(self.tabActiveClass)) {
                var activeTab = self.tabs[target.index()];
                if (activeTab.callback) {
                    activeTab.callback();
                }
                activeTab.relationGird.control.show();
                if (activeTab.relationGird.pager) {
                    activeTab.relationGird.pager.control.show();
                }
                self.activeTab = activeTab;
                for (var i = 0, k = self.tabs.length; i < k; i++) {
                    if (i != target.index()) {//非活动的tab
                        var inactiveTab = self.tabs[i];
                        if (inactiveTab.callback) {
                            inactiveTab.callback();
                        }
                        inactiveTab.relationGird.control.hide();
                        if (inactiveTab.relationGird.pager) {
                            inactiveTab.relationGird.pager.control.hide();
                        }
                    }
                }
                self.tabControls.removeClass(self.tabActiveClass);
                target.addClass(self.tabActiveClass);
                activeTab.relationGird.search(true);
            }
        });
    }
};