var path;
var curPic="default_photo.jpg";
var curIndex = 0;  
var valFadeIn = true; 
var isRandom = true;

var isMiniState=false;
var fadeInStep=55;
var fadeInterval = 100;
var fnames;  //所有图片文件名
var isIniting=false;

function showPic()
{
  if(isNaN(curIndex))  curIndex = 0; 
  if(fnames.length==0)
  {
    curIndex = 0;
    curPic = "default_photo.jpg";
    imgPic.src = curPic;
    timer0.enabled = false;
    return;
  }
  
  if(curIndex<0) curIndex = 0;
    
  try
  {
    if( curIndex>fnames.length-1 )
      curIndex = 0;
      
    if(!isIniting)
    {
      if(isRandom)
      { 
        curIndex = Math.round(Math.random()*fnames.length,0); 
        curPic = fnames[curIndex]
      }
      else
        curPic = fnames[curIndex];
    }
   if(curPic=="") curPic = "default_photo.jpg";

  Print( curIndex + "   " + fnames[curIndex]  );
        
  } catch(e)
  {
    curIndex = 0;
    curPic = "default_photo.jpg";
    imgPic.src = curPic;
    timer0.enabled = false;
    return;
  }
   LockRefresh;
   
   imgPicAlpha.bgAlpha = 0;
   WriteIni("curPic",curPic) ;
   WriteIni("curIndex",curIndex );

   
   if( valFadeIn && (!isIniting) )
   {
     timerAlphaHide.interval = fadeInterval;
     timerAlphaHide.enabled = true;
   }
   else
   {
     imgPic.src=curPic;
     if( GetSetting("autosetwallpaper")=="1" )
       SetWallpaper(curPic);
   }
   

   
   UnLockRefresh;


 
}

function alphaHidePic()
{
  if( imgPicAlpha.bgAlpha>=100 )
  {             
 
    LockRefresh;
    
    timerAlphaHide.enabled = false;   
        
    imgPic.src=curPic;
    imgPicAlpha.bgAlpha = 100;
    
    UnLockRefresh;
    
    timerAlphaShow.interval = fadeInterval;
    timerAlphaShow.enabled = true;    

  }
  else
  {
    LockRefresh;  
    
    imgPicAlpha.bgAlpha = imgPicAlpha.bgAlpha + fadeInStep;
    
    UnLockRefresh;
  }  
}

function alphashowpic()
{
  if( imgPicAlpha.bgAlpha<=0 )
  {
 
    LockRefresh;
   
    timerAlphaShow.enabled = false;       
    imgPicAlpha.bgAlpha = 0;  
    
    UnLockRefresh;
    
    if( GetSetting("autosetwallpaper")=="1" )
       SetWallpaper(curPic);

  }
  else
  {
    LockRefresh;  
    
    imgPicAlpha.bgAlpha = imgPicAlpha.bgAlpha - fadeInStep;
    
    UnLockRefresh;
  }  
  
    
}


function widgetOnInit()
{
  curIndex = 0;
  curPic =  "default_photo.jpg";
  path = windowsPath + "web\\Wallpaper"; 

  imgPicAlpha.bgAlpha = 0;
  timerAlphaShow.enabled = false;
  timerAlphaHide.enabled = false;
  timer0.enabled = false;
  
 
  loadSetting();
  Print( curIndex );
  
  timer0.enabled = parseInt( ReadIni("playState") ); 
  isIniting = true;

  showPic();
  
  isIniting = false;
}

function loadSetting()
{
  valFadeIn = parseInt( GetSetting("valFadeIn") );
  path = GetSetting("valPath");
  curPic =  ReadIni("curPic") ;
  isRandom = parseInt( GetSetting("isRandom") );
  curIndex = parseInt( ReadIni("curIndex") );
  isMiniState  = parseInt( ReadIni("isMiniState") );
  if(isNan(isMiniState)) isMiniState = false;
  
  if( GetSetting("valOpenAct")=="0" ) 
    btOpenPic.tooltip = "打开图片文件"
  else 
    btOpenPic.tooltip = "设为桌面壁纸";  
  
  if((path=="")||(path=="\\"))
    path = windowsPath + "web\\Wallpaper"; 

  fnames  = getFileList( path , "jpg,bmp,png,gif" , GetSetting( "includeSubFolder" )=="1");

  fnames=fnames.split("\r\n");
  fnames.splice(fnames.length-1, 1);
  Print("图片数目："+ fnames.length );

  timer0.interval = parseInt( Getsetting("valTime") ) *1000*60; //单位 分钟
}



