/*
 * @(#)jQuery.CEA.Weather.js
 *       
 * 系统名称：东航电子商务国内B2C系统
 * 版本号：1.0
 * 
 * Copyright (c)  TravelSky
 * All Rights Reserved.
 * 
 * 作者：bjiang
 * 创建日期：Nov 4, 2009
 * 
 * 功能描述：天气控制器
 *      需要DWR支持.
 *      需要Cookies支持.
 * 公用方法描述：
 *      $('#weather').jWeather({city:'北京'});
 *      加载完成后，自动替换weather标记的位置，为天气预报DIV      
 *    
 * 修改人：
 * 修改日期：
 * 修改原因：
 * 
 * 
 */
 if(typeof Weather=='undefined')
 {
     $.ajax({
        type:"GET",
        url:'/mu/dwr/interface/Weather.js',
        async: false,
        dataType:'script'
        });
 }
(function($)
{
    var wimg = Array();
    wimg['中雨'] = 'weather_1.gif';
    wimg['中雨转小雨'] = 'weather_2.gif';
    wimg['中雪'] = 'weather_3.gif';
    wimg['中雪转小雪'] = 'weather_4.gif';
    wimg['冻雨'] = 'weather_5.gif';
    wimg['多云'] = 'weather_6.gif';
    wimg['多云转中雪'] = 'weather_7.gif';
    wimg['多云转小雨'] = 'weather_8.gif';
    wimg['多云转小雪'] = 'weather_9.gif';
    wimg['多云转晴'] = 'weather_10.gif';
    wimg['多云转阴'] = 'weather_11.gif';
    wimg['多云转阵雨'] = 'weather_12.gif';
    wimg['多云转阵雪'] = 'weather_13.gif';
    wimg['多云转雨夹雪'] = 'weather_14.gif';
    wimg['多云转雾'] = 'weather_15.gif';
    wimg['大雨'] = 'weather_16.gif';
    wimg['大雪'] = 'weather_17.gif';
    wimg['大雪转晴'] = 'weather_18.gif';
    wimg['小雨'] = 'weather_19.gif';
    wimg['小雨转冻雨'] = 'weather_20.gif';
    wimg['小雨转多云'] = 'weather_21.gif';
    wimg['小雨转阴'] = 'weather_22.gif';
    wimg['小雨转雨夹雪'] = 'weather_23.gif';
    wimg['小雪'] = 'weather_24.gif';
    wimg['小雪转'] = 'weather_25.gif';
    wimg['小雪转多云'] = 'weather_26.gif';
    wimg['小雪转大雪'] = 'weather_27.gif';
    wimg['小雪转晴'] = 'weather_28.gif';
    wimg['晴'] = 'weather_29.gif';
    wimg['晴转多云'] = 'weather_30.gif';
    wimg['晴转阴'] = 'weather_31.gif';
    wimg['未知'] = 'weather_32.gif';
    wimg['阴'] = 'weather_33.gif';
    wimg['阴转多云'] = 'weather_34.gif';
    wimg['阴转大雨'] = 'weather_35.gif';
    wimg['阴转小雨'] = 'weather_36.gif';
    wimg['阴转小雪'] = 'weather_37.gif';
    wimg['阴转晴'] = 'weather_38.gif';
    wimg['阴转阵雪'] = 'weather_39.gif';
    wimg['阴转雨夹雪'] = 'weather_40.gif';
    wimg['阵雨'] = 'weather_41.gif';
    wimg['阵雨转中雨'] = 'weather_42.gif';
    wimg['阵雨转多云'] = 'weather_43.gif';
    wimg['阵雨转小雨'] = 'weather_44.gif';
    wimg['阵雨转晴'] = 'weather_45.gif';
    wimg['阵雨转阴'] = 'weather_46.gif';
    wimg['阵雪转多云'] = 'weather_47.gif';
    wimg['阵雪转小雪'] = 'weather_48.gif';
    wimg['雨夹雪'] = 'weather_49.gif';
    wimg['雨夹雪转中雪'] = 'weather_50.gif';
    wimg['雨夹雪转冻雨'] = 'weather_51.gif';
    wimg['雨夹雪转多云'] = 'weather_52.gif';
    wimg['雨夹雪转小雨'] = 'weather_53.gif';
    wimg['雨夹雪转小雪'] = 'weather_54.gif';
    wimg['雨夹雪转阴'] = 'weather_55.gif';
    wimg['雷'] = 'weather_56.gif';
    wimg['雷阵雨'] = 'weather_57.gif';
    wimg['雷阵雨转阵雨'] = 'weather_58.gif';
    wimg['雾'] = 'weather_59.gif';
    wimg['雾转多云'] = 'weather_60.gif';
    wimg['雾转晴'] = 'weather_61.gif';
    wimg['高温'] = 'weather_62.gif';

	var $div = $('<div class="weather" id="Major"><div id="icon"><img src="/mu/images/weather/weather_unknown.gif"/></div><div id="city_date"> </div><div id="weather_details"><span id="today_d"></span><span style="display:block" id="today_w"></span></div><div id="tomorrow_after"><table cellpadding="0" cellspacing="0" border="0"><tr><td id="thu_d"></td><td id="fri_d"></td></tr><tr class="line2"><td id="thu_w"></td><td id="fri_w"></td></tr></table><div id="declare">本天气预报由中国航信提供数据</div></div><div class="clear"></div></div>');
    $.fn.jWeather = function(options)
    {
        var settings = {
            city:null
        };
        var config = $.extend(settings, options);
        $this = $(this);
        var rediv = $div.clone();
        var date = new Date();
        var today = date.getFullYear() + '-'+ (date.getMonth()+1) + '.' + date.getDate();
        date.setDate(date.getDate()+1);
        var thu = date.getFullYear() + '-'+ (date.getMonth()+1) + '.' + date.getDate();
        date.setDate(date.getDate()+1);
        var fri = date.getFullYear() + '-'+ (date.getMonth()+1) + '.' + date.getDate();
        var init = function()
        {
             $this.hide();
             if(config.city==""||config.city==null)
             {
                if(!$.isFunction(jQuery.cookie))
                {
                $.ajax({
                    type:"GET",
                    cache:true,
                    url:'/mu/images/jQuery.cookies.js',
                    async: false,
                    dataType:'script'
                    });
                }
                config.city=$.cookie('city');
             }
             Weather.getWeather(config.city,3,function(weather)
             {
                try
                {
              		var weather = weather.match(/[^\>\n]+/g).slice(0, weather.length);
              		for(i in weather)
              		{
              			weather[i]=weather[i].split(/\|/g);
              		}
                    rediv.find('#today_d').text(today + ' ' + weather[0][0] + '天气');
                    rediv.find('#thu_d').text(thu);
                    rediv.find('#fri_d').text(fri);
                    rediv.find('#today_w').text(weather[0][3] + ','+weather[0][1] + '~' + weather[0][2] + '℃');
                    rediv.find('#thu_w').  text(weather[1][3] + ','+weather[1][1] + '~' + weather[1][2] + '℃');
                    rediv.find('#fri_w').  text(weather[2][3] + ','+weather[2][1] + '~' + weather[2][2] + '℃');
                    
                    rediv.find('#icon img').attr('src','/mu/images/' + (wimg[weather[0][3]]===undefined?'weather_32.gif':wimg[weather[0][3]]));
                    //$this.replaceWith(rediv);
                    //$this.show();
                    $this.hide();
                }
                catch(e)
                {
                
                }
             });
             return rediv;
        }
        return init();
    }
})(jQuery);

$(document).ready(function()
{
  $('.weather').jWeather({city:$.cookie('_3CODE')==='NULL'?'SHA':$.cookie('_3CODE')});
})
