var fso;
var defaultPath,p,pp,curPic;
var isMiniState=false;

var maxcountValue =500; 
var maxcount; //最大文件数限制 
var fnames;  //所有文件名
 
//var  alphahandle ; 


function ReadPath(path)
{
  try
  {
    path=defaultPath;
    if((path=="")||(path=="\\"))
    {
      var  WshShell  =  new  ActiveXObject("WScript.Shell"); 
      var keyValue  =  WshShell.RegRead("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\My Pictures"); 
      path= keyValue; 
      defaultPath = path;
      Print( defaultPath );
    }

    fnames = "";
    maxcount = 0;
    
    if(GetSetting( "includeSubFolder" )==false)
    { 
      GetFilnameInFolder(path);
    }
    else
      GetAllFilenameFromAllFolder(path);
    
    fnames=fnames.split(";");
    Print( "图片文件数量："+fnames.length);
  }
  catch(e)
  {
   //alert(e);
   return;
  }    
}
function showPic()
{
  timer0.enabled = false;

  try{
    var pic= fnames[Math.round(Math.random()*fnames.length,0)];
    var picExt=pic.split(".");
    var  picExtName = picExt[picExt.length-1].toLowerCase();
  }
  catch(e)
  {
    timer0.enabled = true;
    return;
  }
  
  if(( picExtName =="jpg")||(picExtName=="bmp")||(picExtName=="png")||(picExtName=="gif")  )
  {  
     LockRefresh;
     
     imgPicAlpha.alpha = 0;
     imgPicAlpha.bgAlpha = 0;
     imgPicAlpha.Visible = true;
     imgPicAlpha.src=pic;
     curPic = pic;
     imgPicAlpha.alpha = 1;
     imgPicAlpha.bgAlpha = 1;
     
     UnLockRefresh;
     Refresh;
     
     timerAlphaShow.interval = 100;
     timerAlphaShow.enabled = true;
     
     //alphahandle = setTimeout( 'alphashowpic' , 100 );
     
  }
  else
  {
    showPic();
    timer0.enabled = true;

  }
  
}

function alphashowpic()
{
  if( imgPicAlpha.alpha>=100 )
  {
  
    //Print( "clearhandle-" + alphahandle );  
    timerAlphaShow.enabled = false;       
    //clearTimeout(alphahandle);
    
//    LockRefresh;
    
    imgPic.src=curPic;
    imgPicAlpha.Visible = false;
    
    Refresh;

    timer0.enabled = true;
    

  }
  else
  {
    LockRefresh;  
    
    imgPicAlpha.alpha = imgPicAlpha.alpha + 30;
    imgPicAlpha.bgAlpha = imgPicAlpha.bgAlpha + 30;
    
    UnLockRefresh;
    Refresh;
  }
  
    
}

function widgetOnStart()
{
  //LockRefresh;
  defaultPath=GetSetting('valPath');
  //print("defaultPath:"+defaultPath);
  ReadPath();
  showPic();
  //UnLockRefresh;
  refresh;
  timer0.interval=Getsetting("valTime")*1000;    //改成秒为单位 
  timer0.enabled=true; 
 
   
}


function widgetOnInit()
{
  maxcountValue = GetSetting( "maxcountValue" );
  try{
    fso = new ActiveXObject("Scripting.FileSystemObject");
  }
  catch(e)
  {
    alert( "您的系统没有启用FileSystemObject，小工具无法启动。" );
    close;    
  }

}

function timer0OnTimer()
{
  showPic();
}

function OnAllSettingsSaved()
{
  maxcountValue = GetSetting( "maxcountValue" );
  widgetOnStart();
  SaveAllOption;
}

function widgetOnDock()
{
  isMiniState = true;
  LockRefresh;
  
  widgetWidth = 139;
  widgetHeight = 108;
  
  bg.Src = 'bg_docked.png';
  
  imgPic.Left = 9;
  imgPic.Top = 9;
  imgPic.Width = 120;
  imgPic.Height = 90;
  
  imgBtBg.Left = 12;
  imgBtBg.Top = 72;

  btPrev.Left = 22;
  btPrev.Top = 75;
  
  btPlay.Left = 47;
  btPlay.Top = 75;
  
  btPause.Left = 47;
  btPause.Top = 75;
  
  btNext.Left = 67;
  btNext.Top = 75;
  
  btOpenPic.Left = 92;
  btOpenPic.Top = 75;  
  
  
  imgPicAlpha.Left = imgPic.Left;
  imgPicAlpha.top = imgPic.Top;
  imgPicAlpha.width = imgPic.width;
  imgPicAlpha.height = imgPic.height;
  
  
  UnLockRefresh;
  Refresh;

  

}

