//2007-7 iTsmy 修改提高天气精确度

var xmlhttp;   //发送网页请求的对象 
var url = "http://weather.tq121.com.cn/detail.php?city=";
var isValid = false;  
var lastUpdateTime; 
var timehandle;
var hint,hint2,hint3,hint4;
var isRefreshing = false; //正在刷新
var mhandle; 
var isMiniState=false;
 
//当天天气代码 
var SkyCode;  
//1-5天的日期文字 例如  DayData1 = '4月8日' 
var DayDate1,DayDate2,DayDate3,DayDate4,DayDate5;
//1-5天的上午天气图标序号  
var weatherIcon1AM,weatherIcon2AM,weatherIcon3AM,weatherIcon4AM,weatherIcon5AM;
//1-5天的下午天气图标序号  
var weatherIcon1PM,weatherIcon2PM,weatherIcon3PM,weatherIcon4PM,weatherIcon5PM;
//1-5天的上午天气文字  
var weatherText1AM,weatherText2AM,weatherText3AM,weatherText4AM,weatherText5AM;
//1-5天的下午天气文字  
var weatherText1PM,weatherText2PM,weatherText3PM,weatherText4PM,weatherText5PM;
//1-5天的最高温度 
var tmp1High,tmp2High,tmp3High,tmp4High,tmp5High;
//1-5天的最低温度 
var tmp1Low,tmp2Low,tmp3Low,tmp4Low,tmp5Low;
//1-5天的风向风力 
var wind1,wind2,wind3,wind4,wind5;

//将所有天气数据赋值到控件 
function AssignWeatherInfoToControls()
{
  //先锁定刷新，方式闪动 
  LockRefresh;
  
  if(isValid)
  {
    hint = GetSetting( 'city' ) + ' ' + DayDate1; 
    hint = hint + '\n温度: ' + tmp1Low + ' - ' + tmp1High + '°' ;
    hint = hint + '\n上午: ' + weatherText1AM ;
    hint = hint + '\n下午: ' + weatherText1PM ;
    hint = hint + '\n风力: ' + wind1;
    hint = hint + '\n月相: ' + MoonState_cn();
  
    hint2 = DayDate2; 
    hint2 = hint2 + '\n温度: ' + tmp2Low + ' - ' + tmp2High + '°' ;
    hint2 = hint2 + '\n上午: ' + weatherText2AM ;
    hint2 = hint2 + '\n下午: ' + weatherText2PM ;
    hint2 = hint2 + '\n风力: ' + wind2;
    //hint = hint + '\n月相: ' + MoonState_cn();
  
    hint3 = DayDate3; 
    hint3 = hint3 + '\n温度: ' + tmp3Low + ' - ' + tmp3High + '°' ;
    hint3 = hint3 + '\n上午: ' + weatherText3AM ;
    hint3 = hint3 + '\n下午: ' + weatherText3PM ;
    hint3 = hint3 + '\n风力: ' + wind3;
    //hint = hint + '\n月相: ' + MoonState_cn();

    hint4 = DayDate4; 
    hint4 = hint4 + '\n温度: ' + tmp4Low + ' - ' + tmp4High + '°' ;
    hint4 = hint4 + '\n上午: ' + weatherText4AM ;
    hint4 = hint4 + '\n下午: ' + weatherText4PM ;
    hint4 = hint4 + '\n风力: ' + wind4;
    //hint = hint + '\n月相: ' + MoonState_cn();
  }




  if(isPM()) lastUpdateTime=2
  else if(isNight()) lastUpdateTime=3
  else lastUpdateTime=1;  

  if((dockedOnSidebar)||(widget.pageIndex==1))
  {
    sidebarOn();
  }
  else
  {
   sidebarOff();
  } 
  
  if(isRefreshing)
  {
    txtDockedCity.caption = '刷新...'
    txtCity.caption = '刷新...';
  }
     
  
   //解除刷新锁定 
   UnLockRefresh;  
   Refresh;
   timer1.enabled = true;
}

