﻿function _FMapLib() {
    var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
    var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
    var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
    var isSafari = (navigator.userAgent.indexOf("Safari") != -1) ? true : false;
    var debug = false;
    window.fmap_cnt = 0;

    //alert(isIE + ',' + isWin + ',' + isOpera + ',' + isSafari);
    /* // FMap //*/
    function FMapObj(movieURL, container, flashID, mapname, LON, LAT, LEVEL, FMapSpec, PageConfig, CustomElements, MapKind, IP, PortNo, RepeatCnt, CpCode, SVCCode, Num, CoordSys) {
        var srcStr = AC_FL_RunContent(
			'src', movieURL, 'id', flashID, 'name', flashID, 'movie', movieURL,
			'flashvars', 'JsObjName=' + mapname + '&sLON=' + LON + '&sLAT=' + LAT + '&sLEVEL=' + LEVEL + '&FMapSpec=' + FMapSpec + '&PageConfig=' + PageConfig + '&CustomElements=' + CustomElements + '&MapKind=' + MapKind,
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
			'width', '100%', 'height', '100%', 'quality', 'high', 'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle', 'play', 'true', 'loop', 'true', 'scale', 'showall', 'wmode', 'opaque',
			'devicefont', 'false', 'bgcolor', '#FFFFFF', 'menu', 'true', 'allowFullScreen', 'true',
			'allowScriptAccess', 'always', 'salign', ''
			); //end AC code
        document.getElementById(container).innerHTML = srcStr;
        //alert(srcStr);
        if (isIE && isWin && !isOpera) {
            this.movie = document.getElementById(flashID);
            //this.movie = window[flashID];
        } else {
            this.movie = document[flashID];
        }
        //		this.movie = document.getElementById(flashID);
    }

    var $A = Array.from = function(iterable) {
        if (!iterable) return [];
        if (iterable.toArray) {
            return iterable.toArray();
        } else {
            var results = [];
            for (var i = 0, length = iterable.length; i < length; i++)
                results.push(iterable[i]);
            return results;
        }
    }
    Function.prototype.bindAsEventListener = function(object) {
        var __method = this, args = $A(arguments), object = args.shift();
        return function(event) {
            return __method.apply(object,
				[(event || window.event)].concat(args).concat($A(arguments)));
        }
    }
    FMapObj.prototype.mousewheel = function(evt) {
        var delta = 0;
        if (evt.wheelDelta) { //for IE
            delta = evt.wheelDelta / 120;
        }
        else if (evt.detail) { //for Mozilla

            delta = -evt.detail / 3;
        }
    }
    FMapObj.prototype.mouseover = function() {
        if (debug) fmap_cnt++;
        if (debug) document.getElementById('consol').innerHTML = this.movie.name + ':' + fmap_cnt;
        var coord = fmap.getCenter();
        if (debug) document.getElementById('centerLon').innerHTML = coord.Lon;
        if (debug) document.getElementById('centerLat').innerHTML = coord.Lat;
        this.movie.focus();
    };


    // 메소드 정의
    FMapObj.prototype.setCenter = function(lon, lat) {
        var coord = new Object();
        coord.Lon = lon;
        coord.Lat = lat;
        //alert("fmaplib: setcenter:  " + lon +"," + lat);
        return this.movie.setCenter(coord);
    };
    FMapObj.prototype.setCenterAndZoom = function(lon, lat, level) {
        var coord = new Object();
        coord.Lon = lon;
        coord.Lat = lat;
        this.movie.setCenterAndZoom(coord, level);
    };
    FMapObj.prototype.getBound = function() {
        var boundary = this.movie.getBound();
        return boundary;
    };
    FMapObj.prototype.getCenter = function() {
        return this.movie.getCenter();
    };
    FMapObj.prototype.setMapLevel = function(level) {
        return this.movie.setMapLevel(level);
    };
    FMapObj.prototype.getMapLevel = function() {
        return this.movie.getMapLevel();
    };
    FMapObj.prototype.zoomIn = function() {
        return this.movie.zoomIn();
    };
    FMapObj.prototype.zoomOut = function() {
        return this.movie.zoomOut();
    };
    FMapObj.prototype.panMove = function(direction) {
        this.movie.panMove(direction);
    };
    FMapObj.prototype.setCenterPanMove = function(lon, lat) {
        var coord = new Object();
        coord.Lon = lon;
        coord.Lat = lat;
        this.movie.setCenterPanMove(coord);
    };
    FMapObj.prototype.getDpSize = function() {
        return this.movie.getDpSize();
    };
    FMapObj.prototype.infoboxShow = function(gid, pid) {
        return this.movie.infoboxShow(gid, pid);
    };
    FMapObj.prototype.infoboxHide = function() {
        return this.movie.infoboxHide();
    };
    FMapObj.prototype.setTopIndex = function(gid, pid) {
        return this.movie.setTopIndex(gid, pid);
    };
    FMapObj.prototype.addPoi = function(pobj) {	//pobj //gid, pid, icon, text, lon, lat, positiontype, imgw, imgh, showtextS, showtextE, infoKind, ID
        //var pobj = makePoiObj(gid, pid, icon, text, lon, lat, positiontype, imgw, imgh, showtextS, showtextE, infoKind, ID);
        return this.movie.addPoi(pobj);
    };
    FMapObj.prototype.stickPoi = function() {
        this.movie.stickPoi();
    };
    FMapObj.prototype.removePoi = function(gid, pid) {
        this.movie.removePoi(gid, pid);
    };
    FMapObj.prototype.removeGroupPoi = function(gid) {
        this.movie.removeGroupPoi(gid);
    };
    FMapObj.prototype.modifyPoi = function(pobj) {
        this.movie.modifyPoi(pobj);
    };

    FMapObj.prototype.externalMouseOverPoi = function(gid, pid) {
        this.movie.externalMouseOverPoi(gid, pid);
    };
    FMapObj.prototype.externalMouseOutPoi = function(gid, pid) {
        this.movie.externalMouseOutPoi(gid, pid);
    };
    FMapObj.prototype.showCancelModifyBtn = function(gid, pid, vis) {
        this.movie.showCancelModifyBtn(gid, pid, vis);
    };


    //PoiSmallMan 관련함수
    FMapObj.prototype.addSmallPoi = function(pobj) {
        //alert("gid : " + pobj.gid + ", pid : " + pobj.pid + ", kind : " + pobj.kind);
        return this.movie.addWholePoi(pobj);
    };
    FMapObj.prototype.removeSmallPoi = function(gid, pid) {
        this.movie.removeWholePoi(gid, pid);
    };
    FMapObj.prototype.removeSmallGroupPoi = function(gid) {
        this.movie.removeWholeGroupPoi(gid);
    };
//    FMapObj.prototype.removeWholeAllPoi = function(arr) {
//        this.movie.removeWholeAllPoi(arr);
//    };
    FMapObj.prototype.showPoiLayer = function(vis) {
        this.movie.showPoiLayer(vis);
    };

    FMapObj.prototype.externalMouseOverSmallPoi = function(gid, pid) {
        this.movie.externalMouseOverSmallPoi(gid, pid);
    };
    FMapObj.prototype.externalMouseOutSmallPoi = function(gid, pid) {
        this.movie.externalMouseOutSmallPoi(gid, pid);
    };
    FMapObj.prototype.setTopIndexSmallPoi = function(gid, pid) {
        this.movie.setTopIndexSmallPoi(gid, pid);
    };
    FMapObj.prototype.setShowPoiText = function(id, visible) {
        this.movie.setShowPoiText(id, visible);
    };
    FMapObj.prototype.showCntInfo = function(visible, showKind) {
        this.movie.showCntInfo(visible, showKind);
    };
    

    //화살표 이미지 관련 poi 등록
    FMapObj.prototype.addArrow = function(pobj) {	//gid, pid, iconPath, text, lon,lat, positiontype, imgw, imgh, showtextS, showtextE) {
        return this.movie.addArrow(pobj);
    };
    FMapObj.prototype.removeArrow = function(gid, pid) {
        this.movie.removeArrow(gid, pid);
    };
    FMapObj.prototype.removeGroupArrow = function(gid) {
        this.movie.removeGroupArrow(gid);
    };
    
    //polyline  함수
    FMapObj.prototype.addPolyline = function(lobj) {
        this.movie.addPolyline(lobj);
    };
    FMapObj.prototype.removePolyline = function(gid, pid) {
        this.movie.removePolyline(gid, pid);
    };

    FMapObj.prototype.removeGroupPolyline = function(gid) {
        this.movie.removeGroupPolyline(gid);
    };

    //ThemeBar함수
    FMapObj.prototype.setOnOff = function(onoff) {
        this.movie.setOnOff(onoff);
    };
    FMapObj.prototype.themeOpen = function() {
        this.movie.themeOpen();
    };
    FMapObj.prototype.themeClose = function() {
        this.movie.themeClose();
    };

    //AroundInfo 함수
    FMapObj.prototype.showAroundInfo = function(lon, lat) {
        this.movie.showAroundInfo(lon, lat);
    };
    FMapObj.prototype.hideAroundInfo = function() {
        this.movie.hideAroundInfo();
    };
    FMapObj.prototype.getDocumentURL = function() {
        var url = document.location.host;
        return url;
    };
    FMapObj.prototype.setCoordsArrange = function(coordList) {
        //alert("fmaplib: setCoordsArrange : " + coordList[0].Lon +", " + coordList[0].Lat);
        this.movie.setCoordsArrange(coordList);
    };

    //TourGuide용
    FMapObj.prototype.tourGuideLoadData = function(id) {
        this.movie.tourGuideLoadData(id);
    };
    FMapObj.prototype.carMovePause = function() {
        this.movie.carMovePause();
    };
    FMapObj.prototype.carMoveReplay = function() {
        this.movie.carMoveReplay();
    };
    FMapObj.prototype.carMoveStop = function() {
        this.movie.carMoveStop();
    };
    FMapObj.prototype.showVideo = function(gid, pid) {
        this.movie.showVideo(gid, pid);
    };
    FMapObj.prototype.tourGuideSetLeftFrame = function(visible) {
        this.movie.tourGuideSetLeftFrame(visible);
    };


	//gps용
    FMapObj.prototype.gpsMyTrackData = function(tid, usersid, kind) {
		this.movie.gpsMyTrackData(tid, usersid, kind);
	};
	FMapObj.prototype.gpsRaceBoardData = function(raceid,usersid) {
		this.movie.gpsRaceBoardData(raceid, usersid);
	};
	FMapObj.prototype.gpsFinishRaceBoardData = function(raceid) {
		this.movie.gpsFinishRaceBoardData(raceid);
	};
	FMapObj.prototype.showFinishRaceBoardTrack = function(tid, usersid) {
		this.movie.showFinishRaceBoardTrack(tid, usersid);
	};
	FMapObj.prototype.selectTrack = function(tid, usersid) {
		this.movie.selectTrack(tid, usersid);
	};
	FMapObj.prototype.unselectTrack = function() {
		this.movie.unselectTrack();
	};
	FMapObj.prototype.removeGPSTrack = function(tid,usersid) {
		this.movie.removeGPSTrack(tid,usersid);
	};
	FMapObj.prototype.removeAllGPSTrack = function() {
		this.movie.removeAllGPSTrack();
	};
	FMapObj.prototype.removePlayerData = function() {
		this.movie.removePlayerData();
	};
	FMapObj.prototype.removeAllPostData = function() {
		this.movie.removeAllPostData();
	};
	FMapObj.prototype.removePostData = function(trackid, usersid) {
		this.movie.removePostData(trackid, usersid);
	};

	FMapObj.prototype.trackPlay = function() {
		this.movie.trackPlay();
	};
	FMapObj.prototype.trackPause = function() {
		this.movie.trackPause();
	};
	FMapObj.prototype.trackStop = function() {
		this.movie.trackStop();
	};
	FMapObj.prototype.posSelectBtnVisible = function(vis) {
		this.movie.posSelectBtnVisible(vis);
	};
	FMapObj.prototype.addSPin = function(pobj) { 
		this.movie.addSPin(pobj);
	};
	FMapObj.prototype.showGPSInfoBoxEx = function(postid, usersid) { 
		this.movie.showGPSInfoBoxEx(postid, usersid);
	};
	FMapObj.prototype.showAllTrack = function() {
		this.movie.showAllTrack();
	};
	FMapObj.prototype.showTrackPoint = function(idx) {
		this.movie.showTrackPoint(idx);
	};
	FMapObj.prototype.hideTrackPoint = function() {
		this.movie.hideTrackPoint();
	};
	FMapObj.prototype.selectPlayerList = function(tid, usersid) {
		this.movie.selectPlayerList(tid, usersid);
	};
	FMapObj.prototype.showLineChart = function(tid, usersid, w, h, viewmode) {
		this.movie.showLineChart(tid, usersid, w, h, viewmode);
	};




    //커서변경
    FMapObj.prototype.showImageCursor = function(obj) {
        this.movie.showImageCursor(obj);
    };
    FMapObj.prototype.hideImageCursor = function() {
        this.movie.hideImageCursor();
    };

    //전체화면
    FMapObj.prototype.setFullScreen = function() { //플래시에서 해야함.
        //alert("setFullScreen - 에러남.. 글구 wmode opaque, transparent하면 전체모드 안됨.. 우씨");
        this.movie.setFullScreen();
    };

    //검색 작게 크게
    FMapObj.prototype.setSearchStatus = function(status) { //max, mini
        this.movie.setSearchStatus(status);
    };
    FMapObj.prototype.goSearch = function(keyword) { //html inputbox값으로 검색할때
        this.movie.goSearch(keyword);
    };
    FMapObj.prototype.setSearchContentBtnTooltip = function(txt) { //검색 content btn tooltip text 세팅
    this.movie.setSearchContentBtnTooltip(txt);
    };
    

    //소켓관련
    FMapObj.prototype.requestBilling = function(IP, PortNo, Version, TransactionID, Sequence, CpCode, SVCCode, RequestorID, PhoneModel, BrowserName, BrowserVersion, RepeartCount) {
        this.movie.requestBilling(IP, PortNo, Version, TransactionID, Sequence, CpCode, SVCCode, RequestorID, PhoneModel, BrowserName, BrowserVersion, RepeartCount);
    };


    /********************************************************/
    // 이벤트 정의  (플래시에서 이 함수를 호출하면 이벤트 발생)
    /********************************************************/
    FMapObj.prototype.onCreate = function() {
        FMapEvent.trigger(this, 'onCreate');
    };
    FMapObj.prototype.onMapMove = function(crd, lvl) {
        var coord = new Object();
        coord.Lon = crd.Lon;
        coord.Lat = crd.Lat;
        FMapEvent.trigger(this, 'onMapMove', coord, lvl );
    };
    FMapObj.prototype.onChangeLevel = function(lvl) {
        FMapEvent.trigger(this, 'onChangeLevel', lvl);
    };

    FMapObj.prototype.onMapClick = function(crd, pxpoint) {
        var coord = new Object();
        coord.Lon = crd.Lon;
        coord.Lat = crd.Lat;
        var point = new Object();
        point.x = pxpoint.x;
        point.y = pxpoint.y;
		FMapEvent.trigger(this, 'onMapClick', coord, point);
    };


    FMapObj.prototype.onMapDblClick = function(crd) {
        var coord = new Object();
        coord.Lon = crd.Lon;
        coord.Lat = crd.Lat;
        FMapEvent.trigger(this, 'onMapClick', coord, point);
    };

    FMapObj.prototype.onMapMouseDown = function() {
        FMapEvent.trigger(this, 'onMapMouseDown');
    };

    

    FMapObj.prototype.onPoiClick = function(pobj) {
        //alert("FMapLib onPoiClick");// + pobj.gid +"," + pobj.pid);
        //alert( "External의 clickPoi" + pobj.gid +"," + pobj.pid + "," + pobj.iconPath +"," + unescape(pobj.text) +","+ pobj.Lon +","+ pobj.Lat +","+ pobj.posType +","+ pobj.imgw +","+ pobj.imgh +","+ pobj.textS +","+ pobj.textE +","+ pobj.infoKind +","+ pobj.ID);
        FMapEvent.trigger(this, 'onPoiClick', pobj);
    };
    FMapObj.prototype.onSmallPoiClick = function(pobj) {
        FMapEvent.trigger(this, 'onSmallPoiClick', pobj);
    };

    FMapObj.prototype.onPoiMouseOver = function(gid, pid) {
        FMapEvent.trigger(this, 'onPoiMouseOver', gid, pid);
    };
    FMapObj.prototype.onPoiMouseOut = function(gid, pid) {
        FMapEvent.trigger(this, 'onPoiMouseOut', gid, pid);
    };

    FMapObj.prototype.onSmallPoiMouseOver = function(gid, pid) {
        FMapEvent.trigger(this, 'onSmallPoiMouseOver', gid, pid);
    };
    FMapObj.prototype.onSmallPoiMouseOut = function(gid, pid) {
        FMapEvent.trigger(this, 'onSmallPoiMouseOut', gid, pid);
    };
    
    FMapObj.prototype.onDetailClick = function(pobj) {
        FMapEvent.trigger(this, 'onDetailClick', pobj);
    };
    FMapObj.prototype.onEndModify = function(gid, pid, coord) {
        FMapEvent.trigger(this, 'onEndModify', gid, pid, coord);
    };
    FMapObj.prototype.onCancelModify = function(gid, pid) {
        FMapEvent.trigger(this, 'onCancelModify', gid, pid);
    };
    FMapObj.prototype.onRMapClick = function() {
        FMapEvent.trigger(this, 'onRMapClick');
    };
    FMapObj.prototype.onClickThemeBar = function(obj) {
        FMapEvent.trigger(this, 'onClickThemeBar', obj);
    };
    FMapObj.prototype.onClosedThemeBar = function() {
        FMapEvent.trigger(this, 'onClosedThemeBar');
    };
    FMapObj.prototype.onOpenedThemeBar = function() {
        FMapEvent.trigger(this, 'onOpenedThemeBar');
    };
    FMapObj.prototype.onCloseVideoPlayer = function() {
        FMapEvent.trigger(this, 'onCloseVideoPlayer');
    };

	
	//인포박스 관련
	FMapObj.prototype.onClickInfoDetailView = function(infoKind, ID) {
		FMapEvent.trigger(this, 'onClickInfoDetailView', infoKind, ID);
	};


    //검색관련
    FMapObj.prototype.onStatusSearch = function(state) {
        FMapEvent.trigger(this, 'onStatusSearch', state);
    };
    FMapObj.prototype.onFullScreen = function(state) {
        FMapEvent.trigger(this, 'onFullScreen', state);
    };
    FMapObj.prototype.onClickSearchBtn = function(obj) {
        FMapEvent.trigger(this, 'onClickSearchBtn', obj);
    };
    FMapObj.prototype.onClickSearchContent = function(obj) {
        //alert("FMapLib dddd");       
        FMapEvent.trigger(this, 'onClickSearchContent', obj);
    };
    FMapObj.prototype.onKeydownSearchInput = function() {
        FMapEvent.trigger(this, 'onKeydownSearchInput');
    };
    FMapObj.prototype.onClickExSearchBtn = function() {
        FMapEvent.trigger(this, 'onClickExSearchBtn');
    };
    FMapObj.prototype.onClickSearchHideBtn = function() {
        FMapEvent.trigger(this, 'onClickSearchHideBtn');
    };
    FMapObj.prototype.onCompleteSearch = function() {
        FMapEvent.trigger(this, 'onCompleteSearch');
    };
    

	//gps 관련 이벤트
	FMapObj.prototype.onSelectGPSPoint = function(gid, pid, crd, kind) {
        FMapEvent.trigger(this, 'onSelectGPSPoint', gid, pid, crd, kind);
    };
	FMapObj.prototype.onGPSCancelSelect = function(gid,pid, kind) {
        FMapEvent.trigger(this, 'onGPSCancelSelect', gid,pid, kind);
    };
	FMapObj.prototype.onGPSPostSave = function(obj) {
        FMapEvent.trigger(this, 'onGPSPostSave', obj);
    };

	FMapObj.prototype.onEndLoadGPSTrack = function(usersid, trackid) { //마이트랙일때 트랙로딩완료 이벤트 //난중에 지우기
        FMapEvent.trigger(this, 'onEndLoadGPSTrack', usersid, trackid);
    };

	FMapObj.prototype.onEndLoadGPSMyTrack = function(trackid, usersid) { //마이트랙일때 트랙로딩완료 이벤트
        FMapEvent.trigger(this, 'onEndLoadGPSMyTrack', trackid, usersid);
    };
	FMapObj.prototype.onEndLoadGPSIngRaceBoardTrack = function(raceid, usersid) { //진행중 레이스일때 트랙로딩완료 이벤트
        FMapEvent.trigger(this, 'onEndLoadGPSIngRaceBoardTrack', raceid, usersid);
    };
	FMapObj.prototype.onEndLoadGPSFinishRaceBoardTrack = function(raceid) { //완료된 레이스일때 트랙로딩완료 이벤트
        FMapEvent.trigger(this, 'onEndLoadGPSFinishRaceBoardTrack', raceid);
    };
	FMapObj.prototype.onClickTrack = function(tid, usersid) { //완료된 레이스일때 트랙 클릭하면 전달되는 이벤트 -> 궤적보기 버튼 클릭했을때와 같은 행위 하면 됨
        FMapEvent.trigger(this, 'onClickTrack', tid, usersid);
    };

	//gps용 그래프
	FMapObj.prototype.onDrawLineChart = function(dt, w, h, kind, viewmode) { 
        FMapEvent.trigger(this, 'onDrawLineChart', dt, w, h, kind, viewmode);
    };

	//gps용 playerlist
	FMapObj.prototype.onClickPlayerListData = function(tid, usersid) { 
        FMapEvent.trigger(this, 'onClickPlayerListData', tid, usersid);
    };
	FMapObj.prototype.onClickPlayerListAllBtn = function() { 
        FMapEvent.trigger(this, 'onClickPlayerListAllBtn');
    };;


    //소켓관련
    FMapObj.prototype.onResponseData = function(obj) {
        FMapEvent.trigger(this, 'onResponseData', obj);
    };

    window.FMap = FMapObj;

    /********************************************************/
    /* 이벤트 객체 선언 // FMapEvent //*/
    /********************************************************/
    var EventMan = {};
    EventMan.addListener = function(element, eventName, handler) {
        var wEvent = EventMan.getWEventName(eventName);
        if (element[wEvent]) element[wEvent].push(handler);
        else element[wEvent] = new Array(handler);
    };
    EventMan.removeListener = function(element, eventName, handler) {
        var evt = EventMan.getWEventName(eventName);
        var arrEvent = element[evt];
        if (arrEvent && arrEvent.length > 0) {
            var flag = false;
            for (var i = 0; i < arrEvent.length; i++) {
                if (arrEvent[i] == handler) {
                    flag = true;
                }
                if (flag && i != (arrEvent.length - 1)) {
                    arrEvent[i] = arrEvent[i + 1];
                }
            }
            if (flag) {
                arrEvent.pop();
            }
        }
    };
    EventMan.trigger = function(element, eventName) {
        var evt = EventMan.getWEventName(eventName);
        var arrEvent = element[evt];
        if (arrEvent && arrEvent.length > 0) {
            var args = new Array();
            for (var i = 2; i < arguments.length; i++) {
                args.push(arguments[i]);
            }
            args.push(element);
            for (var i = 0; i < arrEvent.length; i++) {
                var evtCallBack = arrEvent[i];
                if (evtCallBack) {
                    try {
                        evtCallBack.apply(element, args)
                    }
                    catch (e) {
                    }
                }
            }
        }
    };
    EventMan.getWEventName = function(eventName) {
        return "_fmapEvent__" + eventName;
    };
    /* 이벤트 관리 객체 정의 끝 */

    window.FMapEvent = EventMan;
};
_FMapLib();	
