"use-strict";

/*소통 정보*/
var _FwdLineArr = [];
var _BwdLineArr = [];
var _AtrdSideMarkerArr = [];
var sideInfoList = true;
var _AtrdTrafficInfoArr = [];
let _prevLevel = 0;

function atrdTrfInfo(atrdId, atrdNm) {

	$('#selectRoad').text(atrdNm+' 지도 소통정보');
	$('#selectRoadFont').text(atrdNm+' 문자 소통정보');
	
    if (atrdId == '') {	
    	return;
    }
    _AtrdId = atrdId.split(',');

    //소통통계 메뉴..
    if ($('#atrdListSub').length > 0) {
        $('#atrdListSub')
            .val(atrdId)
            .prop('selected', true);
        $('.resultOfStats').empty();
    }

    showAtrdTrfInfo();

    var roadCnt = 0;
    var atrdFwdSped = 0;
    var atrdFwdLngt = 0;
    var atrdBwdSped = 0;
    var atrdBwdLngt = 0;

    //지도를 재설정할 범위정보를 가지고 있을 LatLngBounds 객체를 생성합니다
    var bounds = new daum.maps.LatLngBounds();

    if (_FwdLineArr.length > 0) {
	    _FwdLineArr.forEach(function (el) {
	        if (el.sped > 0) {
	            roadCnt++;
	            atrdFwdSped += el.sped;
	        } else {}
	        atrdFwdLngt += el.sect_lngt;

	        bounds.extend(new daum.maps.LatLng(el.y_crdn_min, el.x_crdn_min));
	        bounds.extend(new daum.maps.LatLng(el.y_crdn_max, el.x_crdn_max));
	    });
    }

    if (_BwdLineArr.length > 0) {
	    _BwdLineArr.forEach(function (el) {
	        if (el.sped > 0) {
	            atrdBwdSped += el.sped;
	
	        }
	        atrdBwdLngt += el.sect_lngt;
	
	    });
    }
    //간선도로를 지도 가운데로 위치
    if ($('.mapRoadList').css('display') == 'none') {
        _Map.setBounds(bounds);
    	if (_prevLevel == _Level) {
    		_Map.setLevel(_Map.getLevel()+1);
    	}
    	_prevLevel = _Level;
    }

    if ($('#trafficInfo').length > 0) { //소통정보메뉴 문자데이터..
    	atrdFwdSped = isNumberText((atrdFwdSped / roadCnt).toFixed(2), ' km/h');
    	atrdBwdSped = isNumberText((atrdBwdSped / roadCnt).toFixed(2), ' km/h');
    	atrdFwdLngt = isNumberText((atrdFwdLngt / 1000).toFixed(2), ' km');
    	atrdBwdLngt = isNumberText((atrdBwdLngt / 1000).toFixed(2), ' km');

        var strNew =  ' <div class="atrd-name">'+ atrdNm +'</div>' +
                    '   <div class="upload">'+
                    '        <span>평균속도 : '+atrdFwdSped+'</span>'+
                    '        <span>거리 : '+atrdFwdLngt+'</span>'+
                    '    </div>'+
                    '    <div class="dnload">'+
                    '        <span>평균속도 : '+atrdBwdSped+'</span>'+
                    '        <span>거리 : '+atrdBwdLngt+'</span>'+
                    '    </div>'

        $(".searchBox-cont").html(strNew);
    }
    //도로목록에서 도로선택시 문자탭으로 이동..
    $('#trafficInfo dl:nth-of-type(1) dt:nth-of-type(1) a').focus();

    //showAtrdTrfInfo();
}

function isNumberText(number, text){
	let format = '';
	if (isNaN(number)) {
		format = '-' + text;
	}
	else {
		format = number + text;
	}
	return format;
}

