var imgBackground,
    imgHighlight,
    imgColum, 
    imgTimeField1,
    imgTimeField2,
    imgTimeField3,
    imgTimeField4,
    imgAP,
    imgWeek,
    intTimeZoneDiff,
    int24, 
    lasthours,
    lastmins,
    lastday;
var vhour,vhalfhour;
var myDate = new Date();

function widgetOnInit()
{  
  //先锁定刷新，加快程序执行速度，防止闪烁
  LockRefresh;
  
  //initialize all images
  imgBackground = bg;

  imgHighlight = Image.Create(widget);
  imgHighlight.Src = 'images/highlight.png';
  imgHighlight.Top = imgBackground.Top+3;
  imgHighlight.Left = imgBackground.Left+4;
  
  imgColum = Image.Create(widget);
  imgColum.Src = 'images/c.png';
  imgColum.Top = imgBackground.Top+13;
  imgColum.Left = imgBackground.Left+53;
  
  imgTimeField1 = Image.Create(widget);
  imgTimeField1.Top = imgBackground.Top+13;
  imgTimeField1.Left = imgBackground.Left+13;
  imgTimeField1.Visible = false;
  
  imgTimeField2 = Image.Create(widget);
  imgTimeField2.Top = imgBackground.Top+13;
  imgTimeField2.Left = imgBackground.Left+33;
  imgTimeField2.Visible = false;
  
  imgTimeField3 = Image.Create(widget);
  imgTimeField3.Top = imgBackground.Top+13;
  imgTimeField3.Left = imgBackground.Left+63;
  imgTimeField3.Visible = false;
  
  imgTimeField4 = Image.Create(widget);
  imgTimeField4.Top = imgBackground.Top+13;
  imgTimeField4.Left = imgBackground.Left+83; 
  imgTimeField4.Visible = false;
  
  imgAP = Image.Create(widget);
  imgAP.Top = imgBackground.Top+13;
  imgAP.Left = imgBackground.Left+100;
  imgAP.Visible = false;
  
  imgWeek = Image.Create(widget);
  imgWeek.Top = imgBackground.Top+41;
  imgWeek.Left = imgBackground.Left+18;
  imgWeek.Visible = false;
  
  //enable Timer 
  timer1.enabled = true;
  timer0.enabled = true;
 
   //语音报时设置
  vhour = GetSetting("valhour");  
  vhalfhour = GetSetting("valhalfhour");  
  
  //draw the time!
  DrawTime(); 

  //先是显示闪动：号 
  imgColum.Visible=true;

  //解除刷新，加快程序执行速度，防止闪烁
  UnLockRefresh;
  
  //刷新
  refresh; 
  
  //根据配置判断是否要闪动：号 
  if(GetSetting('isFlashDot')==0) 
     timer0.enabled = false
  else
    timer0.enabled = true;
}

//Blink the two dots :)
function BlinkColum()
{
   //hide if it's there, else show it
   if (imgColum.Visible) imgColum.Visible=false;
   else imgColum.Visible=true;
}

