var imgBackground,
    imgHighlight,
    imgColum, 
    imgTimeField1,
    imgTimeField2,
    imgTimeField3,
    imgTimeField4,
    imgAP,
    imgWeek,
    intTimeZoneDiff,
    int24, 
    lasthours,
    lastmins,
    lastday,
    wmp;

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;
  
  //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 DrawTime()
{

  //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=='00')
   {
    if (nmins=='59') iniPlayer();
    if (nmins=='00') wmp.controls.play();
    if (nmins=='01') wmp = null;
   }

   if(( lastday == nday)&&
      ( lastmins == nmins)&&
      ( lasthours == nhours) 
     )
   {
     //如果时间没有变，则不需要刷新,减少cpu占用 
     return; 
   }
   
   lastday = nday;
   lastmins = nmins;
   lasthours = nhours;    
   
   Print('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;
   var intLabel = GetSetting('optionLabel');
   if (intLabel==1)
   {
      var strCity = GetSetting('optionCity');
      textCity.caption = strCity;
      textCity.Visible = true;
   }else textCity.Visible = false;
}

function timer0OnTimer()
{
   BlinkColum();
}

function timer1OnTimer()
{
   DrawTime();
}

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

function iniPlayer()
{
   if (GetSetting('optionAlerm')==1)
   {
      Print('initialize player');
      try
      {
       wmp = new ActiveXObject('WMPlayer.OCX');
      }
      catch (e)
      {
       return;
      }
      wmp.settings.autoStart = false;
      wmp.settings.setMode('loop',false);
      wmp.URL = GetSetting('optionWave');
   }
}

function widgetOnDBClick()
{

  var VoiceObj = new ActiveXObject("Sapi.SpVoice");
  VoiceObj.Speak('oh ,it is great!', 1 );
  VoiceObj = "";

return; 
      try
      {
       wmp = new ActiveXObject('WMPlayer.OCX');
      }
      catch (e)
      {
       return;
      }
      wmp.settings.autoStart = false;
      wmp.settings.setMode('loop',false);
      Print( GetSetting('optionWave'));
      wmp.URL =  GetSetting('optionWave');
      wmp.controls.play();
// sleep(5000); 
// wmp = null;  
}