//解析下载的html网页获得天气信息 
function parseHtml(htmlStr)
{
   var str,tmpstr,n,i,txt;
   
   str = htmlStr;
   
   for(i=1;i<4;i++)
   {   
     tmpstr = '<td width="215" align="center" valign="middle"><span class="big-cn">';
     n = str.indexOf(tmpstr);
     if(n<1)
     {
       txtCity.caption = '查询失败';
       txtDockedCity.caption = '查询失败';
       return;
     }  
     str = str.substr( n + tmpstr.length);
     tmpstr = '</span>';
     n = str.indexOf(tmpstr);
     //获取第1-3天的日期文字
     txt =  trim(str.substr(0,n));
     switch(i) 
     {
       case 1 :
           DayDate1 = txt;
       case 2:
           DayDate2 = txt;
       case 3:
           DayDate3 = txt;
       default :
     } 
     //alert( txt );
      
     tmpstr = '<td width="111" align="center" valign="top"><img src="../images/a';  
     n = str.indexOf(tmpstr);
     str = str.substr( n + tmpstr.length );
     tmpstr = '.gif" width="70"';
     n = str.indexOf(tmpstr);
     //获取第1-3天上午的天气图标序号和文字 
     txt =  trim(str.substr(0,n));
     switch(i) 
     {
/*
       case 1 :
           weatherIcon1AM = transTq121Code(txt);
           if(!isPM) SkyCode = weatherIcon1AM;
           weatherText1AM = toChineseString(weatherIcon1AM);
       case 2:
           weatherIcon2AM = transTq121Code(txt);
           weatherText2AM = toChineseString(weatherIcon1AM);
       case 3:
           weatherIcon3AM = transTq121Code(txt);
           weatherText3AM = toChineseString(weatherIcon1AM);
*/
       case 1 :
           weatherIcon1AM = transTq121Code(txt);
           if(!isPM) SkyCode = weatherIcon1AM;
           weatherText1AM = toChineseString(txt);//iTsmy更改获取天气文字函数参数
       case 2:
           weatherIcon2AM = transTq121Code(txt);
           weatherText2AM = toChineseString(txt);//iTsmy更改获取天气文字函数参数
       case 3:
           weatherIcon3AM = transTq121Code(txt);
           weatherText3AM = toChineseString(txt);//iTsmy更改获取天气文字函数参数
       default :
     }
     //alert( txt );     
     str = str.substr( n + tmpstr.length);

  
  
     tmpstr = '<td width="110" align="center" valign="top"><img src="../images/a';  
     n = str.indexOf(tmpstr);
     str = str.substr( n + tmpstr.length );
     tmpstr = '.gif" width="70"';
     n = str.indexOf(tmpstr);
     //获取第1-3天下午的天气文字 
     txt =  trim(str.substr(0,n));
     switch(i) 
     {
/*
       case 1 :
           weatherIcon1PM = transTq121Code(txt);
           if(isPM()) SkyCode = weatherIcon1AM;
           weatherText1PM = toChineseString(weatherIcon1PM);
       case 2:
           weatherIcon2PM = transTq121Code(txt);
           weatherText2PM = toChineseString(weatherIcon1PM);
       case 3:
           weatherIcon3PM = transTq121Code(txt);
           weatherText3PM = toChineseString(weatherIcon1PM);
*/
       case 1 :
           weatherIcon1PM = transTq121Code(txt);
           if(isPM()) SkyCode = weatherIcon1AM;
           weatherText1PM = toChineseString(txt);//iTsmy更改获取天气文字函数参数
       case 2:
           weatherIcon2PM = transTq121Code(txt);
           weatherText2PM = toChineseString(txt);//iTsmy更改获取天气文字函数参数
       case 3:
           weatherIcon3PM = transTq121Code(txt);
           weatherText3PM = toChineseString(txt);//iTsmy更改获取天气文字函数参数
      default :
     } 
     //alert( txt );     
     str = str.substr( n + tmpstr.length);
     
     
     tmpstr = '<td width="215" align="center" valign="middle"><span class="big-cn">';  
     n = str.indexOf(tmpstr);
     str = str.substr( n + tmpstr.length );
     tmpstr = '℃';
     n = str.indexOf(tmpstr);
     //获取第1-3天最低气温 
     txt =  trim(str.substr(0,n));
     switch(i) 
     {
       case 1 :
           tmp1Low = txt;
       case 2:
           tmp2Low = txt;
       case 3:
           tmp3Low = txt;
       default :
     } 
     //alert( txt );     
     str = str.substr( n + tmpstr.length);
     

     tmpstr = '/';  
     n = str.indexOf(tmpstr);
     str = str.substr( n + tmpstr.length );
     tmpstr = '℃';
     n = str.indexOf(tmpstr);
     //获取第1-3天最高气温 
     txt =  trim(str.substr(0,n));
     switch(i) 
     {
       case 1 :
           tmp1High = txt;
       case 2:
           tmp2High = txt;
       case 3:
           tmp3High = txt;
       default :
     } 
     //alert( txt );     
     str = str.substr( n + tmpstr.length);
   
   
     tmpstr = '<td width="215" align="center" valign="middle"><span class="cn">';  
     n = str.indexOf(tmpstr);
     str = str.substr( n + tmpstr.length );
     tmpstr = '</span>';
     n = str.indexOf(tmpstr);
     //获取第一天风力风向 
     txt =  trim(str.substr(0,n));
     switch(i) 
     {
       case 1 :
           wind1 = txt;
       case 2:
           wind2 = txt;
       case 3:
           wind3 = txt;
       default :
     } 
     //alert( txt );     
     str = str.substr( n + tmpstr.length);
  }
  
  //第4-5天======================
  for(i=4;i<6;i++)
   {   
     tmpstr = '<td width="121"><span class="cn">';
     n = str.indexOf(tmpstr);
     str = str.substr( n + tmpstr.length);
     tmpstr = '</span>';
     n = str.indexOf(tmpstr);
     //获取第4-5天的日期文字
     txt =  trim(str.substr(0,n));
     switch(i) 
     {
       case 4 :
           DayDate4 = txt;
       case 5:
           DayDate5 = txt;
       default :
     } 
     //alert( txt );
     str = str.substr( n + tmpstr.length);
      
     tmpstr = '<td width="59"><img src="../images/b';  
     n = str.indexOf(tmpstr);
     str = str.substr( n + tmpstr.length );
     tmpstr = '.gif" width="50"';
     n = str.indexOf(tmpstr);
     //获取第4-5天上午的天气文字 
     txt =  trim(str.substr(0,n));
     switch(i) 
     {
/*
      case 4 :
           weatherIcon4AM = transTq121Code(txt);
           weatherText4AM = toChineseString(weatherIcon4AM);
      case 5:
           weatherIcon5AM = transTq121Code(txt);
           weatherText5AM = toChineseString(weatherIcon5AM);
*/
      case 4 :
           weatherIcon4AM = transTq121Code(txt);
           weatherText4AM = toChineseString(txt);//iTsmy更改获取天气文字函数参数
      case 5:
           weatherIcon5AM = transTq121Code(txt);
           weatherText5AM = toChineseString(txt);//iTsmy更改获取天气文字函数参数

       default :
     } 
     //alert( txt );     
     str = str.substr( n + tmpstr.length);

  
  
     tmpstr = '<td width="64" align="left"><img src="../images/b';  
     n = str.indexOf(tmpstr);
     str = str.substr( n + tmpstr.length );
     tmpstr = '.gif" width="50"';
     n = str.indexOf(tmpstr);
     //获取第4-5天下午的天气文字 
     txt =  trim(str.substr(0,n));
     switch(i) 
     {
/*
       case 4 :
           weatherIcon4PM = transTq121Code(txt);
           weatherText4PM = toChineseString(weatherIcon4PM);
       case 5:
           weatherIcon5PM = transTq121Code(txt);
           weatherText5PM = toChineseString(weatherIcon5PM);
*/
       case 4 :
           weatherIcon4PM = transTq121Code(txt);
           weatherText4PM = toChineseString(txt);//iTsmy更改获取天气文字函数参数
       case 5:
           weatherIcon5PM = transTq121Code(txt);
           weatherText5PM = toChineseString(txt);//iTsmy更改获取天气文字函数参数

       default :
     } 
     //alert( txt );     
     str = str.substr( n + tmpstr.length);
     
     
     tmpstr = '<td width="86" class="cn"><span class="wendu">';  
     n = str.indexOf(tmpstr);
     str = str.substr( n + tmpstr.length );
     tmpstr = '℃';
     n = str.indexOf(tmpstr);
     //获取第4-5天最低气温 
     txt =  trim(str.substr(0,n));
     switch(i) 
     {
       case 4 :
           tmp4Low = txt;
       case 5:
           tmp5Low = txt;

       default :
     } 
     //alert( txt );     
     str = str.substr( n + tmpstr.length);
     

     tmpstr = '/';  
     n = str.indexOf(tmpstr);
     str = str.substr( n + tmpstr.length );
     tmpstr = '℃';
     n = str.indexOf(tmpstr);
     //获取第4-5天最高气温 
     txt =  trim(str.substr(0,n));
     switch(i) 
     {
       case 4 :
           tmp4High = txt;
       case 5:
           tmp5High = txt;

       default :
     } 
     //alert( txt );     
     str = str.substr( n + tmpstr.length);
     tmpstr = '<td width="157"><span class="cn">';  
     n = str.indexOf(tmpstr);
     str = str.substr( n + tmpstr.length );
     tmpstr = '</span>';
     n = str.indexOf(tmpstr);
     //获取第4-5天风力风向 
     txt =  trim(str.substr(0,n));
     switch(i) 
     {
       case 4 :
           wind4 = txt;
       case 5:
           wind5 = txt;

       default :
     } 
     //alert( txt );     
     str = str.substr( n + tmpstr.length);
  }  
  //将所有天气数据赋值到控件 
  AssignWeatherInfoToControls(); 
   //刷新
   Refresh; 
   
}

