/**********************************************************
说明:跨域请求数据Javascript组件
----------------------------------------------------------
add by 傻帽儿 2015-4-13 18:18:14
**********************************************************/
; (function (window, undefined) {
    var _ap = function () {
    }, _asyncID = 12349, _asyncType = "asynctype", _version = "1.5.0", _cbKey = "cb", _cb = "";
    function addSc(url, cbAndParam) {
        var script = createSc();
        script.setAttribute("src", url + "?" + cbAndParam);
        window.document.getElementsByTagName("head")[0].appendChild(script);
    };
    function delSc(id) {
        var scripts = getSc();
        for (var i = 0; i < scripts.length; i++) {
            if (scripts[i].attributes["tb_id"]) {
                if (parseInt(scripts[i].attributes["tb_id"].value) === parseInt(id)) {
                    remove(scripts[i]);
                }
            }
        }
    };
    function remove(script) {
        if ((script) && (script.parentNode)) {
            script.parentNode.removeChild(script);
        }
    };
    function clearSc() {
        var scripts = getSc();
        for (var i = 0; i < scripts.length; i++) {
            remove(scripts[i]);
        }
    };
    function getSc() {
        var s1 = [];
        var scripts = window.document.getElementsByTagName("script");
        for (var i = 0; i < scripts.length; i++) {
            if (scripts[i].attributes["tb_async"]) {
                if (scripts[i].attributes["tb_async"].value === _asyncType) {
                    s1.push(scripts[i]);
                }
            }
        }
        return s1;
    };
    function createSc() {
        _asyncID++;
        var script = window.document.createElement("script");
        script.setAttribute("tb_id", _asyncID);
        script.setAttribute("tb_async", _asyncType);
        script.setAttribute("type", "text/javascript");
        script.onload = script.onreadystatechange = function () {
            if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
                // 加入等待队里。2秒后将此标签对象移除。避免过多跨域加载导致页面Script标签过多。
                // waitDel(this.attributes["tb_id"].value, 2000);
                delSc(this.attributes["tb_id"].value);
                // 解决内存泄露问题。IE6,7貌似无效
                script.onload = script.onreadystatechange = null;
            }
        }
        return script;
    }
    function waitDel(id, interval) {
        window.setInterval(delSc(id), interval);
    };
    _ap.fn = _ap.prototype = {
        constructor: _ap,
        version: _version,
        init: function (arg) {
            return new _ap().constructor;
        },
        get: function (url, cbk, cb, param) {
            if (!cb) cb = _cb;
            if (!cbk) cbk = _cbKey;
            var qs = this.map(',', param);
            addSc(url, _cbKey + "=" + cb + "&" + qs);
        },
        getEasy: function (url, cb, param) {
            if (!cb) cb = _cb;
            var qs = this.map(',', param);
            addSc(url, _cbKey + "=" + cb + "&" + qs);
        },
        getCustom: function (url, param) {
            addSc(url, this.map(',', param));
        },
        map: function (split, params) {
            var str, strs = [];
            if (typeof params != "string") {
                params = eval(params);
                for (var p in params) {
                    strs.push(p + "=" + params[p])
                }
                str = strs.join(split);
            } else {
                str = param;
            }
            return str;
        },
        clear: function () { clearSc(); }
    };
    // SyntaxError: expected expression, got ')'
    _ap.fn.init.prototype = _ap.fn;
    window.TbAP = _ap.fn.init();
})(window);