function hideAtrdTrfInfo() {
    var Level = _Level;
    
    if (_Level <= 3) {
        Level = 0;
    }

    if (_TrafficInfoArr[Level] && _TrafficInfoArr[Level].length > 0) {
	    _TrafficInfoArr[Level].forEach(function (el) {
	        el
	            .polylineBack
	            .setOptions({strokeColor: '#fff'});
	
	    });
    }
    
    if (_AtrdSideMarkerArr && _AtrdSideMarkerArr.length > 0) {
	    _AtrdSideMarkerArr.forEach(function (el) {
	        el.setMap(null);
	    });
    }

    _FwdLineArr.length = 0;
    _BwdLineArr.length = 0;
    _AtrdSideMarkerArr.length = 0;
}

function showAtrdTrfInfo() {
    //이전 간선도로 선택 색상 초기화..
    if (_FwdLineArr.length > 0) {
    	hideAtrdTrfInfo();
    }
    
    var lettersStr = '';
    var lettersStrNew = '';
    var Level = _Level;

    if (_Level <= 3) {
        Level = 0;
    }

    if (_TrafficInfoArr[Level] && _TrafficInfoArr[Level].length > 0) {
	    _TrafficInfoArr[Level].forEach(function (el) {
	        if (_AtrdId[0] == el.atrd_id) { //시작 라인
	            el
	                .polylineBack
	                .setOptions({
	                    strokeColor: '#000000', //black
	                    zIndex: 3
	                });
	            el
	                .polyline
	                .setOptions({zIndex: 4});
	
	            if ($('#mapOfLetters').length > 0) { //소통정보메뉴 문자데이터..
	                lettersStr += '<tr><td><a href="javascript:void(0)" title="방향:' + el.drct_cd + ',도로:' + el.atrd_nm +
	                        ',속도:' + el.sped + 'km/h"><' + el.drct_cd + '>&nbsp;[' + el.atrd_nm + ']&nbsp;&' +
	                        'nbsp;&nbsp;&nbsp;&nbsp;<strong>' + el.sped + ' km/h </strong></a></td><td><a h' +
	                        'ref="javascript:void(0)" title="도로범위:' + el.strt_nm_node + '부터' + el.end_nm_node +
	                        '까지"><span>' + el.strt_nm_node + ' </span><span> ' + el.end_nm_node + '</span><' +
	                        '/a></td><td><a href="javascript:void(0)" title="소통정보:' + el.cmtrGradText + '">' +
	                        '<strong class="traffic ' + el.cmtr_grad_cd + '">' + el.cmtrGradText + '</stron' +
	                        'g></a></td></tr>';


                    lettersStrNew += '<li>'+
                        '    <div class="road_info">'+
                        '        <div class="atrd-top"><div>['+ el.drct_cd +']' + el.atrd_nm +'</div><strong>' + el.sped + ' km/h</strong></div>'+
                        '        <div class="atrd-bottom">' +
                        '           <div class="atrd-front">'+ el.strt_nm_node +'</div>' +
                        '           <span class="arrow-icon"><img src="../common/images/arrow_icon.png" alt=""></span>' +
                        '           <div class="atrd-end">'+ el.end_nm_node +'</div>' +
                        '           <strong>' + el.sped + ' km/h</strong></div>'+
                        '    </div>'+
                        '    <aside class="road-traffic">'+
                        '        <span class="traffic'+el.cmtrGradText+'">'+el.cmtrGradText+'</span>'+
                        '    </aside>'+
                        '</li>'
	            }
	            _FwdLineArr.push(el);
	        } else if (_AtrdId[1] == el.atrd_id) { // 끝라인
	            el
	                .polylineBack
	                .setOptions({
	                    //strokeColor : '#FF0000' red
	                    strokeColor: '#000000', //black
	                    zIndex: 3
	                });
	
	            el
	                .polyline
	                .setOptions({zIndex: 4});
	            if ($('#mapOfLetters').length > 0) { //소통정보메뉴 문자데이터..
	                lettersStr += '<tr><td><a href="javascript:void(0)" title="방향:' + el.drct_cd + ',도로:' + el.atrd_nm +
	                        ',속도:' + el.sped + 'km/h"><' + el.drct_cd + '>&nbsp;[' + el.atrd_nm + ']&nbsp;&' +
	                        'nbsp;&nbsp;&nbsp;&nbsp;<strong>' + el.sped + ' km/h </strong></a></td><td><a h' +
	                        'ref="javascript:void(0)" title="도로범위:' + el.strt_nm_node + '부터' + el.end_nm_node +
	                        '까지"><span>' + el.strt_nm_node + ' </span><span> ' + el.end_nm_node + '</span><' +
	                        '/a></td><td><a href="javascript:void(0)" title="소통정보:' + el.cmtrGradText + '">' +
	                        '<strong class="traffic ' + el.cmtr_grad_cd + '">' + el.cmtrGradText + '</stron' +
	                        'g></a></td></tr>';

                    lettersStrNew += '<li>'+
                        '    <div class="road_info">'+
                        '        <div class="atrd-top"><div>['+ el.drct_cd +']' + el.atrd_nm +'</div>' +
                        '        <strong>' + el.sped + ' km/h</strong></div>'+
                        '        <div class="atrd-bottom">' +
                        '           <div class="atrd-front">'+ el.strt_nm_node +'</div>' +
                        '           <span class="arrow-icon"><img src="../common/images/arrow_icon.png" alt=""></span>' +
                        '           <div class="atrd-end">'+ el.end_nm_node +'</div>' +
                        '           <strong>' + el.sped + ' km/h</strong>' +
                        '       </div>'+
                        '    </div>'+
                        '    <aside class="road-traffic">'+
                        '        <span class="traffic'+el.cmtrGradText+'">'+el.cmtrGradText+'</span>'+
                        '    </aside>'+
                        '</li>'
	            }
	            _BwdLineArr.push(el);
	        }
	    });
    }

    //소통정보메뉴 문자데이터..
    if ($('#mapOfLetters').length > 0) {
        if (_RoadListStr != '' && _RoadListStr.length > 0) {
            _RoadListStr = '';
            $('#mapOfLetters').empty();
        }
        //_RoadListStr = lettersStr;
        _RoadListStr = lettersStrNew;
        if ($('.mapRoadList').css('display') == 'block') {
            $('#mapOfLetters')
                .empty()
                .html(_RoadListStr);
        }
    }

    if (_FwdLineArr && _FwdLineArr.length > 0) {
	    //상,하행 마커 표시
	    drawAtrdSideMarker(
	        'S',
	        _FwdLineArr[0].polyline.getPath()[0].getLat(),
	        _FwdLineArr[0].polyline.getPath()[0].getLng()
	    );
    }
    if (_BwdLineArr && _BwdLineArr.length > 0) {
	    drawAtrdSideMarker(
	        'E',
	        _BwdLineArr[0].polyline.getPath()[0].getLat(),
	        _BwdLineArr[0].polyline.getPath()[0].getLng()
	    );
    }

}

