function signalPhaseData(idx, aRingPhaseDeg, bRingPhaseDeg) {
    this.idx = idx;
    this.aRingPhaseDeg = aRingPhaseDeg;
    this.bRingPhaseDeg = bRingPhaseDeg;
}

function ringPhaseData(ring0, ring1, ring2, ring3, ring4, ring5, ring6, ring7) {
    this.ring0 = ring0;
    this.ring1 = ring1;
    this.ring2 = ring2;
    this.ring3 = ring3;
    this.ring4 = ring4;
    this.ring5 = ring5;
    this.ring6 = ring6;
    this.ring7 = ring7;
}

function sigMetaData(regionCd, regionNm, intNo, intNm, intSigCycleCnt, intSigCycleLen, collctDtime, commOnOffFlag, contrlrOperModeCd, siglightTurnoffFlag, siglightBlinkFlag, contrlrManualFlag, groupNo, mapNo) {
    this.regionCd = regionCd;
    this.regionNm = regionNm;
    this.intNo = intNo;
    this.intNm = intNm;
    this.intSigCycleCnt = intSigCycleCnt;
    this.intSigCycleLen = intSigCycleLen;
    this.collctDtime = collctDtime;
    this.commOnOffFlag = commOnOffFlag;
    this.contrlrOperModeCd = contrlrOperModeCd;
    this.siglightTurnoffFlag = siglightTurnoffFlag;
    this.siglightBlinkFlag = siglightBlinkFlag;
    this.contrlrManualFlag = contrlrManualFlag;
    this.groupNo = groupNo;
    this.mapNo = mapNo;
}

function weatherObj(addr, lat, lng, t1h, sky, pty, hdongCode, pm10Value, pm25Value) {
    t1h = (t1h == null) ? '-' : t1h;
    sky = (sky == null) ? '-' : sky;
    pty = (pty == null) ? '-' : pty;
    var pm10Color = '';
    var pm25Color = '';
    pm10Value = (pm10Value == null) ? null : parseInt(pm10Value, 10);
    pm25Value = (pm25Value == null) ? null : parseInt(pm25Value, 10);

    if (pm10Value != null) {
        if (0 <= pm10Value && pm10Value <= 30) {
            pm10Value = '좋음';
            pm10Color = '#32a1ff';
        } else if (31 <= pm10Value && pm10Value <= 81) {
            pm10Value = '보통';
            pm10Color = '#00c73c';
        } else if (81 <= pm10Value && pm10Value <= 150) {
            pm10Value = '나쁨';
            pm10Color = '#fd9b5a';
        } else {
            pm10Value = '매우나쁨';
            pm10Color = '#ff5959';
        }
    } else {
        pm10Value = '-';
    }

    if (pm25Value != null) {
        if (0 <= pm25Value && pm25Value <= 15) {
            pm25Value = '좋음';
            pm25Color = '#32a1ff';
        } else if (16 <= pm25Value && pm25Value <= 35) {
            pm25Value = '보통';
            pm25Color = '#00c73c';
        } else if (36 <= pm25Value && pm25Value <= 75) {
            pm25Value = '나쁨';
            pm25Color = '#fd9b5a';
        } else {
            pm25Value = '매우나쁨';
            pm25Color = '#ff5959';
        }
    } else {
        pm25Value = '-';
    }

    var skyType = 'sky' + sky;

    if (pty != 0) skyType = 'pty' + pty;

    var pm10Obj = {
        pm10Value: pm10Value,
        pm10Color: pm10Color
    }
    var pm25Obj = {
        pm25Value: pm25Value,
        pm25Color: pm25Color
    }

    var weatherMiniEl = document.createElement('div');
    weatherMiniEl.className = 'WeatherMini';

    var leftEl = document.createElement('div');
    leftEl.className = 'left';
    var leftChildEl = document.createElement('div');
    leftChildEl.setAttribute('data-id', 'weatherMiniThumb');
    leftChildEl.className= 'thumb '+ skyType;
    leftEl.appendChild(leftChildEl);
    weatherMiniEl.appendChild(leftEl);

    var rightEl = document.createElement('div');
    rightEl.className = 'right';
    weatherMiniEl.appendChild(rightEl);

    var centerEl = document.createElement('div');
    centerEl.className = 'center';

    var centerChildEl = document.createElement('div');
    centerChildEl.className = 'temperature';
    centerChildEl.setAttribute('data-id', 'weatherMiniTemperature');

    var numEl = document.createElement('span');
    numEl.className = 'num';
    numEl.innerHTML = t1h + '°C&nbsp;';
    centerChildEl.appendChild(numEl);

    var addrEl = document.createElement('span');
    addrEl.innerHTML = addr + '&nbsp;' + '&gt;';
    centerChildEl.appendChild(addrEl);
    centerEl.appendChild(centerChildEl);

    var locEl = document.createElement('div');
    locEl.className = 'loc';
    locEl.innerHTML = "미세 <span style=\"font-weight: bold; color: "+pm10Color+";\">"+pm10Value+"</span> 초미세 <span style=\"font-weight: bold; color: "+pm25Color+";\">"+pm25Value+"</span>";
    centerEl.appendChild(locEl);
    weatherMiniEl.appendChild(centerEl);

    weatherMiniEl.addEventListener('click', function () {
        weatherEventListener(hdongCode, JSON.stringify(pm10Obj), JSON.stringify(pm25Obj));
    });

    this.weatherOverlay = new kakao.maps.CustomOverlay({
        clickable: true,
        content: weatherMiniEl,
        position: new kakao.maps.LatLng(lat, lng),
        xAnchor: 0.5,
        yAnchor: -0.1,
        zIndex: 10,
        map: map
    });
}

