﻿/* BaseMan.js*/

function _BaseMan() {
    var BaseMan = Class.create({
        //initialize: function(container, useIndexmap, useLevelbar, obj) {//, useStatic
        initialize: function(container, setting, obj) {//, useStatic
            this.container = container;
            this.container.oncontextmenu = function() { return false; };
            this.container.onselectstart = function() { return false; };
            this.container.style.background = 'url(' + WSpec.backgroundimg + ')';

            //WSpec.Options.useIndexmap = useIndexmap;
            //WSpec.Options.useLevelbar = useLevelbar;
            /*
            var setting = new Object();
            setting.IndexMapUseFlag = true;
            setting.LevelBarUseFlag = true;
            */
            if (setting.LogoUseFlag == undefined) {
                setting.LogoUseFlag = true;
                setting.LogoURL = "/images/Whereis.gif";
                setting.CopyrightUseFlag = true;
                setting.ScaleUseFlag = true;
            }

            WSpec.Options.useLogo = setting.LogoUseFlag;
            WSpec.setLogoUrl(setting.LogoURL);
            WSpec.Options.useCopyright = setting.CopyrightUseFlag;
            WSpec.Options.useScale = setting.ScaleUseFlag;
            WSpec.Options.useIndexmap = setting.IndexMapUseFlag;
            WSpec.Options.useLevelbar = setting.LevelBarUseFlag;

            if (obj && obj.coord) {
                WSpec.slon = obj.coord.lon;
                WSpec.slat = obj.coord.lat;
            }
            if (obj && obj.level) {
                WSpec.initlevel = WSpec.changeLevel(obj.level);
            }
            //WSpec.Options.useStatic = useStatic;

            this.BaseLayer = new Element('div');
            //this.BaseLayer.style.position = 'relative';
            this.BaseLayer.id = 'BaseLayer';
            //this.BaseLayer = WSpec.getBaseLayer(0, 'BaseLayer');
            //this.BaseLayer.style.position = 'relative';
            //this.BaseLayer.style.top = this.BaseLayer.style.left = 0;
            //this.BaseLayer.style.border = '2px solid yellow';

            this.container.appendChild(this.BaseLayer);

            WSpec.init(); 	// WSpec 초기화
            WSpec.setInit(this.container); //초기에 windowlayer에 사이즈에 따라서 다른 layer들 위치 설정
            WSpec.containerBorder = (this.container.offsetWidth - this.container.clientWidth) / 2;

            this.TileMan = new TileMan(this, this.BaseLayer);
            this.PoiMan = new PoiMan(this, this.BaseLayer);
            this.EtcMan = new EtcMan(this, this.BaseLayer);
            this.StaticMan = new StaticMan(this, this.BaseLayer);
            //if(WSpec.Options.useStatic)		this.StaticMan = new StaticMan(this, this.BaseLayer);
            //if(WSpec.Options.useLevelbar)	this.LevelBarMan = new LevelBarMan(this,this.BaseLayer);
            if (WSpec.Options.useLevelbar) this.LevelBarMan = new LevelBarMan(this, this.BaseLayer);
            if (WSpec.Options.useIndexmap) this.IndexMapMan = new IndexMapMan(this, this.BaseLayer);

            this.RGMan = new RGMan(this, this.BaseLayer);
            this.DrawMan = new DrawMan(this, this.BaseLayer);
            this.RMenuMan = new RMenuMan(this, this.BaseLayer);

            WSpec.leftmargin = container.offsetLeft;
            WSpec.topmargin = container.offsetTop;

            /* 이벤트 */
            //Event.observe(this.container, 'resize', this.resizeContainer.bindAsEventListener(this));
            Event.observe(window, 'resize', this.resizeContainer.bindAsEventListener(this));
            //Event.observe(window, 'load', this.load.bindAsEventListener(this));


            EventMan.addListener(this.TileMan, 'onMapMove', this.onMapMove.bindAsEventListener(this));
            EventMan.addListener(this.TileMan, 'moveTileLayer', this.moveTile.bindAsEventListener(this));
            EventMan.addListener(this.TileMan, 'stopTileLayer', this.stopTile.bindAsEventListener(this));
            EventMan.addListener(this.TileMan, 'zoomTileLayer', this.zoomTile.bindAsEventListener(this));
            EventMan.addListener(this.TileMan, 'clickTileLayer', this.clickTile.bindAsEventListener(this));
            EventMan.addListener(this.TileMan, 'moveTileLayerFromIndexMap', this.moveTileFromIndexMap.bindAsEventListener(this));
            EventMan.addListener(this.TileMan, 'redrawTileLayer', this.redrawTile.bindAsEventListener(this));

            if (WSpec.Options.useLevelbar) {
                EventMan.addListener(this.LevelBarMan, 'zoomInLevelBarLayer', this.zoomInLevelBar.bindAsEventListener(this));
                EventMan.addListener(this.LevelBarMan, 'zoomOutLevelBarLayer', this.zoomOutLevelBar.bindAsEventListener(this));
                EventMan.addListener(this.LevelBarMan, 'moveLevelBarLayer', this.moveLevelBar.bindAsEventListener(this));
                EventMan.addListener(this.LevelBarMan, 'panLevelBarLayer', this.panLevelBar.bindAsEventListener(this));
            }
            // Event Listener for IndexMapLayer.. 
            if (WSpec.Options.useIndexmap) {
                EventMan.addListener(this.IndexMapMan, 'zoomInIndexMapLayer', this.zoomInIndexMap.bindAsEventListener(this));
                EventMan.addListener(this.IndexMapMan, 'zoomOutIndexMapLayer', this.zoomOutIndexMap.bindAsEventListener(this));
                EventMan.addListener(this.IndexMapMan, 'moveIndexMapLayer', this.moveIndexMap.bindAsEventListener(this));
            }
            EventMan.addListener(this.PoiMan, 'drawPoi', this.sTileAppendLayer.bindAsEventListener(this));
            EventMan.addListener(this.PoiMan, 'poiClick', this.clickPoi.bindAsEventListener(this));
            //EventMan.addListener(this.PoiMan, 'poiMouseover', this.mouseoverPoi.bindAsEventListener(this));


            EventMan.addListener(this.RGMan, 'drawRG', this.sTileAppendLayer.bindAsEventListener(this));
            EventMan.addListener(this.RGMan, 'successRPRG', this.rpRGSuccess.bindAsEventListener(this));
            EventMan.addListener(this.RGMan, 'failRPRG', this.rpRGFail.bindAsEventListener(this));


            //DrawMan이벤트
            EventMan.addListener(this.DrawMan, 'drawPolyLine', this.sTileAppendLayer.bindAsEventListener(this));
            EventMan.addListener(this.DrawMan, 'drawCircleLayer', this.sTileAppendLayer.bindAsEventListener(this));

            EventMan.addListener(this.DrawMan, 'drawman_mousedown', this.mousedown.bindAsEventListener(this));
            EventMan.addListener(this.DrawMan, 'drawman_mousemove', this.mousemove.bindAsEventListener(this));
            EventMan.addListener(this.DrawMan, 'drawman_mouseup', this.mouseup.bindAsEventListener(this));
            EventMan.addListener(this.DrawMan, 'drawman_mousewheel', this.mousewheel.bindAsEventListener(this));
            EventMan.addListener(this.DrawMan, 'drawman_dblclick', this.dblclick.bindAsEventListener(this));
            EventMan.addListener(this.DrawMan, 'drawman_click', this.click.bindAsEventListener(this));

            //Etc<an이벤트 
            EventMan.addListener(this.EtcMan, 'mousedown', this.mousedown.bindAsEventListener(this));
            EventMan.addListener(this.EtcMan, 'mousemove', this.mousemove.bindAsEventListener(this));
            EventMan.addListener(this.EtcMan, 'mouseup', this.mouseup.bindAsEventListener(this));
            EventMan.addListener(this.EtcMan, 'mousewheel', this.mousewheel.bindAsEventListener(this));
            EventMan.addListener(this.EtcMan, 'dblclick', this.dblclick.bindAsEventListener(this));
            EventMan.addListener(this.EtcMan, 'click', this.click.bindAsEventListener(this));

            //alert('this.BaseLayer.childNodes.length = ' + this.BaseLayer.childNodes.length);
            EventMan.trigger(this, 'onCreate');
            //EventMan.trigger(this,'onCreate2',{});
            //this.onMizcom();
        },
        setPoiCnt: function(cnt) {
            WSpec.maxpoicnt = cnt;
            this.PoiMan.initAddPoi();

        },
        plusPoiMaxCnt: function(cnt) {
            this.PoiMan.plusPoiMaxCnt(cnt);
        },
        getPoiMaxCnt: function() {
            var maxpoi = this.PoiMan.getPoiMaxCnt();
            return maxpoi;
        },
        /*
        onMizcom: function() { 
        alert('pp');
        EventMan.trigger(this,'onCreate');
        //EventMan.trigger(this,'onMapMove',{x:1, y:1, level:1});
        },
        */


        /*RMenuMan add*/
        addMenuItem: function(id, title, func) { //메뉴등록
            this.RMenuMan.addMenuItem(id, title, func);
        },
        removeMenuItem: function(id) {
            this.RMenuMan.removeMenuItem(id);
        },
        getRMenuCoord: function() {
            var menuCoord = this.RMenuMan.getRMenuCoord();
            return menuCoord;
        },
        rMenuHide: function() {
            this.RMenuMan.hide();
        },

        /*PoiMan add*/
        infoboxShow: function(gid, pid, contents) {
            this.PoiMan.infoShow(gid, pid, contents);

        },
        clickPoi: function(val) { //외부로 발생시키는 이벤트 발생
            EventMan.trigger(this, 'onPoiClick', val);
        },
        /*
        mouseoverPoi: function(val) {
        //alert('base');
        var gid = val["gid"];
        var pid = val["pid"];
        this.PoiMan.hintShow(gid,pid);
        },
        */
        stickPoi: function() {
            this.PoiMan.stickPoi();
        },
        addPoi: function(gid, pid, icon, text, coord, positiontype, imgw, imgh, hinttitle, hintbody, showtextS, showtextE) {
            //addPoi: function(gid, pid, text, coord, positiontype, hinttitle, hintbody) {		
            var result = this.PoiMan.addPoi(gid, pid, icon, text, coord, positiontype, imgw, imgh, hinttitle, hintbody, showtextS, showtextE);
            //var result = this.PoiMan.addPoi(gid, pid, WSpec.poiimage, text, coord, positiontype, WSpec.poiimgw, WSpec.poiimgh, hinttitle, hintbody);
            if (result) {
                this.PoiMan.setPosPoi(gid, pid, this.getInnerLevel()); //poi위치결정해서 sTileLayer에 붙이는 함수
            }
            //this.PoiMan.resetPosPoi(this.getInnerLevel());
        },
        removePoi: function(gid, pid) {
            this.PoiMan.removePoi(gid, pid);
        },
        removeGroupPoi: function(gid) {
            this.PoiMan.removeGroupPoi(gid);
        },

        sTileAppendLayer: function(val) {
            var level = val["level"];
            var layer = val["layer"];
            this.TileMan.sTileAppend(level, layer);
        },
        getLayerPosition: function(level) {
            var position = this.TileMan.getLayerPosition(level);
            return position;

        },
        setLayerPosition: function(level, x, y) {
            this.TileMan.setLayerPosition(level, x, y);

        },
        resizeContainer: function() {
            WSpec.setInit(this.container);
            EventMan.trigger(this, 'resizeWindow');
            EventMan.trigger(this, 'onMapResize');
        },

        /*EtcMan Event함수*/
        mousedown: function(event) {
            this.TileMan.startMove(event);
        },
        mousemove: function(event) {
            this.TileMan.processMove(event);
        },
        mouseup: function(event) {
            this.TileMan.stopMove(event);
        },
        mousewheel: function(event) {
            this.TileMan.wheel(event);
        },
        dblclick: function(event) {
            this.TileMan.dblClick(event);
        },
        click: function(event) {
            this.TileMan.click(event);
        },

        /*TileMan Event함수*/
        onMapMove: function(val) {
            var coord = val.coord;
            var level = val.level;
            EventMan.trigger(this, 'onMapMove', { "coord": coord, "level": level });


            // 타이머로 바꿔본다.. mizcom
            //var f = EventMan.trigger.bind(this)(this, 'onMapMove' , {"coord": coord, "level": 13 - level});
            //setTimeout(f, 500);
        },
        moveTile: function(val) {
            var coord = val["coord"];
            var level = val["level"];
            EventMan.trigger(this, 'moveTileLayer', { "coord": coord, "level": level });
        },
        stopTile: function() {
            EventMan.trigger(this, 'stopTileLayer');
        },
        zoomTile: function(level) {
            EventMan.trigger(this, 'zoomTileLayer', level);
            EventMan.trigger(this, 'onChangeLevel', WSpec.changeLevel(level)); //바깥과 통신하는 이벤트
        },
        clickTile: function(val) { //point, coord둘다 받음
            var point = val["point"];
            var coord = val["coord"];
            var button = val["button"];
            if (button == 0) {
                EventMan.trigger(this, 'clickTileLayer');
            }
            else if (button == 2) {
                EventMan.trigger(this, 'clickRMenu', val);
                EventMan.trigger(this, 'onRMapClick', val); //오른메뉴 클릭 좌표 바깥과 통신
            }
            EventMan.trigger(this, 'onMapClick', { "point": point, "coord": coord });
        },
        moveTileFromIndexMap: function() {
            EventMan.trigger(this, 'moveTileLayerFromIndexMap');
        },
        redrawTile: function() {
            EventMan.trigger(this, 'redrawTileLayer');
        },


        /*IndexMap Event함수*/
        zoomInIndexMap: function() {
            EventMan.trigger(this, 'zoomInIndexMapLayer');
        },
        zoomOutIndexMap: function() {
            EventMan.trigger(this, 'zoomOutIndexMapLayer');
        },
        moveIndexMap: function(coord) {
            EventMan.trigger(this, 'moveIndexMapLayer', coord);
        },

        /*LevelBar Event함수*/
        zoomInLevelBar: function() {
            EventMan.trigger(this, 'zoomInLevelBarLayer');
        },
        zoomOutLevelBar: function() {
            EventMan.trigger(this, 'zoomOutLevelBarLayer');
        },
        moveLevelBar: function(level) {
            EventMan.trigger(this, 'moveLevelBarLayer', level);
        },
        panLevelBar: function(kind) {
            EventMan.trigger(this, 'panLevelBarLayer', kind);
        },


        /*
        setLevel : function(level) {
        this.TileMan.setLevel(level); //changeLevel -> setLevel
        this.LevelBarMan.setLevel(level);
        s
        EventMan.Trigger(this, 'setlevel');
        },
        */
        /*PolyLine 그리기*/
        delPolyLines: function(index) {
            this.DrawMan.delPolyLines(index);
        },
        addPolyLines: function(idx, coordList, stroke) {
            this.DrawMan.addPolyLines(idx, coordList, stroke);
        },
        requestDraw: function() {
            this.DrawMan.requestDraw();
        },
        getLineStroke: function(r, g, b, a, w) {
            return this.DrawMan.getLineStroke(r, g, b, a, w);
        },
        setDrawCenterAndZoom: function() {
            this.DrawMan.setDrawCenterAndZoom();
        },

        /*RPRG 경로 검색 요청*/
        requestRPRG: function(coords, option) {
            this.RGMan.requestRPRG(coords, option);
        },
        setStartCoord: function(coord) {
            this.RGMan.setStartCoord(coord);
        },
        setEndCoord: function(coord) {
            this.RGMan.setEndCoord(coord);
        },
        setPassCoord: function(coord) {
            return this.RGMan.setPassCoord(coord);
        },
        delPassCoord: function(index) {
            this.RGMan.delPassCoord(index);
        },
        initCoords: function() {
            this.RGMan.initCoords();
        },
        setRPOption: function(option) {
            this.RGMan.setRPOption(option);
        },
        callRPRG: function() {
            this.RGMan.callRPRG();
        },
        clearRPRG: function() {
            this.RGMan.clearRPRG();
        },
        getRPRGData: function() {
            return this.RGMan.getRPRGData();
        },
        getRPOption: function() {
            return this.RGMan.getRPOption();
        },
        rpRGSuccess: function() {
            EventMan.trigger(this, 'onRPRGSuccess');
        },
        rpRGFail: function(val) {
            EventMan.trigger(this, 'onRPRGFail', val);
        },
        setRPCoords: function(coords) {
            this.RGMan.setRPCoords(coords);
        },
        getRPCoords: function() {
            return this.RGMan.getRPCoords();
        },


        /*외부 사용메소드 */
        setCenter: function(coord) {
            this.TileMan.setCenter(coord);

            //EventMan.Trigger(this, 'move');
        },
        setCenterAndZoom: function(coord, userlevel) {
            var innerlevel = WSpec.changeLevel(userlevel);
            this.TileMan.setCenterAndZoom(coord, innerlevel);
        },
        getBound: function() {
            var boundary = this.TileMan.getBound();
            return boundary;
        },
        getCenter: function() {
            var coord = this.TileMan.getCenter();
            return coord;
        },
        setMapLevel: function(userlevel) {
            //var innerlevel = WSpec.changeLevel(userlevel);
            var innerlevel = userlevel;
            if (innerlevel < WSpec.minlevel) {
                innerlevel = WSpec.minlevel;
            } else if (innerlevel > WSpec.maxlevel) {
                innerlevel = WSpec.maxlevel;
            } else if (isNaN(innerlevel)) {
                innerlevel = WSpec.maxlevel;
            }

            var level = this.TileMan.setMapLevel(innerlevel);
            //var level = WSpec.changeLevel(this.TileMan.setMapLevel(innerlevel));
            return level;
        },
        getMapLevel: function() { //외부level (한반도가 레벨12)
            //var level = WSpec.changeLevel(this.getInnerLevel());
            //var level = WSpec.userLevel(this.TileMan.getMapLevel());
            var level = this.getInnerLevel();
            return level;
        },
        getInnerLevel: function() { //내부level (한반도가 레벨1)
            var level = this.TileMan.getMapLevel();
            return level;
        },
        zoomIn: function() {
            //alert('k');+
            //var level = 13 - this.TileMan.zoomIn();
            //var level = WSpec.changeLevel(this.TileMan.zoomIn());
            var level = this.TileMan.zoomIn();
            return level;
        },
        zoomOut: function() {
            //var level = 13 - this.TileMan.zoomOut();
            //var level = WSpec.changeLevel(this.TileMan.zoomOut());
            var level = this.TileMan.zoomOut();
            return level;
        },
        getDpPixel: function() {
            var point = new WPoint();
            point.x = WSpec.viewportwidth;
            point.y = WSpec.viewportheight;
            return point;
        },
        setContainerSize: function() { //윈도우resize말고 지도가 resize됐을때 이 함수를 실행해서 지도 크기 조절.
            this.resizeContainer();
        },


        //일문지도용.
        drawRadius: function(coord, radius, stroke, fill) { //setting(color, thickness, alpha)
            this.DrawMan.drawRadius(coord, radius, stroke, fill);

        },
        draw2Radius: function(coord, level, radius1, stroke1, fill1, radius2, stroke2, fill2) {
            this.DrawMan.draw2Radius(coord, level, radius1, stroke1, fill1, radius2, stroke2, fill2);
        },

        getScale: function(level) {
            return this.StaticMan.getScale(level);
        }
        /*
        userLevel: function(level) {
        var level = 13 - level;
        return level;
			
		}*/

    });
	window.BaseMan = BaseMan;
};
_BaseMan();	