function drawAtrdSideMarker(src, lat, lng) {
    var imageSize;
    var imageSrc = '/images/traffic/map-marker' + src + '.png'; // 마커이미지의 주소입니다
    if (_Level < 7) {
        imageSize = new daum
            .maps
            .Size(48, 48);
        imageOption = {
            offset: new daum
                .maps
                .Point(24, 48)
        };
    } else {
        imageSize = new daum
            .maps
            .Size(24, 24);
        imageOption = {
            offset: new daum
                .maps
                .Point(12, 24)
        };
    }

    // 마커의 이미지정보를 가지고 있는 마커이미지를 생성합니다
    var markerImage = new daum
            .maps
            .MarkerImage(imageSrc, imageSize, imageOption),
        markerPosition = new daum
            .maps
            .LatLng(lat, lng); // 마커가 표시될 위치입니다

    // 마커를 생성합니다
    var atrdSideMarker = new daum
        .maps
        .Marker({map: _Map, position: markerPosition, image: markerImage, zIndex: 5});
    _AtrdSideMarkerArr.push(atrdSideMarker);
}

function typeOfMap() {
    if (_IncdInfoWindow != null) 
        _IncdInfoWindow.setMap(null);
    $('#mapOfLetters').empty();
    $('.mapRoadList').hide();
    $('.mapContent.traffic.map').show();
    $('.mylocation').show();
    $('.legend-img').show();
}

