﻿// IE线段池
function LinePool() {
    var self = this;
    this.line = [];
    
    this.getLine = function(id, n, p1, p2, color, lineWidth, StartArrow, EndArrow, click, mouseover, mouseout) {
        var o = null;
        if (self.line.length > 0) {
            o = self.line.pop();
        }
        else {
            o = document.createElement('v:line');
            o.appendChild(document.createElement('<v:stroke StartArrow="" EndArrow=""></v:stroke>'));
        }
        o.sid = id;
        o.title = n;
        o.onclick = click;
        o.onmouseover = mouseover;
        o.onmouseout = mouseout;
        o.onmousedown = function() { event.cancelBubble = true; };
        o.strokecolor = color;
        o.strokeweight = lineWidth + 'px';
        o.from = p1.x + ',' + p1.y;
        o.to = p2.x + ',' + p2.y;
        o.style.position = 'absolute';
        o.style.left = "0px";
        o.style.top = "0px";
        o.getElementsByTagName('stroke')[0].StartArrow = StartArrow;
        o.getElementsByTagName('stroke')[0].EndArrow = EndArrow;
        return o;
    };

    this.rccLine = function(o) {
        o.onclick = null;
        o.onmouseover = null;
        o.onmouseout = null;
        o.onmousedown = null;
        self.line.push(o);
    };
}
var _LinePool = new LinePool();


// IE图形池
function ShapePool() {
    var self = this;
    this.sp = [];
    this.getPath = function(path) {
        var mp = path.split(',');
        var m = [];
        m.push('m ' + mp[0] + ',' + mp[1] + ' l ' + mp[2] + ',' + mp[3]);
        for (var i = 4; i < mp.length; i += 2) {
            m.push(' ' + mp[i] + ',' + mp[i + 1]);
        }
        return m.join(',') + 'xe';
    };
    this.getShape = function(sid, path, alpha, fillColor, borderColor, borderWith, txt, click, mouseover, mouseout) {
        var nodeShape = null;
        if (self.sp.length <= 0) {
            var s = '<v:shape style="position:absolute;width:10px;height:10px;overflow:visible;" coordsize="10,10" filled="t" fillcolor="#fff" stroked="t" strokecolor="blue" strokeweight="1px" path=""></v:shape>';
            nodeShape = document.createElement(s);
            nodeShape.appendChild(document.createElement('<v:fill opacity="' + alpha + '"></v:fill>'));
        }
        else {
            nodeShape = self.sp.pop();
            nodeShape.getElementsByTagName('fill')[0].opacity = alpha;
        }
        nodeShape.sid = sid;
        nodeShape.title = txt;
        nodeShape.onclick = click;
        nodeShape.onmouseover = mouseover;
        nodeShape.onmouseout = mouseout;
        nodeShape.onmousedown = function() { event.cancelBubble = true; };
        nodeShape.path = self.getPath(path);
        nodeShape.fillcolor = fillColor;
        nodeShape.strokeweight = borderWith + "px";
        nodeShape.strokecolor = borderColor;
        return nodeShape;
    };
    this.rccShape = function(nodeShape) {
        nodeShape.onclick = null;
        nodeShape.onmouseover = null;
        nodeShape.onmouseout = null;
        nodeShape.onmousedown = null;
        self.sp.push(nodeShape);
    };
}
var _ShapePool = new ShapePool();