function  onDownFile(fileName,errorCode)
{
  isRefreshing = false; 
  if(errorCode<1)
  {
    //alert("连接服务器出错: " + xmlhttp.statusText + " (" + xmlhttp.status  + ")");
    result = "";
    isRefreshing = false;
    isValid = false;
    hint = "连接服务器出错: " + xmlhttp.statusText + " (" + xmlhttp.status  + ")";  
    AssignWeatherInfoToControls();
    Print( errorCode );
    Print( hint );
    return;
  }
  isValid = true;
  parseHtml( readFile(fileName));
  deleteFile(  fileName );
  
   

}

//下载指定url网址的html代码 
function StartGetUrlHtmlString(url)
{
  try
  {
    var oStream = new ActiveXObject("ADODB.Stream");
  }
  catch(e)
  {
    oStream = null;
    //如果无法建立 ADODB.Stream 则改用内置下载函数直接来获取天气预报信息
    DownLoadFileAsync( url , "weatherinfo.tmp" ,"onDownFile" );  
    
    return;
  
  }

  //先创建一个xmlhttp的对象 
  createXmlhttp();
  
  //构造请求，获取网页信息
  try{
    xmlhttp.open("GET", url , true);
  } catch(e){ }
  //设置异步响应事件的函数,当request获得了网页源码后会调用此函数 
  xmlhttp.onreadystatechange = function() 
  {
    //判断请求返回的状态 
  　if (xmlhttp.readyState == 4) 
    {
  　　if (xmlhttp.status == 200) 
      {
        isRefreshing = false;

  　　　 //var response = xmlhttp.responseText; 
         //var response = xmlhttp.responseXml;
        
        //用stream方式返回，防止中文乱码。如果全为英文，可以直接用  xmlhttp.responseText 来返回 

	if(oStream == null)
  	  alert("您的机器不支持ADODB.Stream,请将IE浏览器的安全级别设低一些.")
	else
	{
	  isValid = true;
          oStream.Type=1;
          oStream.Mode=3;
          oStream.Open() ;
          oStream.Write(xmlhttp.responseBody);
          oStream.Position= 0;
          oStream.Type= 2;
          //设置编码方式 
          oStream.Charset="gb2312";
          //获得下载到的html源码，并传入解析函数
          var str = oStream.ReadText(); 
          parseHtml(str);
          oStream.Close();
          oStream = null;
	}
  　　} 
      else
      {
        //alert("连接服务器出错: " + xmlhttp.statusText + " (" + xmlhttp.status  + ")");
        result = "";
        isRefreshing = false;
        isValid = false;
        hint = "连接服务器出错: " + xmlhttp.statusText + " (" + xmlhttp.status  + ")";  
        AssignWeatherInfoToControls();
      }
  　}
  }
  //完成发送请求 
  xmlhttp.send(null);
}