function typeOfLetters() {
    $('.mapContent.traffic.map').hide();
    $('#mapOfLetters').html(_RoadListStr);
    $('.mapRoadList').show();
    $('.mylocation').hide();
    $('.legend-img').hide();
}

function sideListShow(content) {
    if (sideInfoList == true) {
        $('.hideShowButton').attr('title', content+'목록보기');
        $('.hideShowButton').css(
            'background',
            'url("/images/btn_showhide.png") 0 -30px no-repeat'
        );
        $('.sideInfoList').hide();
        sideInfoList = false;
    } else {
        $('.hideShowButton').attr('title', content+'목록숨김');
        $('.hideShowButton').css(
            'background',
            'url("/images/btn_showhide.png") 0 0 no-repeat'
        );
        $('.sideInfoList').show();
        sideInfoList = true;
    }
}

/*CCTV 정보*/

function showCctvInfo(id,nm) {
	$('#selectCctv').text(nm+' 지도 CCTV 정보');
    _CctvCtlrArr.some(function (el) {
        //console.log(id + " " + el.cctv_mngm_nmbr);
        if (el.cctv_mngm_nmbr == id) {
            _Map.panTo(new daum.maps.LatLng(el.y_crdn, el.x_crdn));
            //setTimeout(function () {
                el.cctvClickEvt(el);

                
           // }, 100 * 5);
            return true;
        }
    });
}
/*VMS 정보*/

function showVmsInfo(id,nm) {
	$('#selectVms').text(nm+' 지도 VMS 정보');
    _VmsCtlrArr.some(function (el) {
        if (el.vms_ctlr_nmbr == id) {
            _Map.panTo(new daum.maps.LatLng(el.y_crdn, el.x_crdn));
            
            //setTimeout(function () {
                el.vmsClickEvt(el);
            //}, 100 * 5);
                $('#vmsClose').focus();
            return false;
        }
    });
}

/*돌발 정보*/

function showIncdInfo(id,nm) {

	$('#selectIncd').text(nm+' 지도 돌발 정보');
    var lettersStr = '';
    _IncdOcrrArr.some(function (el) {
        if (el.incd_ocrr_id == id) {
            _Map.panTo(new daum.maps.LatLng(el.y_crdn, el.x_crdn));
            el.incdClickEvt();
            $('#incdClose').focus();
            if (_RoadListStr != '' && _RoadListStr.length > 0) {
                _RoadListStr = '';
                $('#mapOfLetters').empty();
            }

            lettersStr += '<tr><td style="width:30% !important"><a href="javascript:void(0)" title="제목:' +
                    el.incd_titl + '">[' + el.incd_titl + '] <strong>' + el.road_name + '</strong><' +
                    '/a></td><td style="width:30% !important"><a href="javascript:void(0)" title="위' +
                    '치:' + el.road_name + '">위치 : ' + el.road_name + '</a></td><td><a href="javascr' +
                    'ipt:void(0)" title="설명:' + el.incd_expl + '">설명 : ' + el.incd_expl +
                    '</a></td>'
            '<td><a href="javascript:void(0)" title="기간:' + el.incd_strt_dt + '부터' + el.incd_end_prar_dt +
                    '까지">기간 : ' + el.incd_strt_dt + ' 부터 <p>' + el.incd_end_prar_dt + ' 까지</p></a><' +
                    '/td></tr>';

            _RoadListStr = lettersStr;
            $('#mapOfLetters')
                .empty()
                .html(_RoadListStr);

            return true;
        }
    });

    //돌발목록에서 돌발선택시 문자탭으로 이동..
   // $('#mapOfLetters tr:nth-of-type(1) td:nth-of-type(1) a').focus();
}

/*주차 정보*/

