var cputype = 0;

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

function widgetOnInit()
{
  LockRefresh;
  SaveAllOption;
  //CPU1 
  chart1.style = GetSetting('style');
  chart1.fillColor = GetSetting('fillcl');
  chart1.penColor = GetSetting('linecl');
  chart1.gradientColor = GetSetting('gradcl');

  //CPU2
  chart2.style = GetSetting('style');
  chart2.fillColor = GetSetting('fillcl');
  chart2.penColor = GetSetting('linecl');
  chart2.gradientColor = GetSetting('gradcl');

  //RAM
  chart3.style = GetSetting('style');
  chart3.fillColor = GetSetting('fillcl');
  chart3.penColor = GetSetting('linecl');
  chart3.gradientColor = GetSetting('gradcl');  

  cputype = parseInt( GetSetting('cpu') ); 
  if( cputype==0 )
  {
    //单cpu
    chart1.Width = 77;
    chart2.Visible = false; 
  }
  else
  {
    //双核cpu
    chart1.Width = 36;
    chart2.Visible = true;     
  }
  
  UnLockRefresh;

  tm.interval = parseInt( GetSetting('tm')  );
  tm.enabled = true;   
}

function tmOnTimer()
{
  LockRefresh;
  
  if( cputype==0 )
  {
    //单cpu
    var c = cpuUsage;
    chart1.addItem( c );
    txt.caption = 'CPU: ' + c + '%';
  }
  else
  {
    //双核cpu
    cpuLoadInfo;
    var c1 = cpuUsageEx(0);
    var c2 = cpuUsageEx(1);
    chart1.addItem( c1 );
    chart2.addItem( c2 );
    txt.caption = 'CPU: ' + ((c1+c2)/2).toFixed(0) + '%'; 
  }
  //RAM
  var r1 = ramPhysPercent;
  chart3.addItem( r1 );
  txt1.caption = 'RAM\n\n\r' + r1 + '%';
  txt1.tooltip = '总计 ' + Math.round( ramTotalPhys/1048576 ) + 'MB\n\r'+
                 '已用 ' + Math.round( (ramTotalPhys - ramAvailPhys)/1048576 ) + 'MB\n\r'+
                 '剩余 ' + Math.round( ramAvailPhys/1048576 ) + 'MB'
  //net
  getNetSpeed;
  var i=tm.interval/1000; 
  var nup = netUpSpeed/1024/i; 
  var ndown = netDownSpeed/1024/i;
  text0.caption=ndown.toFixed(1)+"k/s";
  text1.caption=nup.toFixed(1)+"k/s"; 
  UnLockRefresh;  
}

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