function timer0OnTimer()
{
  curIndex++;
  showPic();
}



function OnAllSettingsSaved()
{
  loadSetting();
  showPic();
  timer0.enabled = parseInt( ReadIni("playState") ); 
  Print( timer0.enabled );
}

function widgetOnDock()
{
  LockRefresh;
  
  widgetWidth = 130;
  widgetHeight = 100;
  
  bg.Src = 'bg_docked.png';
  
  imgPic.Left = 4;
  imgPic.Top = 5;
  imgPic.Width = 120;
  imgPic.Height = 90;
  
  imgBtBg.Left = 8;
  imgBtBg.Top = 72;

  btPrev.Left = 18;
  btPrev.Top = 75;
  
  btPlay.Left = 43;
  btPlay.Top = 75;
  
  btPause.Left = 43;
  btPause.Top = 75;
  
  btNext.Left = 63;
  btNext.Top = 75;
  
  btOpenPic.Left = 88;
  btOpenPic.Top = 75;  
  
  
  imgPicAlpha.Left = imgPic.Left;
  imgPicAlpha.top = imgPic.Top;
  imgPicAlpha.width = imgPic.width;
  imgPicAlpha.height = imgPic.height;
  
  
  UnLockRefresh;

  

}

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

function widgetOnUnDock()
{

  LockRefresh;

  widgetWidth = 360;
  widgetHeight = 280;
  
  bg.Src = 'bg.png';      
  
  imgPic.Left = 17;
  imgPic.Top = 16;
  imgPic.Width = 320;
  imgPic.Height = 240;
  
  imgBtBg.Left = 125;
  imgBtBg.Top = 230;

  btPrev.Left = 135;
  btPrev.Top = 233;
  
  btPlay.Left = 160;
  btPlay.Top = 233;
  
  btPause.Left = 160;
  btPause.Top = 233;
  
  btNext.Left = 180;
  btNext.Top = 233;
  
  btOpenPic.Left = 205;
  btOpenPic.Top = 233;

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

function widgetOnMouseWheelDown()
{
  btNextOnClick();
}

function widgetOnDBClick()
{
  showPic();  
}

function nMenu1OnClick()
{
  ShowSetting;
}


function nMenu4OnClick()
{
  open( path );
}

function nMenu3OnClick()
{
  run( 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;

}

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

}

function nMenu2OnClick()
{
  SetWallpaper( curPic );
}



function btPlayOnClick()
{
  curIndex++;
  timer0.enabled = true;
  btPlay.Visible = !(timer0.enabled);
  btPause.Visible = timer0.enabled; 
  showPic();
  
  WriteIni("playState",1);
}

function btPauseOnClick()
{
  timer0.enabled = false;
  btPlay.Visible = !(timer0.enabled);
  btPause.Visible = timer0.enabled;
   
  WriteIni("playState",0);

}

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



function nMenu6OnClick()
{
  isMiniState = !isMiniState;
  checkDock();
  WriteIni("isMiniState",isMiniState);
}

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

function btPrevOnClick()
{
  timer0.enabled = false;
  curIndex--;
  if(curIndex<0) curIndex = fnames.length-1;
  showPic();
  timer0.enabled = parseInt( ReadIni("playState") );
}

function btNextOnClick()
{
  timer0.enabled = false;
  curIndex++;
  if(curIndex>fnames.length-1) curIndex = 0;  
  showPic();
  timer0.enabled = parseInt( ReadIni("playState") );
}

function widgetOnMouseWheelUp()
{
  btPrevOnClick();
}

function btOpenPicOnClick(sender)
{
  if( GetSetting("valOpenAct")=="0" ) 
    run( curPic )
  else
    SetWallpaper( curPic );  
}