﻿var JejuMap = { //JejuMap.LoadData
            IconPath : null,
            OrgX : 126.138495, //UTM:232968.13238043024
            OrgY : 33.107000,  //UTM:3666793.082902641
            MapWidth : 0.842155, //126.98065
            MapHeight : 0.464450, //33.57145
            ImgWidth : function(){return top.window.frames['mapall'].frames['map'].$('imgjejumap').clientWidth;},
            ImgHeight : function(){return top.window.frames['mapall'].frames['map'].$('imgjejumap').clientHeight;},
            RatioX : 0.1, 
            RatioY : 0.1, 
            ImgOrgY : 30,
            DataLodge : null,
            DataTicket : null,
            DataTourInfo  : null,
            DataBasicPosition : null,
            
            Initialize : function(imgW, imgH, _iconPath)
            {//try{
                JejuMap.IconPath = _iconPath;
                divtop = $('divtopctls');
                img = $('imgjejumap');
                
                JejuMap.SetImgFile(imgW, img); //크기에 따라 이미지 파일 지정
                divmap = $('divjejumap');
                
                divmap.style.top = JejuMap.ImgOrgY = 35;
                img.style.width = imgW;
                img.style.height = imgH;
                
                JejuMap.RatioX = JejuMap.MapWidth/imgW;
                JejuMap.RatioY = JejuMap.MapHeight/imgH;
                
                JejuMap.LoadData('주요위치');//공항/중문등.. 중요한 위치 표시하기..
                var def = null;
                var qstr = window.top.location.search;
                var naviFrame = top.window.frames['mapall'].frames['navi'];
                if(GUtil.getQueryString('ticket', qstr.substring(1))=='yes') //할인입장권
                {
                    naviFrame.$('chkTicket').checked = true;                    
                    def = 1;
                }
                if(GUtil.getQueryString('lodge', qstr.substring(1))=='yes') //숙소
                {
                    naviFrame.$('chkLodge').checked = true;
                    def = 1;
                }
                if(GUtil.getQueryString('tourinfo', qstr.substring(1))=='yes') //관광정보
                {
                    naviFrame.$('chkTourInfo').checked = true;
                    def = 1;
                }
                if(def == null)//기본=>숙소
                {
                    naviFrame.$('chkLodge').checked = true;
                }
                    
                if(top.window.frames['mapall'].frames['navi'].$('chkTourInfo').checked) JejuMap.LoadData('관광정보', top.window.frames['mapall'].frames['navi'].$('chkTourInfo'));
                if(top.window.frames['mapall'].frames['navi'].$('chkLodge').checked) JejuMap.LoadData('숙소', top.window.frames['mapall'].frames['navi'].$('chkLodge'));
                if(top.window.frames['mapall'].frames['navi'].$('chkTicket').checked) JejuMap.LoadData('할인입장권', top.window.frames['mapall'].frames['navi'].$('chkTicket'));
            //}catch(exx){}},
            },
            
            SetImgFile: function(swidth, imgCtl) //이미지 크기에 따라 파일을 지정한다.
            {
                var mapimg = null;
                if(swidth<1260) mapimg = 'map/960.gif';
                else if(swidth<2200) mapimg = 'map/1920.gif';
                else mapimg = 'map/3840.gif';
                
                if(img.src.indexOf(mapimg)<0) imgCtl.src = mapimg;
            },
            
            //지도 확대 또는 축소..
            ResizeMap : function(isMakeBig)
            {//try{
                img = top.window.frames['mapall'].frames['map'].$('imgjejumap');
                
                var W = GUtil.parseNumber(img.style.width.replace('px','')), H = GUtil.parseNumber(img.style.height.replace('px',''));
                if(W<1000 && !isMakeBig) 
                {
                    alert('최소 크기 입니다.');
                    return;
                }
                else if(W>4000 && isMakeBig) 
                {
                    alert('최대 크기 입니다.');
                    return;
                }
                
                if(isMakeBig){ W = W*2; H = H*2;}
                else { W = W/2; H = H/2;}
                
                JejuMap.SetImgFile(W, img);//크기에 따라 이미지 파일 지정
                
                img.style.width = W;
                img.style.height = H;
                JejuMap.RatioX = JejuMap.MapWidth/W;
                JejuMap.RatioY = JejuMap.MapHeight/H;
                
                //지도상의 모든 아이템의 좌표를 수정한다.
                JejuMap.ReArrangeIconPosition(null, null); 
                
                try
                {
                    top.window.frames['mapall'].frames['map'].RevFormRoom.HideForm(null);
    	            top.window.frames['mapall'].frames['map'].RevFormTicket.HideForm(null);
    	            //top.window.frames['mapall'].frames['map'].TourInfoForm.HideForm(null);
                }
                catch(e){}
                
                    //네비 위치 조정.. 아래것 또는 이것 둘중 하나면 되는데..
                    //top.window.frames['mapall'].frames['map'].JejuMapDrag.OnScrolled(null);
                    
                    //네비 위치에 따라 현재 화면을 스크롤한다. - 화면 크기가 변해도 같은 곳을 보여주기 위해서
                    var xy = top.window.frames['mapall'].frames['navi'].MapNaviImageFrame.GetNaviPosition();
                    top.window.frames['mapall'].frames['map'].OnEventFromNavi.ScrollTo(xy[0], xy[1]);                
            //}catch(exx){}},
            },
            
            //지도 크기 변할때, 아이콘의 위치를 다시 배치한다.
            //cssName이 없을때: 모든 아이템
            //display가 null 일때는 display 속성 수정안하기.
            ReArrangeIconPosition : function(cssName, display)
            {
                if(cssName == null) cssName = 'id_mapitem';
                
                var divjejumap = top.window.frames['mapall'].frames['map'].$('divjejumap');
                var os = GUtil.ElemSByClass(cssName, divjejumap , 'div' );
                for(osk=0; osk<os.length; osk++)
                {
                    var d = os[osk].getAttribute('mapdata').split('|');
                    var xy = JejuMap.ConvertXYtoPoint(GUtil.parseNumber(d[1]), GUtil.parseNumber(d[2]));
                    os[osk].style.left = xy[0];
                    os[osk].style.top = xy[1];
                    if(display != null) os[osk].style.display = display;
                }
            },
            
            ConvertXYtoPoint : function(x, y)
            {//try{
                var diffX = x - JejuMap.OrgX;
                var diffY = y - JejuMap.OrgY;
                
                diffX = GUtil.ceilNumber(diffX/JejuMap.RatioX);
                diffY = GUtil.ceilNumber(diffY/JejuMap.RatioY);
                diffY = JejuMap.ImgHeight() - diffY; // 위도는 위로 갈수록 증가, 이미지는 아래로 갈수록 증가 하므로
                return new Array(diffX, diffY);
            //}catch(exx){}},
            },

            //지도에 아이콘 표시하기.
            AddPositionMark : function(x, y)
            {//try{
                if(JejuMap.OrgX > x || (JejuMap.OrgX+JejuMap.MapWidth)<x || JejuMap.OrgY > y || (JejuMap.OrgY+JejuMap.MapHeight)<y) return null;
                var xy = JejuMap.ConvertXYtoPoint(x, y);
                            
                var div = document.createElement('div');
                div.style.position = 'absolute';
                div.style.cursor = 'pointer';                
                div.style.border = 0;
                div.style.left = xy[0];
                div.style.top = xy[1];
                
                div.style.backgroundColor = '#FFFFFF';
                //div.style.opacity = 0.9;
        	    //div.style.filter = 'alpha(opacity=' + 90 + ')';      	
        	    div.onclick = JejuMap.OnClickIcon;   //아이콘 클릭시
        	    div.onmouseover = JejuMap.OnMouseOverIcon; //아이콘 마우스 오버 이벤트
        	    div.onmouseout = JejuMap.OnMouseOutIcon; //아이콘 마우스 아웃 이벤트
        	    
        	    var divjejumap = $('divjejumap');
                divjejumap.appendChild(div);
                
                return div;
            //}catch(exx){}},
            },
            
            OnCheckChange : function(src, ctype)
            {//try{
                top.window.frames['mapall'].frames['map'].RevFormRoom.HideForm(null);
    	        top.window.frames['mapall'].frames['map'].RevFormTicket.HideForm(null);
    	        //top.window.frames['mapall'].frames['map'].TourInfoForm.HideForm(null);
    	        
                if(src.checked)
                    top.window.frames['mapall'].frames['map'].JejuMap.LoadData(ctype, src);
                else
                    top.window.frames['mapall'].frames['map'].JejuMap.HideData(ctype, src);
            //}catch(exx){}},
            },
            
            //데어터 로드하기.
            LoadData : function(ctype, chksrc)
            {//try{
                if(ctype == '할인입장권') ctype = '상품/할인입장권';
                
                var ds = null;
                if(ctype.indexOf('숙소')>=0) ds = JejuMap.DataLodge;
                else if(ctype.indexOf('할인입장권')>=0 ) ds = JejuMap.DataTicket;
                else if(ctype.indexOf('관광정보')>=0 ) ds = JejuMap.DataTourInfo;
                else if(ctype.indexOf('주요위치')>=0 ) ds = JejuMap.DataBasicPosition;

                if(ds == null)
                {
                    try{
                        JejuMap.DataTypeTmp = ctype;
                        
                        ob_post.AddParam('ctype', ctype);                        
                        ob_post.post('/CS/map/WS/PositionInfo.aspx', 'PositionsOfType', JejuMap.OnDataReadFromServer);
                    }catch(e)
                    {
                        if(chksrc!= null && typeof chksrc != 'undefined')
                            chksrc.checked = false;
                        return;
                    }
                }
                else //이미 있기 때문에 좌표위치계산하고, display 속성만 변경한다
                {
                    var oClassNames = null;
                    if(ctype.indexOf('숙소')>=0) oClassNames = 'id_lodge_map';
                    else if(ctype.indexOf('할인입장권')>=0 ) oClassNames = 'id_ticket_map';
                    else if(ctype.indexOf('관광정보')>=0 ) oClassNames = 'id_tourinfo_map';
                    
                    //지도상의 아이템의 좌표를 수정한다.
                    JejuMap.ReArrangeIconPosition(oClassNames, 'block');
                }
            //}catch(exx){}},
            },
            
            OnDataReadFromServer : function(result)
            {
                var ds = result;
                var ctype = JejuMap.DataTypeTmp;
                if(ctype.indexOf('숙소')>=0) JejuMap.DataLodge = ds;
                else if(ctype.indexOf('할인입장권')>=0 ) JejuMap.DataTicket = ds;
                else if(ctype.indexOf('관광정보')>=0 ) JejuMap.DataTourInfo = ds;
                else if(ctype.indexOf('주요위치')>=0 ) JejuMap.DataBasicPosition = ds;
                
                ds = ds.split('#');
                for(dk = 0; dk<ds.length; dk++)
                    JejuMap.ConvertDataToMark(ds[dk], ctype);
            },
            
            HideData : function(ctype)
            {//try{
                var oClassNames;
                var divjejumap = top.window.frames['mapall'].frames['map'].$('divjejumap');
                
                if(ctype.indexOf('숙소')>=0) oClassNames = 'id_lodge_map';
                else if(ctype.indexOf('할인입장권')>=0 ) oClassNames = 'id_ticket_map';
                else if(ctype.indexOf('관광정보')>=0 ) oClassNames = 'id_tourinfo_map';
                
                var os = GUtil.ElemSByClass(oClassNames, divjejumap , 'div' );
                for(osk=0; osk<os.length; osk++)
                    os[osk].style.display = 'none';
            //}catch(exx){}},
            },
            
            //DB의 데이터를 아이콘으로 변환하기.
            //d :=> CID, X,Y, 제목,분류, 숙소.등급 as 타입별
            ConvertDataToMark : function(dstr, ctype)
            {//try{
                var d = dstr.split('|');
                var divctl = JejuMap.AddPositionMark(GUtil.parseNumber(d[1]), GUtil.parseNumber(d[2]), d[3]);
                if(divctl==null) return;            
                
                divctl.setAttribute('mapdata', dstr);
                
                s = d[3]; //제목
                var imgctl = document.createElement('img');                
                imgctl.setAttribute('alt', s);
                divctl.setAttribute('title', s);
                
                if(s.length>4) s = s.substring(0,4); //제목 4글자만 표시
                
                if(d[4].indexOf('숙소')>=0)
                {
                    if(d[5].indexOf('호텔')>=0)
                        imgctl.setAttribute('src',JejuMap.IconPath + 'icon_hotel.gif');
                    else if(d[5].indexOf('펜션')>=0)
                        imgctl.setAttribute('src',JejuMap.IconPath + 'icon_pension.gif');
                    else if(d[5].indexOf('콘도')>=0 || d[5].indexOf('리조트')>=0)
                        imgctl.setAttribute('src',JejuMap.IconPath + 'icon_resort.gif');
                    else
                        imgctl.setAttribute('src',JejuMap.IconPath + 'icon_etc.gif');
                        
                    divctl.className = 'id_mapitem id_lodge_map';
                }
                else if(d[4].indexOf('할인입장권')>=0)
                {
                    imgctl.setAttribute('src',JejuMap.IconPath + 'icon_coupon.gif');
                    divctl.className = 'id_mapitem id_ticket_map';                
                }
                else if(ctype == '주요위치')
                {
                    divctl.className = 'id_mapitem';
                }
                else //if(ctype == '관광정보') //생략
                {
                    imgctl.setAttribute('src',JejuMap.IconPath + 'icon_tourinfo.gif');
                    divctl.className = 'id_mapitem id_tourinfo_map';
                }
                imgctl.setAttribute('width', 12);
                imgctl.setAttribute('height', 12);                
                
                if(ctype != '주요위치')
                    divctl.appendChild(imgctl);
                    
                var spanctl = document.createElement('span');              
                
                spanctl.innerHTML = s;
                divctl.appendChild(spanctl);
            //}catch(exx){}},
            },
            
            //상세정보 바로 보여주기..
            OnClickIcon2 : function(evt)
            {
                var e=evt?evt:window.event?window.event:null;
	            var srcElem = e.target ? e.target : (e.srcElement ? e.srcElement : null);
	            evt = e; src = srcElem;
	            if(srcElem==null || srcElem.getAttribute ==null || typeof srcElem.getAttribute == 'undefined') return;
    	        
	            if (srcElem && srcElem.tagName.toLowerCase() == 'img') //img태그에서 이벤트 발생시키는 경우 div 찾기.
	            {
	                if (srcElem.parentNode) srcElem = srcElem.parentNode;
	                else if (srcElem.parentElement) srcElem = srcElem.parentElement;
	            }
	            
	            var d = srcElem.getAttribute('mapdata');
                if(d==null) return;
                d = d.split('|');              
	            
                var url = null;
                var frm = $('iframeDetail');                                                
                
                if(d[4].indexOf('숙소')>=0) url = '/CS/DetailPopup/Lodge.aspx?cid=' + d[0];
                else if(d[4].indexOf('할인입장권')>=0) url = '/CS/DetailPopup/Ticket.aspx?cid=' + d[0];
                else url = '/CS/DetailPopup/TourInfo.aspx?cid=' + d[0];
                
                MapItemDetail.ShowItemDetail(url);
            },            

            //아이콘에 대한 간략 정보 및 예약폼 표시하기.
            OnClickIcon : function(evt)
            {//try{
                var e=evt?evt:window.event?window.event:null;
	            var srcElem = e.target ? e.target : (e.srcElement ? e.srcElement : null);
	            evt = e; src = srcElem;
    	        
	            if(srcElem==null || srcElem.getAttribute ==null || typeof srcElem.getAttribute == 'undefined') return;
    	        
	            if (srcElem && srcElem.tagName.toLowerCase() == 'img') //img태그에서 이벤트 발생시키는 경우 div 찾기.
	            {
	                if (srcElem.parentNode) srcElem = srcElem.parentNode;
	                else if (srcElem.parentElement) srcElem = srcElem.parentElement;
	            }
	            if(srcElem.tagName.toLowerCase() != 'div')
	                srcElem = GUtil.FindOuterElementByTagName(srcElem, 'div');
    	        
    	        RevFormRoom.HideForm(evt);
    	        RevFormTicket.HideForm(evt);
    	        MapItemDetail.CloseDetailView();
    	        
                var d = srcElem.getAttribute('mapdata');
                MapItemInfo.ShowForm(evt, src, d);
            //}catch(exx){}}
            },
            
            OnMouseOverIcon : function(evt)
            {
                var e=evt?evt:window.event?window.event:null;
	            var src = e.target ? e.target : (e.srcElement ? e.srcElement : null);
	            
	            if(src.tagName.toLowerCase() != 'div')
	                src = GUtil.FindOuterElementByTagName(src, 'div');
	            src.style.zIndex = 10;
	            src.style.backgroundColor = '#77A700';
                src.style.color = '#FFFFFF';
                
	            var d = src.getAttribute('mapdata');
                if(d==null) return;
                d = d.split('|');               
                
                var span = src.getElementsByTagName('span')[0];                
                span.innerHTML = d[3];
                span.style.fontWeight = 'bold';                
            },
            
        	OnMouseOutIcon : function(evt)
            {
                var e=evt?evt:window.event?window.event:null;
	            var src = e.target ? e.target : (e.srcElement ? e.srcElement : null);	            
	            
	            if(src.tagName.toLowerCase() != 'div')
	                src = GUtil.FindOuterElementByTagName(src, 'div');
	            src.style.zIndex = 0;
	            src.style.backgroundColor = '#FFFFFF';
                src.style.color = '#000000';

	            var d = src.getAttribute('mapdata');
                if(d==null) return;
                d = d.split('|');
                
                s = d[3];
                if(s.length>4) s = s.substring(0,4); //제목 4글자만 표시
                var span = src.getElementsByTagName('span')[0];
                span.innerHTML = s;
                span.style.fontWeight = 'normal';                
            }
        }