//创建 request对象的通用函数，以下代码主要是为了兼容不同版本的ie内核做的防错处理 
function createXmlhttp() {
　try {
　　xmlhttp = new XMLHttpRequest();
　} catch (trymicrosoft) {
　　try {
　　　xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
　　} catch (othermicrosoft) {
　　　try {
　　　　xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
　　　} catch (failed) {
　　　　xmlhttp = false;
　　　}
　　}
　}

　if (!xmlhttp)
　　alert("启用XMLHttpRequest出错!");
}

function nMenu1OnClick()
{
  //刷新天气信息 
  RefreshWeatherInfo(); 
}

function widgetOnDock()
{
  LockRefresh;
  widget.pageIndex = 1;
  widgetWidth = 130;
  widgetHeight = 67; 
  UnLockRefresh;
  //将所有天气数据赋值到控件 
  AssignWeatherInfoToControls(); 
}

function widgetOnUnDock()
{
  LockRefresh;
  widget.pageIndex = 2;
  widgetWidth = 264;
  widgetHeight = 194; 
  
  UnLockRefresh; 
  
  //将所有天气数据赋值到控件 
  AssignWeatherInfoToControls(); 

}

function transTq121Code(code)
{
    var res,a;
    a =  parseInt(code);
    switch ( a ) 
    {
        case (0) : res = 31; break;//晴
        case (1) : res = 26;  break;//多云
        case (2) : res = 26; break;//阴
        case (3) : res = 9; break;//阵雨
        case (4) ://雷阵雨
        case (5) : res = 1; break;//雷阵雨并伴有冰雹
        case (6) : res = 13; break;//雨加雪
        case (7) : res = 39; break;//小雨
        case (8) : res = 10; break;//中雨
        case (9) : res = 10; break;//大雨
        case (10) : res = 40; break;//暴雨
        case (11) : res = 40; break;//大暴雨
        case (12) : res = 40; break;//特大暴雨
        case (13) : res = 13; break;//阵雪
        case (14) : res = 13; break;//小雪
        case (15) : res = 13; break;//中雪
        case (16) : res = 16; break;//大雪
        case (17) : res = 16; break;//暴雪
        case (18) : res = 17; break;//雾
        case (19) : res = 18; break;//冻雨
        case (20) : res = 19; break;//沙尘暴
        case (21) ://小雨-中雨
        case (22) ://中雨-大雨
        case (23) ://大雨-暴雨 
        case (24) ://暴雨-大暴雨 
        case (25) : res = 9; break;//大暴雨-特大暴雨
        case (26) : res = 13; break;//小雪-中雪
        case (27) : res = 13; break;//中雪-大雪
        case (28) : res = 16; break;//大雪-暴雪
        case (29) : res = 19; break;//浮尘
        case (30) : res = 23; break;//扬沙
        case (31) : res = 23; break;//强沙尘暴
        default: res = 31
   }     
        
   return res;
  
}

function trim(str)
{
  // 用正则表达式将前后空格
  // 用空字符串替代。
  return str.replace(/(^\s*)|(\s*$)/g, "");
}


