﻿/* 날씨 정보 업데이트: 1->2->3->4->5 */

/* 2. Weatheri에서 제공한 날씨 정보, XML Parsing 
 * Delay: 정보 변경 간격(2초)
 * offset: 현재 위치 정보
 */
var Weather = {
    Delay: 2000,
    list: undefined,
    offset: 0,
    Timeout: undefined,
    loadWeather: function()                                             
    {
        $.ajax({
            url: 'http://www.yonhapnews.co.kr/weather/wx_current.xml',
            dataType: 'xml',
            success: function(data, textStatus, jqXHR) 
            {
                var str = jqXHR.responseXML;
                Weather.parseXml(str);
            }
        });
    }, 

/* 3. XML 데이타를 읽어서 리스트 생성 */
    parseXml: function(str)
    {
        var xmlDoc = str;
        Weather.list = $(xmlDoc).find("RECORD");
        Weather.slide();
    },

/* 4. HTML 삽입, offset값 증가
 * ClearTimeout: 브라우저의 중복 실행이 최소화되도록 클리어
 * makeDD()를 호출하여 태그 정보를 리턴 받는다
 */
    slide: function()
    {   
        var aa = Weather.makeDD();
        var str = '<dt class="blind">날씨</dt>\n';
        str += '<dd>\n';
        str += '    <a href="http://www.yonhapnews.co.kr/weather/index.html" title="날씨 상세보기"><span class="lctn">' + aa['city'] + '</span><span class="wtr">' + aa['wx'] + '<span class="wtr' + aa['wxCode'] + '"></span></span><span class="tmpr">' + aa['temp'] + '℃</span></a>\n';
        str += '</dd>\n';
        $('.top_wtr > dl').html(str);
        Weather.offset++;
        clearTimeout(Weather.Timeout);
        Weather.Timeout = setTimeout(Weather.slide, Weather.Delay);
    },

/* 5. HTML dd태그 정의 */
    makeDD: function()
    {
        var list = Weather.list;
        if (Weather.offset < 0)
            Weather.offset = 0;
        if (Weather.offset >= list.length)
            Weather.offset = 0;
            //Weather.offset = list.length-1;
        var city = list.eq(Weather.offset).find('POINT_NAME').text();
        var wx = list.eq(Weather.offset).find('WX').text();
        var wxCode = list.eq(Weather.offset).find('WX_CODE').text();
        if (parseInt(wxCode) < 10)
            wxCode = '0' + wxCode;
        var temp = list.eq(Weather.offset).find('TEMPERATURE').text();

        var rv = {};
        rv['city'] = city;
        rv['wx'] = wx;
        rv['wxCode'] = wxCode;
        rv['temp'] = temp;
        return rv;
    },

/* 1. 날씨 정보를 읽어오기 위한 함수 시작, Weather.loadWeather() 함수 실행*/
    start: function()
    {
        Weather.loadWeather();
    }
}

/* html page에서 실행시키는 것이 정석 */
/*
$(document).ready(function() {
    Weather.start();
}); 
*/
