var fso;
var drives, background , bgs , iBars , iDris , dLabels, dInfos ,iNotReads;
var nofso=false;
                               
function convertBytes(b)
{
  var i = 0, u = Array(' MB', ' GB', ' TB');

  while (b >= 1024 && (b / 1024) >= 1)
  {
    b /= 1024;
    i++;
  }
  return Math.round(b * 100)/100;
}

function widgetOnInit()
{
  try 
  {
    fso = new ActiveXObject("Scripting.FileSystemObject");
　} 
  catch (failed) 
  {
　　//Alert("系统不支持FileSystemObject，请降低ie的脚本安全级别。");
    nofso = true;
　}


  initDrives();
  	
  
}

function widgetOnStart()
{

}

//更新磁盘信息 
function initDrives()
{
  timer0.enabled = false; 
  timer0.interval = GetSetting('refreshSec') * 1000; 

  //从配置中载入要显示的磁盘的名称，如 C 
  var d = GetSetting("driverName");
  if (d.length == 0) d = 'C';

  drives = new Array(d.length);
  for(var i = 0; i < drives.length; i++)
  {
    //Print(d.charAt(i)  );
    try 
    {
      if(!nofso)
        drives[i] = fso.GetDrive( d.charAt(i)  )
      else
        drives[i] = d.charAt(i) ;
    }
    catch(e)
    {
    }
      
  }
  background = parseInt('0' + GetSetting("background"));
  if (background == 0) background = 1;


  //初始化图片、文字 数组 
  bgs =  new Array(d.length);     
  iBars =  new Array(d.length);     
  iDris =  new Array(d.length);     
  dLabels =  new Array(d.length);     
  dInfos =  new Array(d.length);   
  iNotReads =  new Array(d.length);

  for(var i = 0; i < drives.length; i++)
  {
      LockRefresh;
      bgs[i] = Image.Create(widget);
      bgs[i].Visible = false;
      iBars[i] = Image.Create(widget);
      iBars[i].Visible = false;
      dLabels[i] = Text.Create(widget);
      dLabels[i].Visible = false;
      dInfos[i] = Text.Create(widget);
      dInfos[i].Visible = false;
      iDris[i] = Image.Create(widget);
      iDris[i].Visible = false;
      iNotReads[i] = Image.Create(widget);
      iNotReads[i].Visible = false;

      bgs[i].Left = 0;
      iBars[i].Left = 24;
      iBars[i].autoSize = false;

      dLabels[i].fontStr = 'clWhite/-15/Tahoma/11/1000';
      dLabels[i].Left = 58;

      dInfos[i].fontStr = 'clWhite/-13/Tahoma/10/1000';
      dInfos[i].Left = 58;


      UnLockRefresh;


  //  }
  }
      
  //显示信息 
  showDrives();
  timer0.enabled = true; 
  
}



//显示磁盘信息
function showDrives()
{
  timer0.enabled = false; 

  Print("show Drivers");
  LockRefresh;
  var y = 0;

  for(var i = 0; i < drives.length; i++)
  {
    var d = drives[i];
    if (d)
    {
      bgs[i].Src = 'images/backgrounds/background' + background + '.png';  
      bgs[i].Top = y;


      if( ( (!nofso)&&(d.isReady) ) || ((nofso)&&(DiskIsReady( d ))  ) ) 
      {
        
        if(!nofso)
          var f = Math.round(d.freeSpace / d.totalSize * 100)
        else
          var f = Math.round( DiskFreeSize(d) / DiskTotalSize(d) * 100);
        var  u = (100 - f);
         
        iBars[i].Src = 'images/meter' + (u < 80 ? 'blue': (u < 90 ? 'orange': 'red')) + GetSetting('barStyle') +'.png';
        iBars[i].Top =  y + 34;
        iBars[i].width = Math.floor((u * 128 / 100));
        iBars[i].clipRight = -Math.floor((f * 128 / 100)); 
        //iBars[i].left = 24 - Math.floor(((128 - iBars[i].width) / 2));
        iBars[i].Visible = true;


        if(!nofso)
          iDris[i].Src = 'images/drives/drive' + d.driveType + '.png'
        else
          iDris[i].Src = 'images/drives/drive' +  DiskType(d) + '.png';
        iDris[i].Top = y;


        if(!nofso)
          dLabels[i].caption = d.VolumeName  + ' (' + d.driveLetter   + ':)'
         else 
          dLabels[i].caption = DiskName( d )  + ' (' + d.toUpperCase() +':)'; 
        dLabels[i].Top = y + 5;
        
        var n = 0, u = Array(' MB', ' GB', ' TB');
        if(!nofso)
          var b = d.FreeSpace
        else
          var b = DiskFreeSize( d );
        while (b >= 1024 && (b / 1024) >= 1)
        {
         b /= 1024;
         n++;
        }   
        var str;
 
        switch(n)                             
        {                                     
           case 2 : 
              str = 'MB';
              break;
           case 3 :
              str = 'GB';
              break;
           case 4 :
              str = 'TB';
              break;
           default: 
              str = "GB";
        }          
        if(!nofso)
          dInfos[i].caption = convertBytes(d.FreeSpace) + str + ' / ' + f + '%'
        else
          dInfos[i].caption = convertBytes(DiskFreeSize(d)) + str + ' / ' + f + '%';
        dInfos[i].Top = y + 18;
        dInfos[i].fontStr = 'clWhite/-13/Tahoma/10/1000';
        
                
        iNotReads[i].Visible = false;
      }
      else
      {
        if(!nofso)
          iDris[i].Src = 'images/drives/drive' + d.driveType + '.png'
        else
          iDris[i].Src = 'images/drives/drive' + DiskType( d ) + '.png';
        iDris[i].Top = y;
        
        iNotReads[i].Src = 'images/notready.png';
        iNotReads[i].Visible = true;
        iNotReads[i].Left = 5;
        iNotReads[i].Top =  y + 29;

        if(!nofso)
          dLabels[i].caption = '(' + d.driveLetter + ':)'
        else 
          dLabels[i].caption = '(' + d.toUpperCase() + ':)'; 
        dLabels[i].fontStr = 'clWhite/-15/Tahoma/11/1000';
        dLabels[i].Left = 60;
        dLabels[i].Top = y + 5;

        dInfos[i].caption = '未就绪';
        dInfos[i].Top = y + 18;
        dInfos[i].fontStr = 'clWhite/-13/Tahoma/10/1000';

        iBars[i].Visible = false;
        
      }
      bgs[i].Visible = true;
      dLabels[i].Visible = true;
      dInfos[i].Visible = true;

      iDris[i].cursorHandPoint = true;
      iDris[i].link = d.driveLetter + ':\\';
      iDris[i].tooltip = "打开硬盘分区 " + iDris[i].link; 

      iDris[i].Visible = true;


      y += 48;
    }
  }

  Print( widgetHeight );
  
  if (y < 57) y = 57;
  if(widgetHeight!=y)
    widgetHeight = y;

  if(drives.length <= 1) 
    if(widgetHeight!=50)
      widgetHeight = 50;  
  
  UnLockRefresh;
  Refresh;
  timer0.enabled = true; 
  
}
 
 
function openDrive()
{        
  //var d = window.event.srcElement.getAttribute('drive');    
  Print(  runCommand(d + ':\\') );
}

function nMenu1OnClick()
{
   showDrives(); 
}

function nMenu2OnClick()
{
  timer0.enabled = false; 
}

function timer0OnTimer()
{
   showDrives(); 
}

function OnAllSettingsSaved()
{
  timer0.enabled = false; 
  SaveAllOption;
  reload; 
}