var bConfiged = true; 
var oldcount; 
var server,username,pwd;


function widgetOnInit()
{
  oldcount = parseInt( ReadIni("oldcount") ) ;
  timer0.interval = 1000 * 60 * parseInt(GetSetting( "intev" ) );
  timer0.enabled = true;
  timer0OnTimer();
}

function timer0OnTimer()
{
  server = GetSetting( 'sev' );
  username = GetSetting( 'user' );
  pwd = GetSetting( 'pwd' );
  if( (server=="")||(username=="")||(pwd==""))
  {
    countTxt.caption = "配置";
    countTxt.tooltip = "点击这里配置pop邮箱帐号";
    countTxt.cursorHandPoint = true;
    bConfiged = false;
    return;
  }
  else
  {
    countTxt.tooltip = "";
    bConfiged = true;
    countTxt.cursorHandPoint = false;
  }
  countTxt.caption = "...";
  countTxt.tooltip = "正在检测帐号"+username;
  startCheckPop3Email( server , username , pwd , "EmailCount" );
  
}



function EmailCount(count)
{
  if(count<0) 
  {
    countTxt.caption = "--";
    countTxt.tooltip = "未检测到邮件";
    return;
  }  
  countTxt.caption = count ;
  countTxt.tooltip = "帐号["+ username + "]共有" + count +"封邮件";
  
  if((oldcount!=0)&&(count>oldcount)) 
  {
    countTxt.tooltip = countTxt.tooltip +"\n收到 "+ (count-oldcount) + " 封新邮件";
    showTrayHint("邮件检测工具" ,"收到 "+ (count-oldcount) +" 封新邮件("+"帐号"+username+")\n双击邮件检测小工具打开指定收信软件收信"  )
    var soundfile = GetSetting("alarm");

    if(soundfile!="no")
    {
      PlaySoundsync( "sound\\" + soundfile );
    
    }
    
  }
  oldcount = count;
  WriteIni("oldcount",oldcount);
}

function nMenu1OnClick()
{
   timer0OnTimer(); 
}

function OnAllSettingsSaved()
{
  var pserver = GetSetting( 'sev' );
  var pusername = GetSetting( 'user' );
  
  if( (pserver!=server)||(pusername!=username)  )
  {
    oldcount = 0;
    WriteIni("oldcount",oldcount);
    
  }	
	
  widgetOnInit();
}

function nMenu2OnClick()
{
  ShowSetting;
}

function widgetOnDBClick()
{
  OpenFile( GetSetting( "client" ) );
}

function countTxtOnClick()
{
  if(!bConfiged) 
    ShowSetting;
}

function widgetOnDock()
{
/* 
  LockRefresh;
  
  bg.Src = "default.png";
  widgetWidth = bg.Width;
  widgetHeight = bg.Height;
  countTxt.Left = 20;
  countTxt.Top = 61;
  countTxt.Width = 90;
  countTxt.Height = 16;
  countTxt.fontSize = 12;
  
  UnLockRefresh;
 */ 
   
}

function widgetOnUnDock()
{
/*
  LockRefresh;

  bg.Src = "mail2.png";
  widgetWidth = bg.Width;
  widgetHeight = bg.Height;
  countTxt.Left = 40;
  countTxt.Top = 92;
  countTxt.Width = 125;
  countTxt.Height = 20;
  countTxt.fontSize = 18;
    
  UnLockRefresh;
  */
}