﻿var loadJS = {
    lock: false, ranks: []
    , callback: function(startTime, callback) {
        //载入完成
        this.lock = false;
        callback && callback(new Date().valueOf() - startTime.valueOf()); //回调	
        this.read(); //解锁，在次载入
    }
    , read: function() {
        //读取
        if (!this.lock && this.ranks.length) {
            var head = document.getElementsByTagName("head")[0];

            if (!head) {
                ranks.length = 0, ranks = null;
                throw new Error('HEAD不存在');
            }

            var wc = this, ranks = this.ranks.shift(), startTime = new Date, script = document.createElement('script');

            this.lock = true;

            script.onload = script.onreadystatechange = function() {
                if (script && script.readyState && script.readyState != 'loaded' && script.readyState != 'complete') return;

                script.onload = script.onreadystatechange = script.onerror = null, script.src = ''
		            , script.parentNode.removeChild(script), script = null; //清理script标记

                wc.callback(startTime, ranks.callback), startTime = ranks = null;
            };

            script.charset = ranks.charset || 'gb2312';
            script.src = ranks.src;

            head.appendChild(script);
        }
    }
    , push: function(src, charset, callback) {
        //加入队列
        this.ranks.push({ 'src': src, 'charset': charset, 'callback': callback });
        this.read();
    }
}
var MaiCheDomHelper = {
    cancelClick: function(e) {
        if (window.event && !window.event.cancelBubble) {
            window.event.cancelBubble = true;
            if (typeof window.event.returnValue != 'undefined') {
                window.event.returnValue = false;
            }
            return;
        }
        if (e && e.stopPropagation && e.preventDefault) {
            e.stopPropagation();
            e.preventDefault();
        }
    },
    addEvent: function(elm, evType, fn, useCapture) {
        if (elm.addEventListener) {
            elm.addEventListener(evType, fn, useCapture);
            return true;
        }
        else if (elm.attachEvent) {
            var r = elm.attachEvent('on' + evType, fn);
            return r;
        }
        else {
            elm['on' + evType] = fn;
        }
    },
    removeEvent: function(elm, evType, fn, useCapture) {
        if (elm.removeEventLister)
            elm.removeEventLister(evType, fn, useCapture);
        else if (elm.detachEvent)
            elm.detachEvent('on' + evType, fn);
    },
    trim: function(value) {
        return (value || "").replace(/^\s+|\s+$/g, "");
    },
    //读取cookie
    readCookie: function(name, value, options) {
        if (typeof value != 'undefined') { // name and value given, set cookie
            options = options || {};
            if (value === null) {
                value = '';
                options.expires = -1;
            }
            var expires = '';
            if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                var date;
                if (typeof options.expires == 'number') {
                    date = new Date();
                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                } else {
                    date = options.expires;
                }
                expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
            }
            var path = options.path ? '; path=' + options.path : '';
            var domain = options.domain ? '; domain=' + options.domain : '';
            var secure = options.secure ? '; secure' : '';
            document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
        } else { // only name given, get cookie
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = this.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
    }
}

// -------------标签切换

function addClass(element, value) {
	if (!element.className) {
		element.className = value;
	} else {
		newClassName = element.className;
		newClassName += " ";
		newClassName += value;
		element.className = newClassName;
	}
}

function removeClass(element, value) {
	var removedClass = element.className;
	var pattern = new RegExp("(^| )" + value + "( |$)");
	removedClass = removedClass.replace(pattern, "$1");
	removedClass = removedClass.replace(/ $/, "");
	element.className = removedClass;
	return true;
}

function commonTabSwitch() {
	tabs("sub_ul", "sub_con", null, false);
}
MaiCheDomHelper.addEvent(window, "load", commonTabSwitch,false);

/*tabs*/
function hide(id) { var Div = document.getElementById(id); if (Div) { Div.style.display = "none" } }
function show(id) { var Div = document.getElementById(id); if (Div) { Div.style.display = "block" } }

function tabsRemove(index, head, divs, div2s) {
	if (!document.getElementById(head)) return false;
	var tab_heads = document.getElementById(head);
	if (tab_heads) {
		var alis = tab_heads.getElementsByTagName("li");
		for (var i = 0; i < alis.length; i++) {
			removeClass(alis[i], "current");
			hide(divs + "_" + i);
			if (div2s) { hide(div2s + "_" + i) };
			if (i == index) {
				addClass(alis[i], "current");
			}
		}
		show(divs + "_" + index);
		if (div2s) { show(div2s + "_" + index) };
	}
}

function tabs(head, divs, div2s, over) {
	if (!document.getElementById(head)) return false;
	var tab_heads = document.getElementById(head);
	if (tab_heads) {
		var alis = tab_heads.getElementsByTagName("li");
		for (var i = 0; i < alis.length; i++) {
			alis[i].num = i;
			if (over) {
				alis[i].onmouseover = function() {
					var thisobj = this;
					thetabstime = setTimeout(function() { changetab(thisobj); }, 150);
				}
				alis[i].onmouseout = function() {
					clearTimeout(thetabstime);
				}
			}
			else {
				alis[i].onclick = function() {
					if (this.className == "current" || this.className == "last current") {
						changetab(this);
						return true;
					}
					else {
						changetab(this);
						return false;
					}
				}
			}

			function changetab(thebox) {
				tabsRemove(thebox.num, head, divs, div2s);
			}

		}
	}
}
// -------------标签切换