function weatherEventListener (hdongCode, pm10Obj, pm25Obj) {
    var option = {
        url: 'main/getSpaceWeatherInfo.do',
        param: {LEVEL: level, HDONG_CODE: hdongCode},
        callback: function (data) {
            getSpaceWeatherInfoCallback(data, pm10Obj, pm25Obj)
        }
    };
    callService(option);
}

function getSpaceWeatherInfoCallback(json, pm10Obj, pm25Obj) {

    pm10Obj = JSON.parse(pm10Obj);
    pm25Obj = JSON.parse(pm25Obj);

    var list = json.data.list;
    if (typeof list == 'undefined') return;

    if (list.length > 0) {
        var t1h = (weatherInfoObj[list[0].HDONG_CODE].T1H == null) ? '-' : weatherInfoObj[list[0].HDONG_CODE].T1H;
        var sky = (weatherInfoObj[list[0].HDONG_CODE].SKY == null) ? '-' : weatherInfoObj[list[0].HDONG_CODE].SKY;
        var pty = (weatherInfoObj[list[0].HDONG_CODE].PTY == null) ? '-' : weatherInfoObj[list[0].HDONG_CODE].PTY;

        var skyType = 'sky' + sky;
        if (pty != '-' && pty != 0) skyType = 'pty' + pty;

        if (weatherInfoWindow != null) weatherInfoWindow.close();

        var d = new Date();
        var year = d.getFullYear();
        var month = (d.getMonth() + 1) < 10 ? '0'+(d.getMonth() + 1) : (d.getMonth() + 1);
        var now = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
        var tomorrow = (d.getDate() + 1) < 10 ? '0' + (d.getDate() + 1) : (d.getDate() + 1);
        var dayAfer = (d.getDate() + 2) < 10 ? '0' + (d.getDate() + 2) : (d.getDate() + 2);
        var todayString = year+""+month+""+now;
        var tomString = year+""+month+""+tomorrow;
        var daString = year+""+month+""+dayAfer;
        var curHour = d.getHours();

        var weatherInfoWindowEl = document.createElement('div');
        weatherInfoWindowEl.className = 'WeatherInfoWindow';

        var topEl = document.createElement('div');
        topEl.className = 'top';
        weatherInfoWindowEl.appendChild(topEl);

        var midEl = document.createElement('div');
        midEl.className = 'mid';

        var weatherThumbEl = document.createElement('span');
        weatherThumbEl.className = 'weatherThumb ' + skyType;
        midEl.appendChild(weatherThumbEl);

        var wrapTitEl = document.createElement('div');
        wrapTitEl.className = 'wrapTit';
        var titEl = document.createElement('strong');
        titEl.className = 'tit';
        titEl.textContent = weatherInfoObj[list[0].HDONG_CODE].ADDR;
        wrapTitEl.appendChild(titEl);

        var temperEl = document.createElement('div');
        temperEl.className = 'temper';
        temperEl.innerHTML = '<span class="num">' + t1h + '</span><span class="unit">°C</span><span class="txt_wea">' + checkSkyText(skyType) + '</span>';
        wrapTitEl.appendChild(temperEl);

        var dustEl = document.createElement('div');
        dustEl.innerHTML = '미세 <span style="color: '+pm10Obj.pm10Color+';">'+pm10Obj.pm10Value+'</span> 초미세먼지 <span style="color:'+pm25Obj.pm25Color+';">'+pm25Obj.pm25Value+'</span>';
        wrapTitEl.appendChild(dustEl);

        var sourceEl = document.createElement('small');
        sourceEl.className = 'date';
        sourceEl.textContent = '※출처:한국환경공단';
        sourceEl.style.fontSize = '5px';
        wrapTitEl.appendChild(sourceEl);
        midEl.appendChild(wrapTitEl);

        var dateEl = document.createElement('span');
        dateEl.className = 'date';
        dateEl.textContent = '현재 ' + weatherInfoObj[list[0].HDONG_CODE].BASEDATE + ' ' + weatherInfoObj[list[0].HDONG_CODE].BASETIME;
        wrapTitEl.appendChild(dateEl);

        var closeBtnEl = document.createElement('a');
        closeBtnEl.textContent = '닫기';
        closeBtnEl.className = 'img_wt_png btnClose';
        closeBtnEl.addEventListener('click', function (e) {
            weatherInfoWindow.close();
        });
        midEl.appendChild(closeBtnEl);

        var middleEl = document.createElement('ul');
        middleEl.className = 'middle';

        var fstEl = document.createElement('li');
        fstEl.className = 'fst';
        var secEl = document.createElement('li');
        secEl.className = 'sec';
        var thdEl = document.createElement('li');
        thdEl.className = 'thd';
        var lstEl = document.createElement('li');
        lstEl.className = 'lst';

        // 기준 시간 오전 9시, 오후 15시
        if (curHour < 12) { // 12시이전에는 오늘오후부터 표출
            list.forEach(function (value) {
                var liDateEl = document.createElement('div');
                var liWeatherEl = document.createElement('div');
                var liWeatherThumbEl = document.createElement('span');
                var liTemperEl = document.createElement('span');

                if (value.FCSTDATE === todayString) {
                    if (value.FCSTTIME === '1500') {
                        liDateEl.className = 'date';
                        liDateEl.textContent = '오늘 오후';
                        fstEl.appendChild(liDateEl);

                        liWeatherThumbEl.className = 'weatherThumb '+ ('sky'+value.SKY);
                        liWeatherEl.appendChild(liWeatherThumbEl);

                        liTemperEl.className = 'temper';
                        liTemperEl.textContent = value.T3H + '°C';
                        liWeatherEl.appendChild(liTemperEl);
                        fstEl.appendChild(liWeatherEl);
                    }
                } else if (value.FCSTDATE === tomString) {
                    if (value.FCSTTIME === '0900') {
                        liDateEl.className = 'date';
                        liDateEl.textContent = '내일 오전';
                        secEl.appendChild(liDateEl);

                        liWeatherThumbEl.className = 'weatherThumb '+ ('sky'+value.SKY);
                        liWeatherEl.appendChild(liWeatherThumbEl);

                        liTemperEl.className = 'temper';
                        liTemperEl.textContent = value.T3H + '°C';
                        liWeatherEl.appendChild(liTemperEl);
                        secEl.appendChild(liWeatherEl);
                    }
                    if (value.FCSTTIME === '1500') {
                        liDateEl.className = 'date';
                        liDateEl.textContent = '내일 오후';
                        thdEl.appendChild(liDateEl);

                        liWeatherThumbEl.className = 'weatherThumb '+ ('sky'+value.SKY);
                        liWeatherEl.appendChild(liWeatherThumbEl);

                        liTemperEl.className = 'temper';
                        liTemperEl.textContent = value.T3H + '°C';
                        liWeatherEl.appendChild(liTemperEl);
                        thdEl.appendChild(liWeatherEl);
                    }
                } else if (value.FCSTDATE === daString) {
                    if (value.FCSTTIME === '0900') {
                        liDateEl.className = 'date';
                        liDateEl.textContent = '모래 오전';
                        lstEl.appendChild(liDateEl);

                        liWeatherThumbEl.className = 'weatherThumb '+ ('sky'+value.SKY);
                        liWeatherEl.appendChild(liWeatherThumbEl);

                        liTemperEl.className = 'temper';
                        liTemperEl.textContent = value.T3H + '°C';
                        liWeatherEl.appendChild(liTemperEl);
                        lstEl.appendChild(liWeatherEl);
                    }
                }
            })
        } else { // 12시 이후는 내일 오전부터 표출
            list.forEach(function (value) {
                var liDateEl = document.createElement('div');
                var liWeatherEl = document.createElement('div');
                var liWeatherThumbEl = document.createElement('span');
                var liTemperEl = document.createElement('span');

                if (value.FCSTDATE === tomString) {
                    if (value.FCSTTIME === '0900') {
                        liDateEl.className = 'date';
                        liDateEl.textContent = '내일 오전';
                        fstEl.appendChild(liDateEl);

                        liWeatherThumbEl.className = 'weatherThumb '+ ('sky'+value.SKY);
                        liWeatherEl.appendChild(liWeatherThumbEl);

                        liTemperEl.className = 'temper';
                        liTemperEl.textContent = value.T3H + '°C';
                        liWeatherEl.appendChild(liTemperEl);
                        fstEl.appendChild(liWeatherEl);
                    }
                    if (value.FCSTTIME === '1500') {
                        liDateEl.className = 'date';
                        liDateEl.textContent = '내일 오후';
                        secEl.appendChild(liDateEl);

                        liWeatherThumbEl.className = 'weatherThumb '+ ('sky'+value.SKY);
                        liWeatherEl.appendChild(liWeatherThumbEl);

                        liTemperEl.className = 'temper';
                        liTemperEl.textContent = value.T3H + '°C';
                        liWeatherEl.appendChild(liTemperEl);
                        secEl.appendChild(liWeatherEl);
                    }
                } else if (value.FCSTDATE === daString) {
                    if (value.FCSTTIME === '0900') {
                        liDateEl.className = 'date';
                        liDateEl.textContent = '모레 오전';
                        thdEl.appendChild(liDateEl);

                        liWeatherThumbEl.className = 'weatherThumb '+ ('sky'+value.SKY);
                        liWeatherEl.appendChild(liWeatherThumbEl);

                        liTemperEl.className = 'temper';
                        liTemperEl.textContent = value.T3H + '°C';
                        liWeatherEl.appendChild(liTemperEl);
                        thdEl.appendChild(liWeatherEl);
                    }
                    if (value.FCSTTIME === '1500') {
                        liDateEl.className = 'date';
                        liDateEl.textContent = '모레 오후';
                        lstEl.appendChild(liDateEl);

                        liWeatherThumbEl.className = 'weatherThumb '+ ('sky'+value.SKY);
                        liWeatherEl.appendChild(liWeatherThumbEl);

                        liTemperEl.className = 'temper';
                        liTemperEl.textContent = value.T3H + '°C';
                        liWeatherEl.appendChild(liTemperEl);
                        lstEl.appendChild(liWeatherEl);
                    }
                }
            })
        }

        middleEl.appendChild(fstEl);
        middleEl.appendChild(secEl);
        middleEl.appendChild(thdEl);
        middleEl.appendChild(lstEl);
        midEl.appendChild(middleEl);
        weatherInfoWindowEl.appendChild(midEl);

        var botEl = document.createElement('div');
        botEl.className = 'bot';
        weatherInfoWindowEl.appendChild(botEl);

        weatherInfoWindow = new kakao.maps.InfoWindow({
            position: new kakao.maps.LatLng(weatherInfoObj[list[0].HDONG_CODE].LAT, weatherInfoObj[list[0].HDONG_CODE].LNG),
            content: weatherInfoWindowEl,
            zIndex: 100,
            map: map
        });

        $('.WeatherInfoWindow').parent().parent().css('background', 0).css('border', 0).children('div:nth-child(1)').css('display', 'none');
    }
}

function checkSkyText(sky) {

    if (sky == '-') return sky;
    var text;

    if (sky == 'sky1') text = '맑음';
    else if (sky == 'sky3') text = '구름많음';
    else if (sky == 'sky4') text = '흐림';
    else if (sky == 'pty1') text = '비';
    else if (sky == 'pty2') text = '비/눈';
    else if (sky == 'pty3') text = '눈';
    else if (sky == 'pty4') text = '소나기';
    else if (sky == 'pty5') text = "빗방울";
    else if (sky == 'pty6') text = "빗방울/눈날림";
    else if (sky == 'pty7') text = "눈날림";

    return text;
}

function plusZero(num) {
    if (num < 10) num = '0' + num;
    return num;
}