function WeatherState(Code) 
{
    var theWeatherState;
    switch ( Code ) 
    {
        case (26) : case (27) : case (28) :
            theWeatherState = "cloudy";
            break;
        case (35) : case (39) : case (45) : case (46) : 
            theWeatherState = "few-showers";
            break;
        case (19) : case (20) : case (21) : case (22) :
            theWeatherState = "foggy";
            break;
        case (29) : case (30) : case (33) :
            theWeatherState = "partly-cloudy";
            break;
        case (5) : case (13) : case (14) : case (15) : case (16) : case (18) : case (25) : case (41) : case (42) : case (43) : 
            theWeatherState = "snow";
            break;
        case (1) : case (2) : case (3) : case (4) : case (37) : case (38) : case (47) : 
            theWeatherState = "thunderstorm";
            break;
        case (31) : case (32) : case (34) : case (36) : case (44) :        // Note 44- "Data Not Available"
            theWeatherState = "sun";
            break;
        case (23) : case (24) :
            theWeatherState = "windy";
            break;
        case (9) : case (10) : case (11) : case (12) : case (40) :
            theWeatherState = "Rainy";
            break;
        case (6) : case (7) : case (8) : case (17) : 
            theWeatherState = "hail";
            break;
        default:
            theWeatherState = "sun";
            break;
      }
      return theWeatherState;
}


function toChineseString(code, str)
{
/* 	var theWeatherState = str;
  switch ( code )
  {
	case (26) : case (27) : case (28) :
  	  theWeatherState = "阴";
  	  break;
  	case (35) : case (39) : case (45) : case (46) :
  	  theWeatherState = "小雨";
  	  break;
  	case (19) : case (20) : case (21) : case (22) :
  	  theWeatherState = "雾";
  	  break;
  	case (29) : case (30) : case (33) :
  	  theWeatherState = "多云";
  	  break;
  	case (5) : case (13) : case (14) : case (15) : case (16) : case (18) : case (25) : case (41) : case (42) : case (43) :
  	  theWeatherState = "雪";
  	  break;
  	case (1) : case (2) : case (3) : case (4) : case (37) : case (38) : case (47) :
  	  theWeatherState = "雷雨";
  	  break;
  	case (31) : case (32) : case (34) : case (36) : case (44) :        // Note 44- "Data Not Available"
  	  theWeatherState = "晴";
  	  break;
  	case (23) : case (24) :
  	  theWeatherState = "中到大风";
  	  break;
  	case (9) : case (10) : case (11) : case (12) : case (40) :
  	  theWeatherState = "中到大雨";
  	  break;
  	case (6) : case (7) : case (8) : case (17) :
  	  theWeatherState = "冰雹";
  	  break;
 	default:
  	  //theWeatherState = "晴";
  	  break;
  }
	return theWeatherState;
 */
//iTsmy更改获取天气文字函数
    var a;
    var theWeatherState = str;
    a =  parseInt(code);
    switch ( a ) 
    {
        case (0) : theWeatherState = "晴"; break;
        case (1) : theWeatherState = "多云";  break;
        case (2) : theWeatherState = "阴"; break;
        case (3) : theWeatherState = "阵雨"; break;
        case (4) : theWeatherState = "雷阵雨"; break;
        case (5) : theWeatherState = "雷阵雨并伴有冰雹"; break;
        case (6) : theWeatherState = "雨加雪"; break;
        case (7) : theWeatherState = "小雨"; break;
        case (8) : theWeatherState = "中雨"; break;
        case (9) : theWeatherState = "大雨"; break;
        case (10) :theWeatherState = "暴雨"; break;
        case (11) : theWeatherState = "大暴雨"; break;
        case (12) : theWeatherState = "特大暴雨"; break;
        case (13) : theWeatherState = "阵雪"; break;
        case (14) : theWeatherState = "小雪"; break;
        case (15) : theWeatherState = "中雪"; break;
        case (16) : theWeatherState = "大雪"; break;
        case (17) : theWeatherState = "暴雪"; break;
        case (18) : theWeatherState = "雾"; break;
        case (19) : theWeatherState = "冻雨"; break;
        case (20) : theWeatherState = "沙尘暴"; break;
        case (21) : theWeatherState = "小雨-中雨"; break;
        case (22) : theWeatherState = "中雨-大雨"; break;
        case (23) : theWeatherState = "大雨-暴雨"; break; 
        case (24) : theWeatherState = "暴雨-大暴雨"; break; 
        case (25) : theWeatherState = "大暴雨-特大暴雨"; break;
        case (26) : theWeatherState = "小雪-中雪"; break;
        case (27) : theWeatherState = "中雪-大雪"; break;
        case (28) : theWeatherState = "大雪-暴雪"; break;
        case (29) : theWeatherState = "浮尘"; break;
        case (30) : theWeatherState = "扬沙"; break;
        case (31) : theWeatherState = "强沙尘暴"; break;
        default: theWeatherState = "晴"
   }     
        
   return theWeatherState;
}


function nMenu2OnClick()
{
  var city = prompt("请输入城市名称", GetSetting("city") );
  if(city=='<undefined>' )
    return;
  SetSetting("city",city);
  SaveSetting('city');
  
  //刷新天气信息 
  RefreshWeatherInfo(); 
}


