var cpu,mem,Angle,memAngle,tp,skin;
var angleOffset = -125 ;
var intRotateStep;
var firstrun = true;

function widgetOnDock()
{
  LockRefresh;
  WidgetWidth=132;
  WidgetHeight=103;
  imgbg.Src=WidgetPath+"images\\"+ skin +"\\back.png";

  imgcpu.src=WidgetPath+"images\\"+ skin +"\\dial.png";
  imgcpu.left=38;  
  imgcpu.top=22;

  imgmem.src=WidgetPath+"images\\"+ skin +"\\dial_sml.png";
  imgmem.left=91;  
  imgmem.top=11;

  imgcpu.angleX = imgcpu.width/2;
  imgcpu.angleY = imgcpu.height/2;
  imgmem.angleX = imgmem.width/2;
  imgmem.angleY = imgmem.height/2;

  imgdot.src=WidgetPath+"images\\"+ skin +"\\dialdot.png";
  imgglass.src=WidgetPath+"images\\"+ skin +"\\glass.png";


  lblcpu.left=25;  
  lblcpu.top=79;
  lblcpu.Width = 35;
  
  lblmem.left=79;  
  lblmem.top=45;
  lblmem.Width = 35;
  
  lblcpu.fontsize=10;
  
  
  lblmem.fontsize=lblcpu.fontsize;
  UnLockRefresh;    
}

function widgetOnUnDock()
{
  LockRefresh;
  WidgetWidth=202;
  WidgetHeight=159;
  imgbg.src=WidgetPath+"images\\"+ skin +"\\back_lrg.png";
  imgcpu.src=WidgetPath+"images\\"+ skin +"\\dial_lrg.png";
  imgcpu.left=64;  
  imgcpu.top=32;
//  imgcpu.width=98; 
//  imgcpu.height=98;
  imgmem.src=WidgetPath+"images\\"+ skin +"\\dial_lrg_sml.png";
  imgmem.left=138;  
  imgmem.top=15;

  imgcpu.angleX = imgcpu.width/2;
  imgcpu.angleY = imgcpu.height/2;
  imgmem.angleX = imgmem.width/2;
  imgmem.angleY = imgmem.height/2;
  
  imgdot.src=WidgetPath+"images\\"+ skin +"\\dialdot_lrg.png";
  imgglass.src=WidgetPath+"images\\"+ skin +"\\glass_lrg.png";
  
  lblcpu.left=49;  
  lblcpu.top=108;
  lblcpu.Width = 40;
  
  lblmem.left=123;  
  lblmem.top=66;
  lblmem.Width = 42;
  
  lblcpu.fontsize=12;
  lblmem.fontsize=lblcpu.fontsize;
  UnLockRefresh;
}

function tmOnTimer()
{
  cpu  = cpuUsage
  if(firstrun)
  {
    firstrun = false;
    cpu = 30;
  }
  var s = cpu +"%";

  if(s!=lblcpu.caption)
  {
    lblcpu.caption = s;        
    var i = Math.round(cpu*2.5) + angleOffset;
    //imgcpu.angle = i;
    
    tmcpu.Enabled = false;
    if((i>=-360)&&(i<=360))
      Angle = i
    else
      Angle = 0;
    tmcpu.Enabled = true;    
 }

  if(tp=="ramv")
    mem  = memVirPercent
  else if(tp=="rama")
    mem  = memPageFilePercent;
  else
    mem  = memPhysPercent
  
  var s = mem +"%";

  if(s!=lblmem.caption)
  {
    lblmem.caption = s;        
    var i = Math.round(mem*2.5) + angleOffset;
    //imgmem.angle = i;

    tmmem.Enabled = false;
    memAngle = i;
    tmmem.Enabled = true;    
    
 }

 
 
  
}

function tmcpuOnTimer()
{
  var i;
  if(imgcpu.angle>Angle)
  {
    intRotateStep = Math.max(1, Math.round( Math.abs( Math.abs(Angle) - Math.abs(imgcpu.angle) )/10) ); 
    i = imgcpu.angle - intRotateStep;
    if(i<=Angle) 
    {
      tmcpu.Enabled  = false;
      imgcpu.angle = Angle;
    }
    else
      imgcpu.angle = i;
  }
  else
  { 
    intRotateStep = Math.max(1, Math.round( Math.abs( Math.abs(Angle) - Math.abs(imgcpu.angle) )/10) ); 
    i = imgcpu.angle + intRotateStep;
    if(i>=Angle) 
    {
      tmcpu.Enabled = false;
      imgcpu.angle = Angle;
    }
    else
    {
      if((i>=-360)&&(i<=360))
        imgcpu.angle  = i;
    }
  }  
}

function tmmemOnTimer()
{
  var i;
  if(imgmem.angle>memAngle)
  {
    intRotateStep = Math.max(1, Math.round( Math.abs( Math.abs(memAngle) - Math.abs(imgmem.angle) )/10) ); 
    i = imgmem.angle - intRotateStep;  
    if(i<=memAngle) 
    {
      tmmem.Enabled  = false;
      imgmem.angle = memAngle;
    }
    else
    { 
      if((i>=-360)&&(i<=360))
        imgmem.angle = i;
    }
  }
  else
  {
    intRotateStep = Math.max(1, Math.round( Math.abs( Math.abs(memAngle) - Math.abs(imgmem.angle) )/10) ); 
    i =  imgmem.angle +  intRotateStep;
    
    if(i>=memAngle) 
    {
      tmmem.Enabled = false;
      imgmem.angle = memAngle;
    }
    else
    {
      if((i>=-360)&&(i<=360))
        imgmem.angle  = i;
    }
  }  

  
  
}

function widgetOnStart()
{
  tm.enabled = true;
  tmOnTimer();
}

function widgetOnInit()
{
  tp = GetSetting("type");
  skin = GetSetting("skin");
  
}

function OnAllSettingsSaved()
{
  widgetOnInit();
  if(dockedOnSidebar)
    widgetOnDock()
  else
    widgetOnUnDock();
    
  
  tmOnTimer();
}

function widgetOnDBClick()
{
  Run("taskmgr");
}

function Run(objPath) 
{
    try 
    {
        var objShell = new ActiveXObject('wscript.shell');
        objShell.Run(objPath);
        objShell = null;
    }
    catch (e)
    {
    alert('找不到指定文件'+objPath+'(或它的组件之一)，请确定文件或路径是否正确，并确定更改了你的IE安全级别：工具--Internet 选项 -- 安全 -- 自定义级别 -- 没有标记为安全的 ActiveX 控件进行初始化和脚本运行\n设置为“启用”或 “提示”');
    }
}