var s,s1,DiskNumber,lastl,nowl;
var dk=new Array();
var txt=new Array();
var pr=new Array();
var ig=new Array();
var s1;


function widgetOnInit()
{
  LockRefresh;
  for (i=0;i<23;i++)
  {
    txt[i]=text.Create(widget);
    txt[i].Visible=false;

    ig[i]=image.Create(widget);
    ig[i].Visible=false;

    pr[i]= ProgressBar.Create(widget);// image.Create(widget);
    pr[i].Visible=false;
  }   

  s1=GetSetting("dkname");
  if(s1=="") s1 = "C";
     
  var num = 0;
  for (var i=0;i<=23;i++ )
  { 
    s = s1.charAt(i);
    if (DiskIsReady(s)==true) 
    {
      num = num + 1;
      dk[num]=s;
    }
  }   
  widgetHeight = (num+1)*24+10+10;
  var s = GetSetting("bg");
  
  bg1.Src = "bg_" + s + "1.png" ;
  bg2.Src = "bg_" + s + "2.png" ;
  bg3.Src = "bg_" + s + "3.png" ;
  
  bg2.Top = bg1.Height;
  bg2.Height = widgetHeight - bg1.Height - bg3.Height;
  bg3.Top = bg1.Height + bg2.Height;
  
  
  UnLockRefresh;
  
  timer0.interval=GetSetting("ft");
  timer0OnTimer();
  
      
}


function timer0OnTimer()
{   
    LockRefresh;

    DiskNumber=-1;
    if(s1=="") s1 = "C";
    for (i=0;i<=23;i++ )
    { 
      s = s1.charAt(i);
      if (DiskIsReady(s)==true) 
      {
        DiskNumber = DiskNumber+1;
        dk[DiskNumber]=s;
       }
    }   
    nowl=DiskNumber;
        
    if (nowl!=lastl)
    { 
      widgetHeight=(DiskNumber+1)*24+10+10;
      var s = GetSetting("bg");
      
      bg1.Src = "bg_" + s + "1.png" ;
      bg2.Src = "bg_" + s + "2.png" ;
      bg3.Src = "bg_" + s + "3.png" ;
      
      bg2.Top = bg1.Height;
      bg2.Height = widgetHeight - bg1.Height - bg3.Height;
      bg3.Top = bg1.Height + bg2.Height;
      
      
      lastl=nowl;
    }

    for (i=0;i<=DiskNumber;i++)
    {
       txt[i].Visible=true;
       ig[i].Visible=true;
       pr[i].Visible=true;

       p= DiskUsage( dk[i] );  //100-Math.round( DiskFreeSize(dk[i]) / DiskTotalSize(dk[i]) * 100);
       if (DiskName(dk[i])=='')
         txt[i].caption = dk[i]+"盘 "+convertBytes(DiskFreeSize(dk[i])) +"/"+(p)+"%";
       else
         txt[i].caption = DiskName(dk[i])+" "+convertBytes(DiskFreeSize(dk[i])) + "/"+(p)+"%";

       if (txt[i].caption.length>20)
       txt[i].caption=DiskName(dk[i])
       txt[i].fontStr = 'clWhite/-15/Tahoma/10/1000'
       txt[i].Top=24*(i)+10;
       txt[i].Left=28;
       txt[i].fontColor="255,255,255";
       
       pr[i].Top=24*(i)+23;
       pr[i].Left=28;
       pr[i].Height=5;
       pr[i].barStyle = 1;
       pr[i].bgColor = 'clWhite';
       pr[i].bgAlpha = 20;
       pr[i].borderAlpha = 0;
       pr[i].Src='meter' + (p < 80 ? 'blue': (p < 90 ? 'orange': 'red'))  +'.png';
       pr[i].percent = p;
       //pr[i].clipRight = -(100-p);
       
       ig[i].Src="drives\\drive"+(DiskType(dk[i]))+".png";
       ig[i].Top=i*24+10;
       ig[i].link=(dk[i] + ":\\")
       ig[i].cursorHandPoint = true;
       ig[i].tooltip =  DiskName(dk[i])+"("+ DriveTypeName(DiskType(dk[i]))+dk[i].toUpperCase() +":"+")\n" ;   
       ig[i].tooltip =  ig[i].tooltip + "总大小: "+convertBytes(DiskTotalSize(dk[i])) + "\n可用空间("+ (100 - DiskUsage(dk[i])) +"%): " + convertBytes(DiskFreeSize(dk[i])) ;   
    }

    for (i=DiskNumber+1;i<23;i++)
    {
      txt[i].Visible=false;
      ig[i].Visible=false;
      pr[i].Visible=false; 
    }

   UnLockRefresh;      
} 

function OnAllSettingsSaved()
{
  LockRefresh;
  var s = GetSetting("bg");
  
  bg1.Src = "bg_" + s + "1.png" ;
  bg2.Src = "bg_" + s + "2.png" ;
  bg3.Src = "bg_" + s + "3.png" ;
  
  bg2.Top = bg1.Height;
  bg2.Height = widgetHeight - bg1.Height - bg3.Height;
  bg3.Top = bg1.Height + bg2.Height;  
  
  s1=GetSetting("dkname");
  if(s1=="") s1 = "C";   
  
  UnLockRefresh;
  timer0OnTimer();
}                                                                                 

 
function convertBytes(bytes)
{
  var i = 0;
  var units = Array("B", "KB" , "MB", "GB", "TB" );
  while ( bytes >= 1024 && ( bytes / 1024 ) >= 1 )
  {
    bytes /= 1024;
    i++;
  }       
  return ( bytes.toFixed(1)  + units[i] );

}
function DriveTypeName(pDriveType)
{
   switch (pDriveType)
   {
      case 0: t = "Unknown"; break;
      case 1: t = "可移动磁盘"; break;
      case 2: t = "本地磁盘"; break;
      case 3: t = "网络驱动器"; break;
      case 4: t = "CD驱动器"; break;
      case 5: t = "RAM驱动器"; break;
   }
   return(t);
}