//刷新天气信息 
function RefreshWeatherInfo()
{
  isRefreshing = true;
  timer1.enabled = false;
  txtDockedCity.caption = '刷新...'
  txtCity.caption = '刷新...';
    
    Print( (new Date).getTime() ); 
  StartGetUrlHtmlString( url + GetSetting( 'city' ) + "&" + (new Date).getTime() );
}


function widgetOnDBClick()
{ 
  //RefreshWeatherInfo();
   nMenuSwitchnClick(); 
}

//判断是否是晚上 
function isNight() 
{
  var d = new Date();
  // Before SunRise or after Sunset means it's Night 
  return ((d.getHours()<6)||(d.getHours()>18));
}
//判断是否是下午 
function isPM() 
{
  var d = new Date();
 
  return (  (!isNight())  && (d.getHours()>12)  );
}

//背景图片前缀 
function GetBackgroundPngHead()
{ 
    var theBackground = "BLUE";
    var theDisplayMode;
    
    switch ( SkyCode ) 
    {
      default : 
      case (26) : case (27) : case (28) :
      case (35) : case (39) : case (45) : case (46) : 
      case (19) : case (20) : case (21) : case (22) :
      case (1) : case (2) : case (3) : case (4) : case (5) : case (37) : case (38) : case (47) : 
      case (9) : case (10) : case (11) : case (12) : case (40) : case (41) : case (42) : case (43) :
      case (6) : case (7) : case (8) : case (17) : case (13) : case (14) : case (15)  : case (16) : case (18) :
        theBackground = "BLUE";
        break;
      case (29) : case (30) : case (33) : case (34) :
      case (31) : case (32) : case (36) : case (44) :        
      case (23) : case (24) : case (25) : 
        theBackground = "GRAY";
        break;
    }  
    
    if (isNight()) 
    { 
      theBackground = "BLACK";  
    }
    
    
    if ( !isValid )
    { 
      theBackground = "BLUE";  
    }
    
    if(widget.pageIndex==1)   //(dockedOnSidebar)
    {
      theBackground = theBackground + 'docked';
    }
    
    return theBackground;
}

////////////////////////////////////////////////////////////////////////////////
//
// GMTTime() - returns time adjusted to GMT (Universal Time)
//
////////////////////////////////////////////////////////////////////////////////
function GMTTime() 
{ 
  var aDate = new Date();
  var aDateAdjustedToGMTInMS = aDate.getTime();// + (aDate.getTimezoneOffset() * 60 * 1000);
  return ( new Date( aDateAdjustedToGMTInMS ) );
}  

function widgetOnStart()
{
  //mhandle = setTimeout( 'moshi' , 100 );
  RefreshWeatherInfo();
}

function moshi()
{
  Print( 'moshi' );
  clearTimeout(mhandle);
  if(!dockedOnSidebar)
  {
    if(ReadIni( 'uimode' )=='mini')
      widget.pageIndex = 1
    else
      widget.pageIndex = 2;    
  }
  
}

function widgetOnInit()
{
  lastUpdateTime = -1;
  timer0.interval = 1 * 60 * 60 * 1000;  //每小时刷新
  timer0.enabled = true; 
  
}

function timer0OnTimer()
{
  RefreshWeatherInfo();
}

function MoonState() 
{ 
  return computePhaseOfMoon(new Date().getFullYear(), new Date().getMonth()+1, new Date().getDate()); 
}

function MoonState_cn() 
{ 
  return computePhaseOfMoon_cn(new Date().getFullYear(), new Date().getMonth()+1, new Date().getDate()); 
}

////////////////////////////////////////////////////////////////////////////////
//中文月相 
// computePhaseOfMoon_cn(Year, Month, Day) - Computes Phase of Moon based on Date
//
////////////////////////////////////////////////////////////////////////////////
function computePhaseOfMoon_cn(Year, Month, Day) 
{
  // Variable names used: J, K1, K2, K3, MM, P2, V, YY
  var P2 = 3.14159 * 2;    
  var YY = Year - parseInt((12 - Month)/10);
  var MM = Month + 9;
  if (MM >= 12) { MM = MM-12; }
  var K1 = parseInt(365.25 * (YY+4712));
  var K2 = parseInt(30.6 * MM + .5);
  var K3 = parseInt(parseInt((YY/100) + 49) * .75) - 38;
  // J is the Julian date at 12h UT on day in question
  var J = K1+K2+Day+59;             
  // Adjust for Gregorian calendar, if applicable   
  if (J > 2299160) { J = J-K3; }    
  // Calculate illumination (synodic) phase
  var V = (J - 2451550.1)/29.530588853;
  V = V - parseInt(V);
  // Normalize values to range from 0 to 1
  if (V<0) { V=V+1; }
  // Moon's age in days from New Moon
  var AG = V*29.53;    

  switch (true) 
  { 
    // Each phase lasts approximately 3.28 days
    case ((AG > 27.6849270496875) || (AG <= 1.8456618033125)) :
      var retVal = '朔月';
      break;
    case ((AG > 1.8456618033125) && (AG <= 5.5369854099375)) :
      var retVal = '眉月';
      break;
    case ((AG > 5.5369854099375) && (AG <= 9.2283090165625)) :
      var retVal = '上弦月';
      break;
    case ((AG > 9.2283090165625) && (AG <= 12.9196326231875)) : 
      var retVal = '盈凸月';
      break;
    case ((AG > 12.9196326231875) && (AG <= 16.6109562298125)) :
      var retVal = '满月';
      break;
    case ((AG > 16.6109562298125) && (AG <= 20.3022798364375)) :
      var retVal = '盈凸月';
      break;
    case ((AG > 20.3022798364375) && (AG <= 23.9936034430625)) :
      var retVal = '下弦月';
      break;
    case ((AG > 23.9936034430625) && (AG <= 27.6849270496875)) :
      var retVal = '残月';
      break;
    default : 
      var retVal = '满月';
      break;
  }    
  return retVal;
}