function widgetOnUnDock()
{
  isMiniState = false;

  LockRefresh;

  widgetWidth = 245;
  widgetHeight = 188;
  
  bg.Src = 'bg.png';      
  
  imgPic.Left = 11;
  imgPic.Top = 10;
  imgPic.Width = 224;
  imgPic.Height = 168;
  
  imgBtBg.Left = 75;
  imgBtBg.Top = 150;

  btPrev.Left = 85;
  btPrev.Top = 153;
  
  btPlay.Left = 110;
  btPlay.Top = 153;
  
  btPause.Left = 110;
  btPause.Top = 153;
  
  btNext.Left = 130;
  btNext.Top = 153;
  
  btOpenPic.Left = 155;
  btOpenPic.Top = 153;

  
  imgPicAlpha.Left = imgPic.Left;
  imgPicAlpha.top = imgPic.Top;
  imgPicAlpha.width = imgPic.width;
  imgPicAlpha.height = imgPic.height;
  
  
  UnLockRefresh;
  
  imgPic.src=curPic;
       
  Refresh;
  
}

function widgetOnMouseWheelDown()
{
  showPic();
}

function widgetOnDBClick()
{
  showPic();  
}

function nMenu1OnClick()
{
  ShowSetting;
}


function nMenu4OnClick()
{
   
  Open( defaultPath );
}

function nMenu3OnClick()
{
  OpenFile( curPic );
  
}

function widgetOnEnter()
{
  LockRefresh;
  imgBtBg.Visible = true;
  btPrev.Visible = true;
  btNext.Visible = true;
  btOpenPic.Visible = true;
  
  btPlay.Visible = !(timer0.enabled);
  btPause.Visible = timer0.enabled; 
  
  UnLockRefresh;
  Refresh;  
}

function widgetOnLeave()
{
  LockRefresh;
  
  imgBtBg.Visible = false;
  btPrev.Visible = false;
  btNext.Visible = false;
  btOpenPic.Visible = false;
  
  btPlay.Visible = false;
  btPause.Visible = false; 
  
  UnLockRefresh;
  Refresh;  
}

function nMenu2OnClick()
{
  SetWallpaper( curPic );
}



function btPlayOnClick()
{
  timer0.enabled = true;
  btPlay.Visible = !(timer0.enabled);
  btPause.Visible = timer0.enabled; 
  showPic();
}

function btPauseOnClick()
{
  timer0.enabled = false;
  btPlay.Visible = !(timer0.enabled);
  btPause.Visible = timer0.enabled; 

}

function widgetOnPopupMenu()
{
  nMenu6.Visible = !dockedOnSidebar;
  if(!dockedOnSidebar)
  {
    if(isMiniState)
      nMenu6.caption = "切换到详细模式"
    else 
      nMenu6.caption = "切换到迷你模式";       
  } 
}



function nMenu6OnClick()
{
  if(dockedOnSidebar)
  {
  
  }
  else
  {
    if(isMiniState)
      widgetOnUnDock()
    else 
      widgetOnDock(); 
  }  
}

function  isInvalidFilename(fname)
{
  try{
    var pic= "" + fname;
    var picExt=pic.split(".");
    var  picExtName = picExt[picExt.length-1].toLowerCase();
  }
  catch(e)
  {
    return false;
  }
  
  if(( picExtName =="jpg")||(picExtName=="bmp")||(picExtName=="png")||(picExtName=="gif")  )
    return true
  else
    return false;  
}

function GetFilnameInFolder(path)
{
  if(maxcount>maxcountValue) 
    return;        

  var f=fso.GetFolder(path);  
  var fc=new Enumerator(f.files);  
  for(; !fc.atEnd();fc.moveNext())  
  {  
    var fname = fc.item();
    if(isInvalidFilename(fname))
    {   
      fnames += fname;  
      fnames += ";";
      maxcount++;
      if(maxcount>maxcountValue) 
        return;        
    }
  }
}

function GetAllFilenameFromAllFolder(path)
{
  if(maxcount>maxcountValue) 
    return;        

  GetFilnameInFolder(path);
  
  var f=fso.GetFolder(path);  
  var fs = new Enumerator(f.SubFolders);
  for( ;!fs.atEnd(); fs.moveNext())
  {
     if(maxcount>maxcountValue) 
      return;        
     GetAllFilenameFromAllFolder( fs.item() );
  }
      
} 

function widgetOnKeyDown()
{
  if((keyCode==37)||(keyCode==38)||(keyCode==39)||(keyCode==40))
  {
    showPic(); 
  
  }
}