/*
*地图主函数
*
*
*/
//document.write('<script language="javascript" src="mapjs/Fun.js"></script>');

//兼容旧程序
var ZB_x = 2488; //原点坐标 广电大楼位置
var ZB_y = 2540;
var ZB_zhuanhuan = 1.46; //一象素代表多少地图单位 象素/ZB_zhuanhuan = 米

//开始建地图
function setmapstart(x, y, mappath9) {
    var p = new Point(x, y);
    MAPSTART('mapdiv_ver3', posChange(p, true, mappath9), mappath9);
    //pointjs.src = 'http://127.0.0.98/showobject.asp?g=4,5,6,7,8,9,10,11,21,22,23,24,25,26,27,28,29,30,31';
}
//==============================================================================
var isDebug = true;    //调试模式
var mapWidth = 0; //地图宽度
var mapHeight = 0;  //地图高度
var maplevel = 8;      //地图缩放级别
var imgBaseDir = "http://img.glinan.com/map";    //地图文件夹
var mousedown = 0;         //鼠标是否按下
var mouseX = 0;            //鼠标点下的位置
var mouseY = 0;
var maparr = new Array();  //地图数组
var mapPoint = new Array(); //地图标志
var maindiv;

function MAPSTART(outerdiv, firstpos, level) {

    maplevel = level;      //地图缩放级别
    mapWidth = parseInt($(outerdiv).offsetWidth); //地图宽度
    mapHeight = parseInt($(outerdiv).offsetHeight);  //地图高度

    //初始化地图数据
    maindiv = new __mapDIV();
    maindiv.mapground.style.left = -firstpos.x + mapWidth/2;
    maindiv.mapground.style.top = -firstpos.y + mapHeight/2;
    samepos();

    maindiv.maindiv.onmousedown = function() { map_mousedown(event); }
    maindiv.maindiv.onmousewheel = function() { map_mousewheel(event); return false;}
    $(outerdiv).appendChild(this.maindiv.maindiv);
    
    //初始化地图
    this.__mapreset = function() {
        /* 浏览器 */
        $(outerdiv).ondragstart = function() { return false } //禁止拖放
        $(outerdiv).oncontextmenu = function() { event.returnValue = false } //禁用菜单
        $(outerdiv).onselectstart = function() { return false } //禁止选择
        var ei = $C('<meta http-equiv="imagetoolbar" content="no" />');
        var head = document.getElementsByTagName("head")[0];
        head.appendChild(ei);   //禁用图片提示
    }
    this.__mapreset();

    //this.maindiv.map.appendChild(new mapitem(0, 0).img);
    //this.maindiv.map.appendChild(new mapitem(256, 256).img);
    //移动位置后刷新地图
    this.flashmap = function() {
        var newarr = createMap(); //新地图
        var t = map_check(newarr, this.maparr);
        delMap(t[0]); //删除不显示的
        showMap(t[1]); //只显示增加的图片
        this.maparr = newarr;
    }
    
    //初始生成地图
    this.maparr = createMap();
    showMap(this.maparr);

}
//==============================================================================
//将几个层位置设为一致
function samepos() {
    maindiv.mapmark.style.left = maindiv.mapground.style.left;
    maindiv.mapmark.style.top = maindiv.mapground.style.top;
    maindiv.mapmark2.style.left = maindiv.mapground.style.left;
    maindiv.mapmark2.style.top = maindiv.mapground.style.top;
}
//获得地图中心坐标
function getmapmidpos(){
        var px = parseInt(mapWidth / 2) - parseInt(maindiv.mapground.style.left);
        var py = parseInt(mapHeight / 2) - parseInt(maindiv.mapground.style.top);
        return new Point(px,py);
}
//==============================================================================
//生成地图DIV对象
function __mapDIV() {

    //容器，最底层
    this.mapground = $C('div');
    this.mapground.className = 'map__ground';
    this.mapground.style.left = '0px';
    this.mapground.style.top = '0px';
    //地图层
    this.map = $C('div');
    this.map.className = 'map__pic';
    this.map.style.left = '0px';
    this.map.style.top = '0px';
    this.mapground.appendChild(this.map);
    //文字、标志层
    this.mapmark = $C('div');
    this.mapmark.className = 'map__mark';
    this.mapmark.style.left = '0px';
    this.mapmark.style.top = '0px';
    this.mapmark2 = $C('div');
    this.mapmark2.className = 'map__mark2';
    this.mapmark2.style.left = '0px';
    this.mapmark2.style.top = '0px';
    //this.mapground.appendChild(this.mapmark);
    //外框
    this.maindiv = $C('div');
    this.maindiv.className = 'map__div';
    this.maindiv.appendChild(this.mapground);
    this.maindiv.appendChild(this.mapmark);
    this.maindiv.appendChild(this.mapmark2);
}
//==============================================================================
//根据当前的数据状态返回地图数组
function createMap() {
    var maparr = new Array();
    var x = parseInt((-255 - parseInt(maindiv.mapground.style.left)) / 256) * 256;
    var y = parseInt((-255 - parseInt(maindiv.mapground.style.top)) / 256) * 256;
    x2 = x + mapWidth + 512;
    y2 = y + mapHeight + 512;
    for (var i = x; i <= x2; i += 256) {
        for (var j = y; j < y2; j += 256) {
            maparr.push(new mapitem(i, j));
        }
    }
    return maparr;
}
//==============================================================================
//POS地理和像素转换   pos 位置数据，pix 是否转换成像素
function posChange(pos, pix, maplevel) {
    if (pix == null) pix = false;
    if (pix) {
        var nx = (pos.x + ZB_x) * ZB_zhuanhuan / maplevel;
        var ny = (pos.y + ZB_y) * ZB_zhuanhuan / maplevel;
        return new Point(nx, ny);
    } else {
        var nx = (pos.x * maplevel) / ZB_zhuanhuan - ZB_x;
        var ny = (pos.y * maplevel) / ZB_zhuanhuan - ZB_y;
        return new Point(nx, ny);
    }
}
//==============================================================================
//根据地图Array进行画图
function showMap(maparr) {
    for (var i = 0; i < maparr.length; i++) {
        maindiv.map.appendChild(maparr[i].img);
    }
}
//==============================================================================
//调整标志位置 超出边界则不显示 f 是否全部重新画
function pointResize(f) {
    if (f == null) f = false;
    var rect = new Array();
    rect[0] = new Point(-parseInt(maindiv.mapground.style.left) - 1, -parseInt(maindiv.mapground.style.top) - 1);
    rect[1] = new Point(mapWidth - parseInt(maindiv.mapground.style.left) + 2, mapHeight - parseInt(maindiv.mapground.style.top) + 2);
    for (var i = 0; i < mapPoint.length; i++) {
        if (f) {
            try {
                maindiv.mapmark.removeChild($(mapPoint[i].img.id));
                maindiv.mapmark2.removeChild($(mapPoint[i].title.id));
            } catch (e) { }
        }
        var nx=(mapPoint[i].pos.x + ZB_x) * ZB_zhuanhuan / maplevel - 4;
        var ny = (mapPoint[i].pos.y + ZB_y) * ZB_zhuanhuan / maplevel - 4;

        if (inRect(nx, ny, rect)) {//是否在地图内
            mapPoint[i].img.style.left = nx;
            mapPoint[i].img.style.top = ny;
            mapPoint[i].title.style.left = nx + 9;
            mapPoint[i].title.style.top = ny - 3;
            if ($(mapPoint[i].id) == null) {
                maindiv.mapmark.appendChild(mapPoint[i].img);
                if (maplevel < 4) maindiv.mapmark2.appendChild(mapPoint[i].title);
            }
        } else { //到图外去了
            if ($(mapPoint[i].id)) {
                try {
                    maindiv.mapmark.removeChild($(mapPoint[i].img.id));
                    maindiv.mapmark2.removeChild($(mapPoint[i].title.id));
                } catch (e) { }
            }
        }
    }
    try { maindiv.mapmark.style.display = 'block'; maindiv.mapmark2.style.display = 'block'; } catch (e) { }
    //testbutton.innerText = Math.random();
}
//==============================================================================
//删除不显示的图片
function delMap(maparr) {
    for (var i = 0; i < maparr.length; i++) {
        try {
            maindiv.map.removeChild($(maparr[i].img.id));
            maparr[i] = null;
        } catch (e) { }
    }
}
//==============================================================================
//对比地图数组，找出A(新)，B(旧)组中各没有的
function map_check(arrA, arrB) {
    var temp = {};
    var result1 = new Array();
    var result2 = new Array();
    for (var i = 0; i < arrA.length; i++) {
        temp[arrA[i].id] = 1;
    }
    for (var i = 0; i < arrB.length; i++) {
        if (temp[arrB[i].id]) {//重叠部分
            temp[arrB[i].id] = 2;
        } else { //A中没有部分（需删除部分）
            result1.push(arrB[i]);
        }
    }
    for (var i = 0; i < arrA.length; i++) {
        if(temp[arrA[i].id] == 1)result2.push(arrA[i]);//B中没有的，需增加
    }
    var t = new Array();
    t.push(result1);t.push(result2);
    return t;
}
//==============================================================================
//鼠标按下
var time_draw;
function map_mousedown(event) {
    //return;
    //alert(event.srcElement.className);
    if (event.button == 3) return; //右键未结束，不处理左键

    mousedown = event.button;
    mouseX = event.clientX;
    mouseY = event.clientY;
    if (event.button == 1) {
        maindiv.maindiv.style.cursor = 'move';
        time_draw = window.setInterval(function() { flashmap(); pointResize(); }, 200);
    }
    if (event.button == 2) {//鼠标右键 放大缩小
	if($('xiaoqi'))return;
        old_px = 0.5; old_zoom = 0.5; maindiv.maindiv.style.cursor = 'n-resize';
        maindiv.mapmark.style.display = 'none';
        maindiv.mapmark2.style.display = 'none';
    }
    document.body.onmousemove = function() { map_mousemove(event); }
    document.body.onmouseup = function(event) {
        document.body.onmousemove = function() { }
        maindiv.maindiv.style.cursor = '';
        if (mousedown == 2) {//缩放后
            if (zoomlevel == 3 || zoomlevel == 2 || zoomlevel == 1) { //缩小1/4
                if (maplevel < 4) {
                    maplevel = maplevel * 4;
                    maindiv.mapground.style.left = -old_px / 4 + parseInt(mapWidth / 2);
                    maindiv.mapground.style.top = -old_py / 4 + parseInt(mapHeight / 2);
                } else {
                    zoomlevel = 4; //降级到1/2
                }
            }
            if (zoomlevel == 7 || zoomlevel == 6 || zoomlevel == 5 || zoomlevel == 4) { //缩小1/2
                if (maplevel < 8) {
                    maplevel = maplevel * 2;
                    maindiv.mapground.style.left = -old_px / 2 + parseInt(mapWidth / 2);
                    maindiv.mapground.style.top = -old_py / 2 + parseInt(mapHeight / 2);
                }
            }
            if (zoomlevel > 16) { //放大4倍
                if (maplevel >= 4) {
                    maplevel = maplevel / 4;
                    maindiv.mapground.style.left = -old_px * 4 + parseInt(mapWidth / 2);
                    maindiv.mapground.style.top = -old_py * 4 + parseInt(mapHeight / 2);
                } else {
                    zoomlevel = 9; //降级到*2
                }
            }
            if (zoomlevel > 8 && zoomlevel <= 16) { //放大2倍
                if (maplevel > 1) {
                    maplevel = maplevel / 2;
                    maindiv.mapground.style.left = -old_px * 2 + parseInt(mapWidth / 2);
                    maindiv.mapground.style.top = -old_py * 2 + parseInt(mapHeight / 2);
                }
            }
            samepos();
            var newarr = createMap(); //新地图
            showMap(newarr); //只显示增加的图片
            delMap(maparr);
            pointResize(true);
            maparr = newarr;
        }
        //移动后
        if (mousedown == 1) {
            samepos();
            flashmap();
            pointResize();
        }
        mousedown = 0;
        try{window.clearInterval(time_draw);}catch(e){}
        //testbutton.innerText = maindiv.mapground.children[0].children.length;
    }
}
//==============================================================================
//鼠标移动
function map_mousemove(event) {
    if (mousedown == 1) {//左键，移动地图
        maindiv.mapground.style.left = parseInt(maindiv.mapground.style.left) - (mouseX - event.clientX);
        maindiv.mapground.style.top = parseInt(maindiv.mapground.style.top) - (mouseY - event.clientY);
        samepos();
        mouseX = event.clientX; mouseY = event.clientY;
    }
    if (mousedown == 2) {
        zoomtime(parseInt((mouseY - event.clientY) / 20));
    }
}
//==============================================================================
//鼠标滚轮
function map_mousewheel(event) {
    if (event.wheelDelta > 0 && maplevel > 1) {
        old_px = 0.5; old_zoom = 0.5;
        zoomtime(4);
        maplevel = maplevel /2;
        maindiv.mapground.style.left = -old_px *2 + parseInt(mapWidth / 2);
        maindiv.mapground.style.top = -old_py * 2 + parseInt(mapHeight / 2);
        samepos();
        var newarr = createMap(); //新地图
        showMap(newarr); //只显示增加的图片
        delMap(maparr);
        pointResize(true);
        maparr = newarr;
    }
    if (event.wheelDelta < 0 && maplevel < 8) {
        old_px = 0.5; old_zoom = 0.5;
        zoomtime(-4);
        maplevel = maplevel * 2;
        maindiv.mapground.style.left = -old_px / 2 + parseInt(mapWidth / 2);
        maindiv.mapground.style.top = -old_py / 2 + parseInt(mapHeight / 2);
        samepos();
        var newarr = createMap(); //新地图
        showMap(newarr); //只显示增加的图片
        delMap(maparr);
        pointResize(true);
        maparr = newarr;
    }
    return false;
}
//图片缩===========================================================================
var zoomlevel = 8;
var old_px = 0.5;
var old_py = 0.5;
var old_zoom = 0.5;
function zoomtime(val) {
    if (old_zoom != 0.5 && old_zoom == val) return;//已经缩放过，不用重复计算
    if (val == null) return; //空，不缩放
    if (val > 0 && maplevel <= 1) return;//太大
    if (val < 0 && maplevel >= 8) return;//太小
    
    if (val > 0) {//放大
        if (val > 8) val = 8;
        zoomlevel = 8 + val * 2;
        zoomlevel2 = 8;
    } else {//缩小
        if (val < -6) val = -6;
        zoomlevel = 8 + val;
        zoomlevel2 = 8;
    }
    old_zoom = val;//记录上次变化
    if (old_px == 0.5) {//初始化
        //相对地图中点坐标
        var px = parseInt(mapWidth / 2) - parseInt(maindiv.mapground.style.left);
        var py = parseInt(mapHeight / 2) - parseInt(maindiv.mapground.style.top);
        old_px = px;
        old_py = py;
    } else {
        px = old_px;
        py = old_py;
    }
    //坐标转换*********************
    var temp = 256;
    temp = temp * zoomlevel / zoomlevel2;
    px2 = px * zoomlevel / zoomlevel2;
    py2 = py * zoomlevel / zoomlevel2;
    //调整地图图片位置和大小
    for (var i = 0; i < maparr.length; i++) {
        $(maparr[i].img.id).style.width = temp;
        $(maparr[i].img.id).style.height = temp;
        var nx = maparr[i].pos.x - px;
        var ny = maparr[i].pos.y - py;
        $(maparr[i].img.id).style.left = nx * zoomlevel / zoomlevel2 + px2;
        $(maparr[i].img.id).style.top = ny * zoomlevel / zoomlevel2 + py2;
    }
    maindiv.mapground.style.left = - px2 + parseInt(mapWidth / 2);
    maindiv.mapground.style.top = -py2 + parseInt(mapHeight / 2);
    samepos();
}
//==============================================================================
//地图标志
var objectoff;//兼容旧程序
function creatpoint(x, y, name, url, ico) {
    if (maindiv) { } else { return };
    var temp = new __mapPoint(x, y, name, url, ico);
    mapPoint.push(temp);
    temp.img.style.left = (x + ZB_x) * ZB_zhuanhuan / maplevel - 4;
    temp.img.style.top = (y + ZB_y) * ZB_zhuanhuan / maplevel - 4;
    temp.title.style.left = parseInt(temp.img.style.left) + 9;
    temp.title.style.top = parseInt(temp.img.style.top) - 3;
    maindiv.mapmark.appendChild(temp.img);
    maindiv.mapmark2.appendChild(temp.title);
}
//==============================================================================
//清除旧地图标志
function clearobject() {
    for (var i = 0; i < mapPoint.length; i++) {
        try {
            maindiv.mapmark.removeChild($(mapPoint[i].img.id));
            maindiv.mapmark2.removeChild($(mapPoint[i].title.id));
        } catch (e) { }
    }
    mapPoint = new Array();
}
//==============================================================================
//单点标志方法
//biaozhi(1200,-82.1,'亚君内衣专卖店','images\icon_qi.gif');setmapstart(1200,-82.1);
function biaozhi(x,y,name,ico) {
    //biaozhi(147.9, 383.5, '宝贝囡囡', 'images\icon_qi.gif');
    var url = '';
    var temp = new __mapPoint(x, y, name, url, ico);
    mapPoint.push(temp);
    temp.img.style.left = (x + ZB_x) * ZB_zhuanhuan / maplevel - 4;
    temp.img.style.top = (y + ZB_y) * ZB_zhuanhuan / maplevel - 4;
    temp.title.style.left = parseInt(temp.img.style.left) + 9;
    temp.title.style.top = parseInt(temp.img.style.top) - 3;
    maindiv.mapmark.appendChild(temp.img);
    maindiv.mapmark2.appendChild(temp.title);
}
//==============================================================================
//动画移动地图
var mapmove_time;
function start_mapmove(pos,level) {//移动到位置，放大到几层
    //新建立移动目标，如果移动还未停止，则先停止之前的移动。
    //如果太远则直接移动到近目标，再开始移动
    if (Distance(pos, getmapmidpos) > 900) alert('far away');
    try {
        window.clearTimeout(mapmove_time);
    } catch (e) { }
    __mapmove(pos,0);
    
}
//==============================================================================
//pos 移动目的位置,t 次数
function __mapmove(pos2, t) {
    t++;
    pos = posChange(pos2, true, maplevel); //转换成像素单位
    if (t < 6) {
        //取半程移动
        var p = getmapmidpos();
        maindiv.mapground.style.left = parseInt(maindiv.mapground.style.left) + (p.x - pos.x)/2;
        maindiv.mapground.style.top = parseInt(maindiv.mapground.style.top) + (p.y - pos.y)/2;
        samepos();
        //flashmap();
        mapmove_time = window.setTimeout(function() { __mapmove(pos2,t) }, 50);
    } else {
        //移动结束
        maindiv.mapground.style.left = mapWidth/2 - pos.x;
        maindiv.mapground.style.top = mapHeight / 2 - pos.y;
        var vE = new vEvent(200, 1);
        map_mousewheel(vE);
        map_mousewheel(vE);
        //map_mousewheel(vE);
        samepos();
        flashmap();
        pointResize();
        show_shuoming(pos2);
    }
}
//==============================================================================
//聚焦某点
function gopoint(id) {
    for (var i = 0; i < mapPoint.length; i++) {
        if (mapPoint[i].img.id == id || mapPoint[i].title.id == id) {
            start_mapmove(mapPoint[i].pos, 1);
            shuomingurl = 'shuoming2.asp?id=' +mapPoint[i].url;
            return;
        }
    }
}

//==============================================================================
//虚拟事件对象
function vEvent(a,b) {
    this.wheelDelta = a;
    this.button = b;
}
//==============================================================================
//显示说明
var shuomingdiv = $C('div');
var shuomingurl = '';
shuomingdiv.className = 'shuoming';
shuomingdiv.innerHTML = '<div></div><iframe src="shuoming2.asp" width=325 height=130 scrolling=no name="if_shuoming" id="if_shuoming" border="0" frameborder="0"></iframe>';
function show_shuoming(pos) {
    try { maindiv.mapmark.removeChild(shuomingdiv); } catch (e) { }
    var p = posChange(pos, true, maplevel); //转换成像素单位
    shuomingdiv.style.top = p.y - 212;
    shuomingdiv.style.left = p.x - 164;
    maindiv.mapmark.appendChild(shuomingdiv);
    $('if_shuoming').src = shuomingurl;

}
//关闭说明
function closeshuoming() {
    try { maindiv.mapmark.removeChild(shuomingdiv); } catch (e) { }
}
