var Weather= {
    temperature_now: 0,
    temperature_night: 0,
    temperature_tomorrow: 0,
    today_icon: '',
    town: 'Краснодар',
    _xml: null,

    load_xml: function(){
        if (Weather._xml==null) {
            $.get('/modules/weather/cache/day.xml',{},function(data){
                 Weather._xml= data;
                 Weather.init();
            }, "xml");
        }
    },

    init: function() {
        if (Weather._xml==null) {
            Weather.load_xml();
        }
        else
        {
            var currentDate= new Date();

            Weather.town= unescape($('forecast_information postal_code', Weather._xml).attr('data'));

            Weather.temperature_now=$('current_conditions temp_c', Weather._xml).attr('data');
            Weather.temperature_night=$('current_conditions temp_c', Weather._xml).attr('data');
            Weather.temperature_tomorrow=$('forecast_conditions:first high', Weather._xml).attr('data');
            Weather.today_icon=$('current_conditions icon', Weather._xml).attr('data');

            Weather.setTodayWeather();
            Weather.setWeekWeather();
        }
    },

    setTodayWeather: function(){
        $('#weather-oneday #town').text(Weather.town);
        $('#weather-oneday #now-temperature').text(Weather.temperature_now);
        $('#weather-oneday #night-temperature span').text(Weather.temperature_night);
        $('#weather-oneday #tomorrow-temperature span').text(Weather.temperature_tomorrow);
        $('#weather-oneday img#weather-icon').attr('src', 'http://www.google.com'+Weather.today_icon);
    },

    setWeekWeather: function(){
        $('#weather-week tr:first td').text(Weather.town);

        $('forecast_conditions', Weather._xml).each(function(){
            tr_obj= $('#weather-week tr:eq(1)').clone();

            $('#weather-icon',tr_obj).attr('src','http://www.google.com'+$(this).find('icon').attr('data'));
            $('#day',tr_obj).text($(this).find('day_of_week').attr('data'));
            $('#temperature',tr_obj).html('Днём '+$(this).find('low').attr('data')+'<br/>Ночью '+$(this).find('high').attr('data'));

            $('#weather-week').append(tr_obj);
        });
        $('#weather-week tr:eq(1)').remove();
    }
}

$().ready(function(){

    Weather.init();

    var oldValue= '';

    $('a#weather-switch').click(function(){
        if ( oldValue=='' ) {
            oldValue = this.text;
            $(this).text('Прогноз на сегодня');
        } else {
            $(this).text( oldValue );
            oldValue = '';
        }
        $('#weather-place table').toggle();
      
        return false;
    });
});
