﻿
///////////////////////////////////////////////////////////////////

/* 定义方法 */
var $ = function(id) { var obj = typeof id == "string" ? document.getElementById(id) : id; return obj; }

/* 取数组最大，小值 */
var maxValue = function(a) { return Math.max.apply(null, a); }
var minValue = function(a) { return Math.min.apply(null, a); }

/* 数组排序{ string } asc：从小到大，desc：从大到小 */
function paixu(y, string) {
    y.sort(
        function(mode) {
            if (mode == "asc") {
                return function(a, b) {
                    if (a >= b) return 1;
                    if (a < b) return -1;
                };
            }
            else if (mode == "desc") {
                return function(a, b) {
                    if (a >= b) return -1;
                    if (a < b) return 1;
                };
            } else {
                return function(a, b) { return a - b; };
            }
        } (string)
        );
    return y;
}

/* 统计数字、字符在数组中出现次数 */
function count(t, c) {
    var m = t.sort(function compare(a, b) { return a - b; });
    var n = 0;
    for (var i = 0; i < m.length; i++) {
        if (m[i] == c) n++;
        else if (m[i] > c) break;
    }
    return n;
}
    
/* 取地址栏参数 */
function argument(name) {
    var sReg = "(?:\\?|&){1}" + name + "=([^&]*)";
    var re = new RegExp(sReg, "gi");
    re.exec(location.href);
    return RegExp.$1;
}

/* 分解字符串 */
function str_split(s, n) {
    var temp = [];
    for (var i = 0; i < s.length; i += n) {
        temp.push(s.substring(i, i + n));
    }
    return temp;
}

/* 左边补足位数 */
function padLeft(string, pad, count) {
    while (string.length < count) {
        string = pad + string;
    }
    return string;
}

///////////////////////////////////////////////////////////////////

var tb = {};

/* 获取浏览器类型 */
tb.isIE = /msie/i.test(navigator.userAgent);
tb.isFF = /firefox/i.test(navigator.userAgent);

tb.ajax = {
    xmlHttp: function() {
        if (tb.isIE) {
            var a = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
            for (var i = 0, l = a.length; i < l; i++) {
                try {
                    return new ActiveXObject(a[i]);
                } catch (e) { };
            }
            return false;
        } else {
            return new XMLHttpRequest();
        };
    },

    // 载入文件
    callFile: function(url, callBack) {
        var xmlhttp = this.xmlHttp();
        var async = !!callBack;
        xmlhttp.open("GET", url, async);
        if (tb.isFF) xmlhttp.overrideMimeType("text/html");
        if (async) {
            xmlhttp.onreadystatechange = this.state(xmlhttp, callBack);
            xmlhttp.send(null);
        } else {
            xmlhttp.send(null);
            return this.result(xmlhttp);
        }
    },

    // 状态改变
    state: function(xmlhttp, callBack) {
        return function() {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) callBack(xmlhttp.responseText);
                else callBack(false);
            };
        }
    },

    // 返回结果
    result: function(xmlhttp) {
        return (xmlhttp.status == 200) ? xmlhttp.responseText : false;
    }
}

tb.xml = {
    urlXML: function() {
        return "../Data/Map/KjHm/";
    },

    domXML: function() {
        if (tb.isIE) {
            var a = ["MSXML2.DOMDocument", "Microsoft.XMLDOM", "MSXML.DOMDocument", "MSXML3.DOMDocument"];
            for (var i = 0, l = a.length; i < l; i++) {
                try {
                    return new ActiveXObject(a[i]);
                } catch (e) { };
            }
            return false;
        } else {
            return document.implementation.createDocument("", "", null);
        };
    },

    // 载入XML
    callXML: function(url, callBack, isDom) {
        var oDom = this.domXML();
        oDom.async = !!callBack;
        if (oDom.async) {
            if (tb.isIE) {
                oDom.onreadystatechange = function() {
                    if (oDom.readyState == 4) {
                        callBack(isDom ? oDom : oDom.documentElement);
                    }
                }
            } else {
                oDom.onload = function() {
                    callBack(isDom ? oDom : oDom.documentElement);

                }
            }
            oDom.load(url);
        } else {
            oDom.load(url);
            return isDom ? oDom : oDom.documentElement;
        }
    },

    // 字符串转换为XML文档并返回xmlDom对象
    str2oDom: function(str) {
        if (tb.isIE) {
            var oDom = this.domXML();
            oDom.async = false;
            oDom.loadXML(str);
            return oDom.documentElement;
        } else {
            return new DOMParser().parseFromString(str, "application/xml").documentElement;
        };
    }
}