function showParkingInfo(id,nm,comm) {
	$('#selectParking').text(nm+' 지도 주차 정보');
    if (_Level != 3) 
        _Map.setLevel(3);
    
    _ParkingInfoArr.some(function (el) {
        if (el.park_id == id) {
            _Map.setCenter(new daum.maps.LatLng(el.x_crdn, el.y_crdn));
            el.parkingClickEvt(comm);

            parrkingInfoStr(
                el.park_id,
                el.park_nm,
                el.park_type_cd,
                el.park_fee,
                el.park_num,
                el.addr,
                el.x_crdn,
                el.y_crdn,
                el.rmrk
            );
            return;
        } else if (el.park_nm == id) {
            _Map.setCenter(new daum.maps.LatLng(el.x_crdn, el.y_crdn));
            el.parkingClickEvt(comm);

            parrkingInfoStr(
                el.park_id,
                el.park_nm,
                el.park_type_cd,
                el.park_fee,
                el.park_num,
                el.addr,
                el.x_crdn,
                el.y_crdn,
                el.rmrk
            );
            return;
        }
    });
    //$('.parkingInfo ul li:nth-of-type(1) span a').focus();
}

var beforeParkingLot = null;
function parrkingInfoStr(
    park_id,
    park_nm,
    park_type_cd,
    park_fee,
    park_num,
    addr,
    x_crdn,
    y_crdn,
    rmrk
) {

    if (beforeParkingLot != null) {
        beforeParkingLot.attr('style', 'background-color:#fff');
    }
    beforeParkingLot = $(park_id).css('background-color', '#dcdcdc');
    var typeCd = park_type_cd == 1? '노상': park_type_cd == 2 ? '노외': '부설';
    var fee = park_fee == null? '정보없음': park_fee == 1? '유료': '무료';

    var content = '';

    /*	content += '<ul><li><label>주차면수</label><span><a href="javascript:void(0)" title="주차면수: 대" tabindex="24">'+park_num+'</a></span></li>';
	content += '<li><label>장애인 주차</label><span><a href="javascript:void(0)" title="장애인주차:대" tabindex="24">-</a></span></li>';
	content += '<li><label>구분</label><span><a href="javascript:void(0)" title="구분:공영 / 노외" tabindex="24">'+typeCd+'</a></span></li>';
	content +='<li><label>기본요금</label><span><a href="javascript:void(0)" title="기본요금: 분 무료" tabindex="24">'+fee+'</a></span></li>';
	content += '<li><label>추가요금</label><span><a href="javascript:void(0)" title="소형요금:원 / 30분" tabindex="24">-</a></span></li>';
	content +='<li><span style="margin-left:60px"><a href="javascript:void(0)" title="소형최대요금:원 / 일" tabindex="24">-</a></span></li>';
	content += '<li><span style="margin-left:60px"><a href="javascript:void(0)" title="대형요금:원 / 30분" tabindex="24">-</a></span></li>';
	content += '<li><span style="margin-left:60px"><a href="javascript:void(0)" title="대형최대요금:원 / 일" tabindex="24">-</a></span></li>';
	content += '<li><label>주소</label><span><a href="javascript:void(0)" title=주소"" tabindex="24">'+addr+'</a></span></li>';
	content += '<li><label>연락처</label><span><a href="javascript:void(0)" title="연락처" tabindex="24">-</a></span></li></ul>';
	*/
    content += '<ul><li><label>주차면수</label><span><a href="javascript:void(0)" title="주차면수: 대" ' +
            'tabindex="24">' + park_num + '</a></span></li>';
    content += '<li><label>구분</label><span><a href="javascript:void(0)" title="구분:공영 / 노외" tab' +
            'index="24">' + typeCd + '</a></span></li>';
    content += '<li><label>기본요금</label><span><a href="javascript:void(0)" title="기본요금: 분 무료" t' +
            'abindex="24">' + fee + '</a></span></li>';
    content += '<li><label>주소</label><span><a href="javascript:void(0)" title=주소"" tabindex="2' +
            '4">' + addr + '</a></span></li>';

    $('.parkingInfo')
        .empty()
        .html(content);

}