function LayerCanvas(_mapobj) {
    var self = this;
    this.handler = document.createElement('DIV'); // 必须的，创建自已的图层容器
    this.handler.style.cssText = 'position:absolute;overflow:visible;z-index:66;';  // 不用设置 left,top ，注意 z-index的值
    
    // 地图回调事件

    // :: 拖动开始
    this.onmap_dragstart = function(evt) {
    };

    // :: 拖动中
    this.onmap_drag = function(evt) {
    };

    // :: 拖动结束
    this.onmap_dragend = function() {
        self.update();
        if (self.visible) self.handler.style.display = '';
    };

    // :: 放缩开始
    this.onmap_zoomstart = function() {
        self.handler.style.display = 'none';
    };

    // :: 放缩中
    this.onmap_zoom = function(omap) {
        // 注意进行坐标转换，否则需要在放缩开始前隐藏图层，避免放缩过程中显示错乱
    };

    // :: 放缩结束
    this.onmap_zoomend = function() {
        // 注意进行坐标转换，将坐标转到当前级别上
        self.update();
        if (self.visible) self.handler.style.display = '';
    };

    this.onmap_click = function(sender, arg) {
        if (arg.target._flag == self.handler._flag && arg.target._url && arg.target._url != '') {

        }
    };

    // :: 视口大小发生改变
    this.onmap_resize = function() {
        self.update();
    };

    // :: 销毁程序
    this.onmap_unload = function() {
        self.shapeClear();
        self.lineClear();
        self.handler = null;
    };

    this.lines = [];
    this.shapes = [];
    this.visible = true;

    this.update = function() {
        if (!self.visible) return;
        var scale = Math.pow(2, (gConfig.zoom.data - gMap.getZoom()));
        for (var i = 0; i < self.lines.length; i++) {
            var p = self.lines[i].params;
            var l = self.lines[i].line;
            l.from = Math.round(parseInt(p.p1.x) / scale) + ',' + Math.round(parseInt(p.p1.y) / scale);
            l.to = Math.round(parseInt(p.p2.x) / scale) + ',' + Math.round(parseInt(p.p2.y) / scale);
            l.style.left = "0px";
            l.style.top = "0px";
        }
        for (var i = 0; i < self.shapes.length; i++) {
            var p = self.shapes[i].path;
            var s = self.shapes[i].shape;
            s.path = self.getPath(self.zoomPath(p, scale));
        }
    };
    this.lineAdd = function(sid, sn, p1, p2, color, lineWidth, StartArrow, EndArrow, _click, _mouseover, _mouseout) {
        var scale = Math.pow(2, (gConfig.zoom.data - gMap.getZoom()));
        var lineParams = {
            id: sid,
            n: sn,
            p1: p1,
            p2: p2,
            color: (color) ? (color) : ('#0000ff'),
            lineWidth: (lineWidth) ? (lineWidth) : (1),
            StartArrow: (StartArrow) ? (StartArrow) : ('StartArrow'),
            EndArrow: (EndArrow) ? (EndArrow) : ('EndArrow'),
            click: _click,
            mouseover: _mouseover,
            mouseout: _mouseout
        };
        var oline = _LinePool.getLine(
            lineParams.id,
            lineParams.n,
            { x: Math.round(parseInt(p1.x) / scale), y: Math.round(parseInt(p1.y) / scale) },
            { x: Math.round(parseInt(p2.x) / scale), y: Math.round(parseInt(p2.y) / scale) },
            lineParams.color,
            lineParams.lineWidth,
            lineParams.StartArrow,
            lineParams.EndArrow,
            lineParams.click,
            lineParams.mouseover,
            lineParams.mouseout
        );
        self.lines.push({
            params: lineParams,
            line: oline
        });
        self.handler.appendChild(oline);
        return oline;
    };
    this.linePop = function() {
        if (self.lines.length <= 0) return;
        var o = self.lines.pop();
        self.handler.removeChild(o.line);
        _LinePool.rccLine(o.line);
    };
    this.lineClear = function() {
        while (self.lines.length > 0) {
            self.linePop();
        }
    };
    this.zoomPath = function(path, scale) {
        var m = path.split(',');
        for (var i = 0; i < m.length; i++) {
            m[i] = Math.round(parseInt(m[i]) / scale);
        }
        return m.join(',');
    };
    this.getPath = function(path) {
        var mp = path.split(',');
        var m = [];
        m.push('m ' + mp[0] + ',' + mp[1] + ' l ' + mp[2] + ',' + mp[3]);
        for (var i = 4; i < mp.length; i += 2) {
            m.push(' ' + mp[i] + ',' + mp[i + 1]);
        }
        return m.join(',') + ' xe';
    };
    this.shapeAdd = function(sid, path, alpha, fillColor, borderColor, borderWith, txt, click, mouseover, mouseout) {
        var scale = Math.pow(2, (gConfig.zoom.data - gMap.getZoom()));
        var shapePath = path;
        var shape = _ShapePool.getShape(sid, self.zoomPath(path, scale), alpha, fillColor, borderColor, borderWith, txt, click, mouseover, mouseout);
        self.shapes.push({
            'path': path,
            'shape': shape
        });
        self.handler.appendChild(shape);
        return shape;
    };
    this.shapePop = function() {
        if (self.shapes.length <= 0) return;
        var o = self.shapes.pop();
        self.handler.removeChild(o.shape);
        _ShapePool.rccShape(o.shape);
    };
    this.shapeClear = function() {
        while (self.shapes.length > 0) {
            self.shapePop();
        }
    };
}


//初始化
var layerCanvas = null;
if (!layerCanvas && gMap) {
    layerCanvas = new LayerCanvas(gMap);
    gMap.addLayer(layerCanvas);
}


/*
var s = gMap.getMapCenter();
var e = { x: s.x + 300, y: s.y + 300 };
layerCanvas.lineAdd('line1', 'line1', s, e, '#ff0000', 1, '', 'Classic', null, null, null);

参数：
sid          线段ID
sn           线段名称
p1           起点 {x:0,y:0}
p2           终点 {x:0,y:0}
color        颜色 '#ff0000'
lineWidth    宽度 数字
StartArrow   起点图标 Block、Classic、Diamond、Oval、Open
EndArrow     终点图标 Block、Classic、Diamond、Oval、Open
_click       点击事件
_mouseover   鼠标经过事件
_mouseout    鼠标离开事件
*/