////////////////////////////////////////////////////////////////////////////////
//
// computePhaseOfMoon(Year, Month, Day) - Computes Phase of Moon based on Date
//
////////////////////////////////////////////////////////////////////////////////
function computePhaseOfMoon(Year, Month, Day) 
{
  // Variable names used: J, K1, K2, K3, MM, P2, V, YY
  var P2 = 3.14159 * 2;    
  var YY = Year - parseInt((12 - Month)/10);
  var MM = Month + 9;
  if (MM >= 12) { MM = MM-12; }
  var K1 = parseInt(365.25 * (YY+4712));
  var K2 = parseInt(30.6 * MM + .5);
  var K3 = parseInt(parseInt((YY/100) + 49) * .75) - 38;
  // J is the Julian date at 12h UT on day in question
  var J = K1+K2+Day+59;             
  // Adjust for Gregorian calendar, if applicable   
  if (J > 2299160) { J = J-K3; }    
  // Calculate illumination (synodic) phase
  var V = (J - 2451550.1)/29.530588853;
  V = V - parseInt(V);
  // Normalize values to range from 0 to 1
  if (V<0) { V=V+1; }
  // Moon's age in days from New Moon
  var AG = V*29.53;    

  switch (true) 
  { 
    // Each phase lasts approximately 3.28 days
    case ((AG > 27.6849270496875) || (AG <= 1.8456618033125)) :
      var retVal = 'New';
      break;
    case ((AG > 1.8456618033125) && (AG <= 5.5369854099375)) :
      var retVal = 'Waxing-Crescent';
      break;
    case ((AG > 5.5369854099375) && (AG <= 9.2283090165625)) :
      var retVal = 'First-Quarter';
      break;
    case ((AG > 9.2283090165625) && (AG <= 12.9196326231875)) : 
      var retVal = 'Waxing-Gibbous';
      break;
    case ((AG > 12.9196326231875) && (AG <= 16.6109562298125)) :
      var retVal = 'Full';
      break;
    case ((AG > 16.6109562298125) && (AG <= 20.3022798364375)) :
      var retVal = 'Waning-Gibbous';
      break;
    case ((AG > 20.3022798364375) && (AG <= 23.9936034430625)) :
      var retVal = 'Last-Quarter';
      break;
    case ((AG > 23.9936034430625) && (AG <= 27.6849270496875)) :
      var retVal = 'Waning-Crescent';
      break;
    default : 
      var retVal = 'Full';
      break;
  }    
  return retVal;
}

////////////////////////////////////////////////////////////////////////////////
//
// makesSenseToDisplayTheMoon - boolean indicating whether it makes sense to 
//                              display the moon.  Dependant on weather state
//
////////////////////////////////////////////////////////////////////////////////
function makesSenseToDisplayTheMoon() 
{
  var retVal = false;
  if ( isNight() ) 
  { 
    var theWeatherState = WeatherState();
    if ( ( theWeatherState=="sun" ) || ( theWeatherState=="partly-cloudy" )  ) 
    {
      retVal = true;
    }
  }
  return retVal;
}





function OnAllSettingsSaved()
{
  SaveSetting('city');
  
  //刷新天气信息 
  RefreshWeatherInfo();   
}

function timer1OnTimer()
{
  var nowtm;
  if(isPM()) nowtm=2
  else if(isNight()) nowtm=3
  else nowtm=1;  
  
  
  if(nowtm!=lastUpdateTime) 
  {
     lastUpdateTime = nowtm;
     RefreshWeatherInfo();
  }
}

function nMenuSwitchnClick()
{
  //先锁定刷新，方式闪动 
  LockRefresh;
  
  if(widget.pageIndex == 1)
  {
    //如果已经停靠在边栏了，就不需要再放大了 
    if(dockedOnSidebar)
      return;
      
    widget.pageIndex = 2;
    WriteIni('uimode' ,'full' ); 
    //sidebarOff();
     
    //WriteIni( 'myState' , 'Normal' );
  }
  else
  {
    widget.pageIndex = 1;
    WriteIni('uimode' ,'mini' ); 
    
    //sidebarOn();
     
    //WriteIni( 'myState' , 'mini' );
  }
  
  UnLockRefresh;

  AssignWeatherInfoToControls()
  Refresh;  
  
  if(( widgetWidth+widget.Left>DesktopWidth)) // || ( (!dockedOnSidebar)&&(widget.pageIndex = 1)  ) )
    timehandle = setTimeout( 'duiqi' , 100 );
}

