﻿/*对jQuery无依赖的公共组件*/
//判断是否IE6
IsIe6 = /MSIE 6/i.test(window.navigator.userAgent);

//trim
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,""); 
};
//字符串格式化
String.prototype.format = function() {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g,
        function(m, i) {
            return args[i];
        });
};

//定义全局变量GLOBAL
if ("undefined" == typeof (GLOBAL) || !GLOBAL) {
    var GLOBAL = {};
}

/*命名空间*/
GLOBAL.namespace = function(str) {
    var arr = str.split("."), o = GLOBAL;
    for (i = (arr[0] == "GLOBAL") ? 1 : 0; i < arr.length; i++) {
        o[arr[i]] = o[arr[i]] || {};
        o = o[arr[i]];
    }
}

/*Dom相关*/
GLOBAL.namespace("Dom");
//获取节点组（根据class）
GLOBAL.Dom.getElementsByClassName = function(str, root, tag) {
    if (root) {
        root = typeof root == "string" ? document.getElementById(root) : root;
    } else {
        root = document.body;
    }
    tag = tag || "*";
    var els = root.getElementsByTagName(tag), arr = [];
    for (var i = 0, n = els.length; i < n; i++) {
        for (var j = 0, k = els[i].className.split(" "), l = k.length; j < l; j++) {
            if (k[j] == str) {
                arr.push(els[i]);
                break;
            }
        }
    }
    return arr;
}
//添加class
GLOBAL.Dom.addClass = function(node, str) {
    if (!new RegExp("(^|\\s+)" + str).test(node.className)) {
        node.className = node.className + " " + str;
    }
}
//移除class
GLOBAL.Dom.removeClass = function(node, str) {
    node.className = node.className.replace(new RegExp("(^|\\s+)" + str), "");
}

/*Event相关*/
GLOBAL.namespace("Event");
//节点监听事件
GLOBAL.Event.on = function(node, eventType, handler, scope) {
    node = typeof node == "string" ? document.getElementById(node) : node;
    scope = scope || node;
    if (document.all) {
        node.attachEvent("on" + eventType, function() { handler.apply(scope, arguments) });
    } else {
        node.addEventListener(eventType, function() { handler.apply(scope, arguments) },
false);
    }
}

/*Tab面板
调用示例:
var tabs = GLOBAL.Dom.getElementsByClassName("J_tab");
new Tab({ root: tabs[0], trigger: "mouseover" });
new Tab({ root: tabs[1], currentClass: "tab-currentMenu", autoPlay: true, playTime: 5000 });
new Tab({ root: tabs[2], currentClass: "tab-currentMenu2", trigger: "mouseover",
handler: function(index) { alert("您激活的是第" + (index + 1) + "个标签") } 
}); 
*/
function Tab(config) {
    this._root = config.root;
    this._currentClass = config.currentClass;
    var trigger = config.trigger || "click";
    this._handler = config.handler;
    var autoPlay = config.autoPlay;
    var playTime = config.playTime || 3000;
    this._tabMenus = GLOBAL.Dom.getElementsByClassName("J_tab-menu", this._root);
    this._tabContents =
GLOBAL.Dom.getElementsByClassName("J_tab-content", this._root);
    this.currentIndex = 0;
    var This = this;
    if (autoPlay) {
        setInterval(function() { This._autoHandler() }, playTime);
    }
    for (var i = 0; i < this._tabMenus.length; i++) {
        this._tabMenus[i]._index = i;
        GLOBAL.Event.on(this._tabMenus[i], trigger, function() {
            This.showItem(this._index);
            this.currentIndex = this._index;
        });
    }
}
Tab.prototype = {
    showItem: function(n) {
        for (var i = 0; i < this._tabContents.length; i++) {
            this._tabContents[i].style.display = "none";
        }
        if (typeof jQuery != 'undefined') {
            $(this._tabContents[n]).fadeIn('slow');
        } else {
            this._tabContents[n].style.display = "block";
        }
        if (this._currentClass) {
            var currentMenu =
GLOBAL.Dom.getElementsByClassName(this._currentClass, this._root)[0];
            if (currentMenu) {
                GLOBAL.Dom.removeClass(currentMenu, this._currentClass);
            }
            GLOBAL.Dom.addClass(this._tabMenus[n], this._currentClass);
        }
        if (this._handler) {
            this._handler(n);
        }
    },
    _autoHandler: function() {
        this.currentIndex++;
        if (this.currentIndex >= this._tabMenus.length) {
            this.currentIndex = 0;
        }
        this.showItem(this.currentIndex);
    }
}