function GetDate()
{
   var myDate2 = new Date();
   var iMonth = myDate2.getMonth() + 1;
   text_Date.caption =iMonth + '月' + myDate2.getDate() + '日' +' ' +
                     cnMonth(myDate2.getYear(),iMonth ,myDate2.getDate() ) + 
                     cnDay(myDate2.getYear(),iMonth ,myDate2.getDate() ) ;
}
////////////////////////////////////////////////////////////////////////////
function DrawTime()
{
   GetDate();
   
   //get today's date
   var thetime=new Date();
   //get hour
   var nhours=thetime.getHours();
   //get minutes
   var nmins=thetime.getMinutes();
   //get seconds
   var nsecn=thetime.getSeconds();
   //get week
   var nday=thetime.getDay();
    
//这个地方修改报时设置 
    if (nsecn=="0")
      {  
       if ((vhalfhour==1)&&(nmins=='30')) playVoiceTime;
       if ((vhour==1)&&(nmins=='00')) playVoiceTime;
      }

//这个地方用来修改语音报时  

   if(( lastday == nday)&&
      ( lastmins == nmins)&&
      ( lasthours == nhours) 
     )
   {  
     //如果时间没有变，则不需要刷新,减少cpu占用 
    return; 
   }
 
   lastday = nday;
   lastmins = nmins;
   lasthours = nhours;    
  //Refresh Timer=========================================================================== 
  
   //get month
   //var nmonth=thetime.getMonth();
   //get date
   //var ntoday=thetime.getDate();
   //get year
   //var nyear=thetime.getYear();
   //var timezone = thetime.getTimezoneOffset()/60;
   int24 = GetSetting('optionTimeFormat');
   intTimeZoneDiff = GetSetting('optionTimeZone');
   nhours += parseInt(intTimeZoneDiff);
 
   if (int24==0)
   {
     //12 hours  ===============================================================   
     if (nhours>=12)
     {
         //it's afternoon
         imgAP.Src = 'images/pm.png';
     }
     else
     {
         //it's morning
         imgAP.Src = 'images/am.png';
     }
     imgAP.Visible = true;
 
     //reset hours 
     if (nhours>=13)
        nhours-=12;
     if (nhours==0)
        nhours=12;
     if (nhours>=10) 
     {
        imgTimeField1.Src = 'images/1.png';
        imgTimeField1.Visible = true;
        nhours -= 10;
        imgTimeField2.Src = 'images/'+nhours+'.png';
        imgTimeField2.Visible = true;
     }
     else 
     {     
        imgTimeField2.Src = 'images/'+nhours+'.png';
        imgTimeField2.Visible = true;
        //hide field one , we don't need it in 12 hours
        imgTimeField1.Visible = false;
     }
     
   }else
   {     
    //24 hours =================================================================
     if (nhours>=24)
     {
        nhours -= 24;
        nday++;
      }
     imgAP.Visible = false;
     if (nhours>=10) 
     {    
        var intRemainder = nhours%10;
        nhours = (nhours-intRemainder)/10;
        imgTimeField1.Src = 'images/'+nhours+'.png';
        nhours -= 10;
        imgTimeField2.Src = 'images/'+intRemainder+'.png';
     }
     else 
     {
        imgTimeField1.Src = 'images/0.png';
        imgTimeField2.Src = 'images/'+nhours+'.png';
     }
     imgTimeField1.Visible = true;
     imgTimeField2.Visible = true;
   }
   
   //draw the minutes ==========================================================
  
   if (nmins<10) 
   {
      imgTimeField3.Src = 'images/0.png';
      imgTimeField4.Src = 'images/'+nmins+'.png';
   }else
   {
      var intRemainder = nmins%10;
      imgTimeField4.Src = 'images/'+intRemainder+'.png';
      nmins -= intRemainder;
      nmins /= 10;
      imgTimeField3.Src = 'images/'+nmins+'.png';
   }
   imgTimeField3.Visible = true;
   imgTimeField4.Visible = true;
   //draw the week   ===========================================================
   if (nday>6) nday=0;
   if (nday<0) nday=6;
   imgWeek.Src = 'images/week/'+nday+'.png';
   imgWeek.Visible = true;

}

////////////////////////////////////////////////////////////////////////////