function duiqi()
{
  clearTimeout(timehandle);
  widget.Left = DesktopWidth - widgetWidth;
     
}


function sidebarOn()
{
    //停靠状态下
    var bgNameHead = GetBackgroundPngHead(); 
    dockedBg.Src = 'images\/' + GetBackgroundPngHead() + '-base.png';
    dockhl1.Src = 'images\/' + bgNameHead + '-highlight-02.png'; 
    dockhl2.Src = 'images\/' + bgNameHead + '-highlight-01.png'; 

    if ( makesSenseToDisplayTheMoon() ) 
    {
      dockedIcon.Src =  'images\/icons\/docked_moon-' + MoonState() + '.png';
    }
    else
    {
      if(!isPM())
        dockedIcon.Src =  'images\/icons\/docked_'+ WeatherState( weatherIcon1AM )  + '.png'
      else
        dockedIcon.Src =  'images\/icons\/docked_'+ WeatherState( weatherIcon1PM ) + '.png';
    } 

    if(isValid)
    { 
      txtDockedCity.caption = GetSetting( 'city' )
      txtDockedTmp.caption = tmp1Low + '~' + tmp1High + '°';
      txtDockedCity.tooltip = hint;
    }
    else
    {
      txtDockedCity.caption = GetSetting( 'city' ) + "!";
      txtDockedTmp.caption = "";
      txtDockedCity.tooltip = "链接网络失败";
    }
    
//    txtDay2.tooltip = hint2;
//    txtDay3.tooltip = hint3;
//    txtDay4.tooltip = hint4;
   
   }
   
function sidebarOff()
{
    //未停靠状态下
    var bgNameHead = GetBackgroundPngHead();
    bg.Src = 'images\/' + bgNameHead + '-base.png';
    hl1.Src = 'images\/' + bgNameHead + '-highlight-01.png'; 
    hl2.Src = 'images\/' + bgNameHead + '-highlight-01.png'; 

    txtWeatherInfo.caption = weatherText1AM;
    if ( makesSenseToDisplayTheMoon() ) 
    {
      bigIcon.Src =  'images\/icons\/undocked_moon-' + MoonState() + '.png';
      txtCity.tooltip = MoonState_cn();
    }
    else
    {
      if(!isPM())
        bigIcon.Src =  'images\/icons\/undocked_'+ WeatherState( weatherIcon1AM ) + '.png'
      else
        bigIcon.Src =  'images\/icons\/undocked_'+ WeatherState( weatherIcon1PM )  + '.png';
    } 
    if(isPM())
    {
      iconDay2.Src =  'images\/small\/'+ weatherIcon2AM  + '.png';
      iconDay3.Src =  'images\/small\/'+ weatherIcon3AM  + '.png';
      iconDay4.Src =  'images\/small\/'+ weatherIcon4AM  + '.png';
    }
    else
    {
      iconDay2.Src =  'images\/small\/'+ weatherIcon2PM  + '.png';
      iconDay3.Src =  'images\/small\/'+ weatherIcon3PM  + '.png';
      iconDay4.Src =  'images\/small\/'+ weatherIcon4PM  + '.png';
    }
    txtBigTmp.caption = parseInt(( parseInt(tmp1Low) + parseInt(tmp1High) )/2) + '°'; 
    txtTodayTmp.caption = tmp1Low + '  -  ' + tmp1High + '°';
    txtCity.caption = GetSetting( 'city' );
    txtDay2.caption = DayDate2;
    txtDay3.caption = DayDate3;
    txtDay4.caption = DayDate4;

    txtTmpH2.caption =  tmp2High + '°';
    txtTmpH3.caption =  tmp3High + '°';
    txtTmpH4.caption =  tmp4High + '°';

    txtTmpL2.caption =  tmp2Low + '°';
    txtTmpL3.caption =  tmp3Low + '°';
    txtTmpL4.caption =  tmp4Low + '°';

    txtCity.tooltip = hint;  
    txtDay2.tooltip = hint2;
    txtDay3.tooltip = hint3;
    txtDay4.tooltip = hint4;
    

}

function copyrightOnClick()
{
  OpenLink( url + GetSetting( 'city' ) );
}

function widgetOnPopupMenu()
{
  if(dockedOnSidebar)
  {
    nMenu4.Visible = false;
  
  }
  else
  {
    nMenu4.Visible = true;
    if(widget.pageIndex==1)
      nMenu4.caption = '切换到详细模式'
    else
      nMenu4.caption = '切换到迷你模式';
  
  }
}

function widgetOnClose()
{
  if(widget.pageIndex == 1)
  {
    WriteIni('uimode' ,'full' ); 
  }
  else
  {
    WriteIni('uimode' ,'mini' ); 
  }
  
}

function btRefresh2OnClick()
{
  RefreshWeatherInfo();   
}