///////////////////////////////////////////////////////////////////

/* 连线类 */
tb.joinLine = function(color, size, gArray, indent) {
    this.color = color || "#E4A8A8";
    this.size = size || 2;
    this.g = gArray || [];
    this.show = true;   // 默认显示连线
    this.autoDraw = true; // 默认自动连线
    this.lines = [];
    this.tmpDom = null;
    this.visible = true;
    this.indent = indent || 8;
};
tb.joinLine.prototype = {
    remove: function() {
        for (var i = 0; i < this.lines.length; i++)
            this.lines[i].parentNode.removeChild(this.lines[i]);
        this.lines = [];
    },
    join: function(objArray, hide) {
        this.remove();
        this.visible = hide ? "visible" : "hidden";
        this.tmpDom = document.createDocumentFragment();
        for (var i = 0; i < objArray.length - 1; i++) {
            var a = this.pos(objArray[i]);
            var b = this.pos(objArray[i + 1]);
            if (document.all) {
                this.IELine(a.x, a.y, b.x, b.y);
            } else {
                this.FFLine(a.x, a.y, b.x, b.y);
            };
        };
        document.body.appendChild(this.tmpDom);
    },
    pos: function(obj) {
        var pos = { x: 0, y: 0 }, a = obj;
        for (; a; a = a.offsetParent) { pos.x += a.offsetLeft; pos.y += a.offsetTop };
        pos.x += parseInt(obj.offsetWidth / 2);
        pos.y += parseInt(obj.offsetHeight / 2);
        return pos;
    },
    FFLine: function(x1, y1, x2, y2) {
        var np = this.nPos(x1, y1, x2, y2, this.indent);  // 两端缩减函数（防止连线覆盖球）
        x1 = np[0]; y1 = np[1]; x2 = np[2]; y2 = np[3];
        var cvs = document.createElement("canvas");
        cvs.style.position = "absolute";
        cvs.style.visibility = this.visible;
        cvs.width = Math.abs(x1 - x2) || this.size;
        cvs.height = Math.abs(y1 - y2);
        var newY = Math.min(y1, y2);
        var newX = Math.min(x1, x2);
        cvs.style.top = newY + "px";
        cvs.style.left = newX + "px";
        var FG = cvs.getContext("2d");
        FG.save();             // 缓存历史设置
        FG.strokeStyle = this.color;
        FG.lineWidth = this.size;
        FG.globalAlpha = 0.9;  // 线透明度	
        FG.beginPath();
        FG.moveTo(x1 - newX, y1 - newY);
        FG.lineTo(x2 - newX, y2 - newY);
        FG.closePath();
        FG.stroke();
        FG.restore();          // 恢复历史设置
        this.lines.push(cvs);
        this.tmpDom.appendChild(cvs);
    },
    IELine: function(x1, y1, x2, y2) {
        var np = this.nPos(x1, y1, x2, y2, this.indent);  // 两端缩减函数（防止连线覆盖球）
        x1 = np[0]; y1 = np[1]; x2 = np[2]; y2 = np[3];
        var line = document.createElement("<v:line></v:line>");
        line.from = x1 + "," + y1;
        line.to = x2 + "," + y2;
        line.strokeColor = this.color;
        line.strokeWeight = this.size + "px";
        line.style.cssText = "position:absolute;z-index:99;top:0;left:0";
        line.style.visibility = this.visible;
        line.coordOrigin = "0,0";
        this.lines.push(line);
        this.tmpDom.appendChild(line);
    },
    nPos: function(x1, y1, x2, y2, r) {
        var a = x1 - x2, b = y1 - y2;
        var c = Math.round(Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)));
        var x3, y3, x4, y4;
        var _a = Math.round((a * r) / c);
        var _b = Math.round((b * r) / c);
        return [x2 + _a, y2 + _b, x1 - _a, y1 - _b];
    },
    draw: function() {
        if (this.autoDraw) this.join(this.g, this.show);
    }
}

///////////////////////////////////////////////////////////////////