function DrawTime2()
{
   GetDate();
   
   //get today's date
   var thetime=new Date();
   //get hour
   var nhours=thetime.getHours();
   //get minutes
   var nmins=thetime.getMinutes();
   //get seconds
   var nsecn=thetime.getSeconds();
   //get week
   var nday=thetime.getDay();
    
//这个地方修改报时设置 
    if (nsecn=="0")
{
      {  
       if ((vhalfhour==1)&&(nmins=='30')) playVoiceTime;
       if ((vhour==1)&&(nmins=='00')) playVoiceTime;
      }

//这个地方用来修改语音报时  

   if(( lastday == nday)&&
      ( lastmins == nmins)&&
      ( lasthours == nhours) 
     )
   {  
     //如果时间没有变，则不需要刷新,减少cpu占用 
    return; 
   }
 
   lastday = nday;
   lastmins = nmins;
   lasthours = nhours;    
  //Refresh Timer=========================================================================== 
  
   //get month
   //var nmonth=thetime.getMonth();
   //get date
   //var ntoday=thetime.getDate();
   //get year
   //var nyear=thetime.getYear();
   //var timezone = thetime.getTimezoneOffset()/60;
   int24 = GetSetting('optionTimeFormat');
   intTimeZoneDiff = GetSetting('optionTimeZone');
   nhours += parseInt(intTimeZoneDiff);
 
   if (int24==0)
   {
     //12 hours  ===============================================================   
     if (nhours>=12)
     {
         //it's afternoon
         imgAP.Src = 'images/pm.png';
     }
     else
     {
         //it's morning
         imgAP.Src = 'images/am.png';
     }
     imgAP.Visible = true;
 
     //reset hours 
     if (nhours>=13)
        nhours-=12;
     if (nhours==0)
        nhours=12;
     if (nhours>=10) 
     {
        imgTimeField1.Src = 'images/1.png';
        imgTimeField1.Visible = false;
        imgTimeField1.Visible = true;
        nhours -= 10;
        imgTimeField2.Src = 'images/'+nhours+'.png';
        imgTimeField2.Visible = false;
        imgTimeField2.Visible = true;
     }
     else 
     {     
        imgTimeField2.Src = 'images/'+nhours+'.png';
        imgTimeField2.Visible = false;
        imgTimeField2.Visible = true;
        //hide field one , we don't need it in 12 hours
        imgTimeField1.Visible = false;
     }
     
   }else
   {     
    //24 hours =================================================================
     if (nhours>=24)
     {
        nhours -= 24;
        nday++;
      }
     imgAP.Visible = false;
     if (nhours>=10) 
     {    
        var intRemainder = nhours%10;
        nhours = (nhours-intRemainder)/10;
        imgTimeField1.Src = 'images/'+nhours+'.png';
        nhours -= 10;
        imgTimeField2.Src = 'images/'+intRemainder+'.png';
     }
     else 
     {
        imgTimeField1.Src = 'images/0.png';
        imgTimeField2.Src = 'images/'+nhours+'.png';
     }
     imgTimeField1.Visible = false;
     imgTimeField2.Visible = false;
     imgTimeField1.Visible = true;
     imgTimeField2.Visible = true;
   }
   
   //draw the minutes ==========================================================
  
   if (nmins<10) 
   {
      imgTimeField3.Src = 'images/0.png';
      imgTimeField4.Src = 'images/'+nmins+'.png';
   }else
   {
      var intRemainder = nmins%10;
      imgTimeField4.Src = 'images/'+intRemainder+'.png';
      nmins -= intRemainder;
      nmins /= 10;
      imgTimeField3.Src = 'images/'+nmins+'.png';
   }
   imgTimeField3.Visible = false;
   imgTimeField4.Visible = false; 
   imgTimeField3.Visible = true;
   imgTimeField4.Visible = true;
   //draw the week   ===========================================================
   if (nday>6) nday=0;
   if (nday<0) nday=6;
   imgWeek.Src = 'images/week/'+nday+'.png';
   imgWeek.Visible = false;
   imgWeek.Visible = true;

}
 }
////////////////////////////////////////////////////////////////////////////


function timer0OnTimer()
{
   BlinkColum();
}

function timer1OnTimer()
{
  DrawTime2();       
}

function OnAllSettingsSaved()
{
  //根据配置判断是否要闪动：号 
  if(GetSetting('isFlashDot')==0) 
     timer0.enabled = false
  else
    timer0.enabled = true;
    
   //先是显示闪动：号 
  imgColum.Visible=true;
  lastmins = -1; 
  DrawTime();
}


function widgetOnDBClick()
{
   Run("RunDLL32.exe Shell32.dll,Control_RunDLL TimeDate.cpl");
}

function nMenu1OnClick()
{
  playVoiceTime;
}

function nMenu2OnClick()
{
   Run("RunDLL32.exe Shell32.dll,Control_RunDLL TimeDate.cpl");
}