/////////////////////////////////////////////
///                                       ///
///     小恐龙呕血原创                    ///
///                                       ///
///   使用本widget代码请勿删除本声明      ///
///////////////////////////////////////////// 


var wmp;
var duration;   //歌曲长度 
var ps;  //播放状态 int 
var fdown=false;//音量图片,鼠标按下状态bool
var tdown=false;//进度图片,鼠标按下状态bool 
var musicExt=new Array("mp3","m3u","wma","wax","mid","midi","rmi","aif","aifc","aiff","au","snd","cda","wav");
var vol,mu;//系统音量 
var pl;//播放列表
var music=new Array; //音乐文件项目集合 
var sty=0;  //外观 
var nbg="noalbumart.png";
 
function widgetOnInit()
{
  
  widgetStyle(ReadIni("style"));
  try   
  {           
    wmp = new ActiveXObject('WMPlayer.OCX');
  }
  catch(e)   
  { 
    alert('请安装微软媒体播放器9.0或以上版本(Windows Media Player 9)!');
    closeWidget;
    return;   
  }
  if (wmp.playlistCollection.getbyname('鱼鱼桌面秀播放列表').count == 0) {
    wmp.playlistCollection.newplaylist('鱼鱼桌面秀播放列表');
  }
  pl = wmp.playlistCollection.getbyname('鱼鱼桌面秀播放列表').item(0);
  wmp.currentPlaylist=pl;
  //Print( wmp.currentPlaylist.name);
  wmp.controls.stop();  //初始时停止
  //获取随机播放设置 
  if(GetSetting("valRnd")==1)
  {
    wmp.settings.setMode("shuffle", true);
  }
  else
  {
    wmp.settings.setMode("shuffle", false);
  }
  //获取重复播放设置 
  if(GetSetting("valRep")==1)
     wmp.settings.setMode("loop",true);
  else
     wmp.settings.setMode("loop",false);
  
  //获取背景设置 
  nbg=GetSettingText("valBg"); 
  if(nbg=="")nbg="noalbumart.png";
  imgAlbum.Src=nbg;   
      
  txtAuthor.caption=txtZhuanji.caption=txtTitle.caption=txtTime.caption="";
  timer0.enabled=false; 
  barTrack.percent=0;
  barVol.Visible=false; 
  imgVol.Visible=false;
  tmOnTimer(); 
}

// 拖入歌曲文件后加入播放列表 
function widgetOnDropFile(fileNameList)
{
  var f=fileNameList.split("|");
  Print("拖入的文件组:"+f);
  barTrack.percent=0;
  Print("长度:"+f.length); 
  for(i=0;i<f.length;i++)
  {
    //Print(checkMusicName(f[i]));
    Print("f"+i+":"+f[i]);
    var ff=f[i];
    if(checkMusicName(f[i]))
    { 
      Print("添加文件:"+ff);
      var pmedia = wmp.newMedia(ff);       //新建一个newMedia对象 
      wmp.currentPlaylist.appendItem(pmedia);  //插入到当前播放列表 
    }
  }
  wmp.Controls.play();
  btnPlay.downImgSrc="pause_down.png";
  btnPlay.hotImgSrc="pause_over.png";
  btnPlay.normalImgSrc="pause.png";
  getInfo();
  timer0.enabled=true;
}

function checkMusicName(files)  //检测文件扩展名 
{
   Print("chk:"+files);
   var Ext=files.split(".");
   var ExtName = Ext[Ext.length-1].toLowerCase();
   //"mp3","m3u","wma","wax","mid","midi","rmi",
   //"aif","aifc","aiff","au","snd","cda","wav";
   if(ExtName=="mp3" || ExtName=="m3u"|| ExtName=="wma"|| ExtName=="wax")
       return true;
   else if(ExtName=="mid"|| ExtName=="midi"|| ExtName=="rmi"|| ExtName=="aif")
       return true;
   else if(ExtName=="aifc"|| ExtName=="aiff"|| ExtName=="au"|| ExtName=="snd")
       return true;
   else if(ExtName=="cda"|| ExtName=="wav")
       return true;
   else 
       return false;

}


///////////////////////////////////////////////
////// 获取歌曲信息
//////////////////////////////////////////////// 
function getInfo()
{
  LockRefresh;
  //获取艺术家信息
  try{
    var author=wmp.currentMedia.getItemInfo("Author");  
    Print("author:"+author);
  }
  catch(e){
   author="";
  }
  txtAuthor.tooltip=author;  
  txtAuthor.caption=author;
  //获取唱片集信息
  try{
    //var album=wmp.currentMedia.getItemInfo("AlbumID");
    var album=wmp.currentMedia.getItemInfo("WM/AlbumTitle");
  }
  catch(e){
   album="";
  } 
  txtZhuanji.tooltip=album;
  txtZhuanji.caption=album;
  //获取歌曲名称
  try{
   var title=wmp.currentMedia.getItemInfo("Title");
  }
  catch(e){
   title="";
  }
  txtTitle.tooltip=title;
  txtTitle.caption=title; 
  //获取歌曲长度 
  try{
    duration=wmp.currentMedia.durationString; 
    //Print("duration:"+duration);
  }
  catch(e){
    duration="0.00";
  }
  //获取歌曲封面
  //imgAlbum.Src=nbg;
  if(GetSetting("valCov")==1)
  { 
   try{
     var picPath=findAlbumArt();
     if(picPath==null){
       imgAlbum.Src=nbg;
     }
     else
       imgAlbum.Src= picPath;
   }
   catch(e){
     imgAlbum.Src=nbg;
   }
  }
  
  getCurTrack();
  //txtTime.caption="0:00/"+duration;
  UnLockRefresh;
}
//////////////////////////////////////////////////////////////////// 

function getCurTrack()  //当前进度 
{
  var track=wmp.controls.currentPositionString;  //Print("track:"+track);
  if(track==""){track="00:00";}
  txtTime.caption=track+"/"+duration;
  try{
   barTrack.percent=(wmp.controls.currentPosition/wmp.currentMedia.duration)*100;
   setbtnPlay(wmp.playState);
  }
  catch(e){
   barTrack.percent=0;
  }
} 

///根据传入的参数,设置播放按钮的显示状态 
function setbtnPlay(ps)
{
  switch(ps){
     case 1: case 2:  //1=Stopped  2=Paused
       btnPlay.downImgSrc="play_down.png";
       btnPlay.hotImgSrc="play_over.png";
       btnPlay.normalImgSrc="play.png";
       timer0.enabled=false;
       timer1.enabled=false;
       break;
     case 3:      //3=playing
       btnPlay.downImgSrc="pause_down.png";
       btnPlay.hotImgSrc="pause_over.png";
       btnPlay.normalImgSrc="pause.png";
       timer0.enabled=true;
       timer0.enabled=true; 
   } 
}

///通过timer控件获取播放进度 
function timer0OnTimer()
{
  try{
    getCurTrack();
    timer1.enabled=true;
  }
  catch(e){
    timer0.enabled=false;Print("Timer error!");
  }
}
//鼠标划过音量按钮后,显示音量控制条 
function btnVolOnMouseEnter(sender)
{
  barVol.Visible=true; 
  imgVol.Visible=true;
  tmOnTimer();
}

//获取歌曲封面图片,采用了google播放器类似的方法.
//currentMedia.getItemInfoByType()这个函数未能得到支持.尽管wmp SDK上有此方法 
function findAlbumArt()    
{
  if( wmp == null ) return null;
  
  var media = wmp.currentMedia;
  if( media == null ) return null;
  
  var filename = media.sourceUrl;
  
  var i = filename.lastIndexOf( "\\" );
  if( i == -1 ) return null;
  
  var art_path = filename.substr( 0, i ) + "\\folder.jpg";
  if( !FileExists( art_path ) ) return null;

  return art_path;
}

//音量状态和音量图标的关系 
function tmOnTimer()
{
  //var vol= wmp.settings.volume;  使用wmp的音量响应速度太慢,效果不好 
  var vol=system.volume; 
  if((system.volume == vol)&&( system.volume==mu ))
    return;
  LockRefresh;                     
  mu  = system.mute;
  barVol.percent = vol;
  if(vol==0)
  {
    btnVol.normalImgSrc="volume_0.png";
  }
  else if(vol<=33)
  {
    btnVol.normalImgSrc="volume_33.png";
  }
  else if(vol<=66 && vol>33)
  {
    btnVol.normalImgSrc="volume_66.png";
  }
  else
  {
    btnVol.normalImgSrc="volume_100.png";
  }
  if(mu==true)
  {
     btnVol.normalImgSrc="volume_muted.png";
  }
  UnLockRefresh;                     
}
//////////////////////////////////////////////////////// 
///    音量控制
//////////////////////////////////////////////////////// 
function btnVolOnClick(sender)
{
   system.mute = !system.mute;
   //wmp.settings.mute=!wmp.settings.mute     //使用wmp的音量响应速度太慢,效果不好 
   tmOnTimer();
}

function imgVolOnMouseMove(sender)
{
  if(fdown)
  {
    //使用wmp的音量响应速度太慢,效果不好 
    //wmp.settings.volume= (mouseX - widget.Left - imgVol.Left)*1.25;
    system.volume = (mouseX - widget.Left - imgVol.Left)*1.25;
    tmOnTimer(); 
  }
}

function imgVolOnMouseDown(sender)
{
  fdown = true;
  //wmp.settings.volume=(mouseX - widget.Left - imgVol.Left)*1.25;
  system.volume = (mouseX - widget.Left - imgVol.Left)*1.25; 
  tmOnTimer();  
}

function imgVolOnMouseUp(sender)
{
  fdown = false;
}
function imgVolOnClick(sender)
{
  fdown = false;
}
//////////////////////////////////////////////////////////////////// 

//音量控制条的显示状态 
function widgetOnDeActive()
{
  imgVol.Visible=false;
  barVol.Visible=false;
}

//播放/暂停按钮的播放控制 
function btnPlayOnClick(sender)
{
  if(wmp.playState==3){ 
    wmp.controls.pause();
  }
  else if(wmp.currentPlaylist.count==0){
    setbtnPlay(wmp.playState);
    return;
  }
  else {
   wmp.controls.play();
   btnPlay.downImgSrc="pause_down.png";
   btnPlay.hotImgSrc="pause_over.png";
   btnPlay.normalImgSrc="pause.png";
   getInfo();
   timer0.enabled=true;
   timer1.enabled=true;   
  }
  setbtnPlay(wmp.playState); 
}
//下一首歌曲 
function btnNextOnClick(sender)
{
  wmp.controls.next();
  getInfo();
  timer0.enabled=true;
}
//上一首歌曲 
function btnPrevOnClick(sender)
{
  wmp.controls.previous();
  getInfo();
  timer0.enabled=true;
}
////////////////////////////////////////////// 
////播放列表管理器的脚本窗体 
/////////////////////////////////////////////// 
function plEdit_scriptFuncName(panel)
{
  ////添加文件数量统计标签 
  var mcount= wmp.currentPlaylist.count;
  lbIconCount = TLabel.Create(widget);
  lbIconCount.parent = panel;
  lbIconCount.rc = rect(20,2,200,14);
  lbIconCount.caption = "音乐文件 [共 "+mcount +" 个]:";
  ////添加列表框,并读入播放列表文件 
  musicListBox = TListBox.Create(widget);
  musicListBox.parent = panel;    
  musicListBox.rc = Rect(20,18,340,130);
  for(var i=0;i<mcount;i++)
    musicListBox.items.add(wmp.currentPlaylist.Item(i).name);
  if(musicListBox.items.count>0)  musicListBox.itemIndex = 0;
  musicListBox.OnClickEx = "musicListBoxOnClickEx";
  
//////////////////////////////////////////////////////// 
  var lb = TLabel.Create(widget);
  lb.parent = panel;
  lb.rc = rect(25,160,50,14);
  lb.caption = "歌曲名称:";
  
  edtMusicName = TEdit.Create(widget);
  edtMusicName.parent = panel;
  edtMusicName.rc = Rect(90,155,270,21);

//////////////////////////////////////////////////////// 
  var lb = TLabel.Create(widget);
  lb.parent = panel;
  lb.rc = rect(25,185,50,14);
  lb.caption = "艺 术 家:";

  edtMusicAuthor = TEdit.Create(widget);
  edtMusicAuthor.parent = panel;
  edtMusicAuthor.rc = Rect(90,182,270,21);
//////////////////////////////////////////////////////// 
  var lb = TLabel.Create(widget);
  lb.parent = panel;
  lb.rc = rect(25,211,50,14);
  lb.caption = "文件位置:";

  edtMusicURL = TEdit.Create(widget);
  edtMusicURL.parent = panel;
  edtMusicURL.rc = Rect(90,208,270,21);
//////////////////////////////////////////////////////// 
  btAddMusic = TButton.Create(widget);
  btAddMusic.parent = panel;
  btAddMusic.caption = "添加";
  btAddMusic.OnClickEx = "btAddMusicOnClick";
  btAddMusic.rc = Rect(20,250,40,21);
  
  btDelMusic = TButton.Create(widget);
  btDelMusic.parent = panel;
  btDelMusic.caption = "移走";
  btDelMusic.OnClickEx = "btDelMusicOnClick";
  btDelMusic.rc = Rect(61,250,40,21);
  
  btClearMusic = TButton.Create(widget);
  btClearMusic.parent = panel;
  btClearMusic.caption = "清空";
  btClearMusic.OnClickEx = "btClearMusicOnClick";
  btClearMusic.rc = Rect(102,250,40,21);
  
  btPlayMusic = TButton.Create(widget);
  btPlayMusic.parent = panel;
  btPlayMusic.caption = "播放";
  btPlayMusic.OnClickEx = "btPlayMusicOnClick";
  btPlayMusic.rc = Rect(200,250,40,21);
//////////////////////////////////////////////////////// 
  btUpLevel = TButton.Create(widget);
  btUpLevel.parent = panel;
  btUpLevel.caption = "上移";
  btUpLevel.OnClickEx = "btUpLevelOnClick";
  btUpLevel.rc = Rect(278,250,40,21);
  
  btDownLevel = TButton.Create(widget);
  btDownLevel.parent = panel;
  btDownLevel.caption = "下移";
  btDownLevel.OnClickEx = "btDownLevelOnClick";
  btDownLevel.rc = Rect(320,250,40,21);
  
  musicListBoxOnClickEx(musicListBox); 
}
//////////////////////////////////////////////////////// 
//播放试听按钮 

//点击播放列表某项时显示歌曲信息 
function musicListBoxOnClickEx(sender)
{
  var curID = musicListBox.itemIndex;
  if((curID<0)||(curID>=musicListBox.items.count) )
    return;
  try{
    edtMusicName.Text=wmp.currentPlaylist.Item(curID).name;
  }
  catch(e){edtMusicName.Text="错误!";}
  try{
    edtMusicAuthor.Text=wmp.currentPlaylist.Item(curID).getItemInfo("Author");
  }
  catch(e){edtMusicAuthor.Text="错误!";}  
  try{
    edtMusicURL.Text=wmp.currentPlaylist.Item(curID).sourceURL;
  }
  catch(e){edtMusicURL.Text="错误!";}  
  
}

//添加歌曲到播放列表 
function btAddMusicOnClick(sender)
{
  //openFileDialog.
  var dg = TOpenDialog.Create(widget);
  dg.Filter = "音乐文件|*.mp3;*.m3u;*.wma;*.wax;*.mid;*.midi;*.rmi;*.aif;*.aifc;*.aiff;*.au;*.snd;*.cda;*.wav";
  dg.title = "添加音乐文件";
  dg.InitialDir = myMusicPath; 
    if(!dg.Execute)
      return;
    fn = dg.FileName;
    dg.free;  
  try{
   if(checkMusicName(fn)){
     var pmedia = wmp.newMedia(fn);       //新建一个newMedia对象
     wmp.currentPlaylist.appendItem(pmedia);  //插入到当前播放列表
     var mcount= wmp.currentPlaylist.count;  //刷新播放列表个数 
     musicListBox.items.add(pmedia.getItemInfo("Title"))
     lbIconCount.caption = "音乐文件 [共 "+mcount +" 个]:";    
   } 
  }
  catch(e){alert("添加音乐文件出错!");}  

}

//从播放列表移走歌曲 
function btDelMusicOnClick(sender)
{
  var curID = musicListBox.itemIndex;
  if((curID<0)||(curID>=musicListBox.items.count) )
    return;
  try{
   var pmedia = wmp.currentPlaylist.item(curID);
   wmp.currentPlaylist.removeItem(pmedia);
   musicListBox.DeleteSelected;
   var mcount= wmp.currentPlaylist.count;
   lbIconCount.caption = "音乐文件 [共 "+mcount +" 个]:";    
  }
  catch(e){alert("移走音乐文件出错!");}
}

//清空播放列表 
function btClearMusicOnClick(sender)
{
  try{
   wmp.currentPlaylist.clear();
   musicListBox.clear;
   var mcount= wmp.currentPlaylist.count;
   lbIconCount.caption = "音乐文件 [共 "+mcount +" 个]:";    
  }
  catch(e){alert("清空播放列表出错!");}
}
//试听歌曲 
function btPlayMusicOnClick(sender)
{
  var curID = musicListBox.itemIndex;
  if((curID<0)||(curID>=musicListBox.items.count) )
    return;
 
  try{
    var pmedia = wmp.currentPlaylist.item(curID);
    wmp.controls.playItem(pmedia);
    getInfo();
    setbtnPlay(3); 
        
  }
  catch(e){alert("试听音乐文件时出错!");}
   
}
function btUpLevelOnClick(sender)
{
  curID = musicListBox.itemIndex;
  if((curID<=0)||(curID>=musicListBox.items.count) )
    return;
  try{ 
    wmp.currentPlaylist.moveItem(curID,curID-1);
    musicListBox.Items.Move(curID,curID-1);
    musicListBox.ItemIndex=curID-1;
  }
  catch(e){alert("移动音乐文件的顺序出错!");}
}

//调整播放列表中歌曲的顺序 
function btDownLevelOnClick(sender)
{
  curID = musicListBox.itemIndex;
  if((curID<=0)||(curID>=musicListBox.items.count) )
    return;
  try{ 
    wmp.currentPlaylist.moveItem(curID,curID+1);
    musicListBox.Items.Move(curID,curID+1);
    musicListBox.ItemIndex=curID+1;
  }
  catch(e){alert("移动音乐文件的顺序出错!");}
}

////////////////////////////////////////////////
// 播放进度控制
/////////////////////////////////////////////// 
function imgTrackOnMouseDown(sender)
{
  tdown=true;
  var c=(mouseX - widget.Left - imgTrack.Left)/imgTrack.Width;
  wmp.controls.currentPosition=wmp.currentMedia.duration *c; 
}

function imgTrackOnMouseMove(sender)
{
  if(tdown){
    var c=(mouseX - widget.Left - imgTrack.Left)/imgTrack.Width;
    wmp.controls.currentPosition=wmp.currentMedia.duration *c; 
  }
}
function imgTrackOnMouseUp(sender)
{
  tdown=false;
}

function imgTrackOnClick(sender)
{
  tdown=false;
}
//////////////////////////////////////////////////////////// 
//添加音乐到播放列表的函数 
function addMusciFile()
{
  var dg = TOpenDialog.Create(widget);
  dg.Filter = "音乐文件|*.mp3;*.m3u;*.wma;*.wax;*.mid;*.midi;*.rmi;*.aif;*.aifc;*.aiff;*.au;*.snd;*.cda;*.wav";
  dg.title = "添加音乐文件";
  dg.InitialDir = myMusicPath; 
    if(!dg.Execute)
      return;
    fn = dg.FileName;
    dg.free;  
  try{
   if(checkMusicName(fn)){
     var pmedia = wmp.newMedia(fn);       //新建一个newMedia对象
     wmp.currentPlaylist.appendItem(pmedia);  //插入到当前播放列表
   } 
  }
  catch(e){alert("添加音乐文件出错!");}  
}
///添加音乐菜单项 
function nMenu1OnClick()
{
  addMusciFile();
}
//显示播放列表窗体菜单项 
function nMenu7OnClick()
{
  ShowSettingEx(2);
}

function timer1OnTimer()
{
  try
    {
      Print("State:"+wmp.playState);
      if(wmp==null){
       timer1.enabled=false;
       return;
      }
      if(wmp.currentPlaylist.count<=0) return;
      switch(wmp.playState)
      {
         case 3: case 7:
           getInfo();  
           Print("获取歌曲信息"+wmp.settings.getMode("loop"));  
           setbtnPlay(3);
           break;
         case 10:
           timer1.enabled=false;
           setbtnPlay(1); 
           break;
         case 0: case 1:
           setbtnPlay(1);
           timer1.enabled=false;
           break;
      }
    }
    catch(e)
    {
      Print(e);
    }
}



  /*wmp.playState                         //播放机状态,共有1－10个状态,具体如下
    wmp.playState==1                 //停止播放
    wmp.playState==2                 //暂停播放
    wmp.playState==3                 //正常播放
    wmp.playState==4                 //向前搜索
    wmp.playState==5                 //向后搜索
    wmp.playState==6                 //缓冲处理
    wmp.playState==7                 //等待反应
    wmp.playState==8                 //播放完毕
    wmp.playState==9                 //连接媒体
    wmp.playState==10               //准备就绪  */

function widgetOnLeave()
{
  imgVol.Visible=false;
  barVol.Visible=false;
}

function widgetStyle(sty)
{
  LockRefresh;
  var s=""+sty; 
  switch(s){
    case "0":
        widgetHeight=185;    //full
        btnPlay.Top=138;
        btnNext.Top=145;
        btnPrev.Top=145;
        bg.Visible=true;
        imgAlbum.Visible=true;
        WriteIni("style",0);
        txtAuthor.Visible=true;
        txtZhuanji.Visible=true; 
        break;
    case "1":
        widgetHeight=130;    //small
        btnPlay.Top=138;
        btnNext.Top=145;
        btnPrev.Top=145;
        bg.Visible=true;
        imgAlbum.Visible=true;
        WriteIni("style",1);
        txtAuthor.Visible=true;
        txtZhuanji.Visible=true;
        break;
    case "2":
        widgetHeight=50;     //mini
        btnPlay.Top=0;
        btnNext.Top=7;
        btnPrev.Top=7;
        bg.Visible=false;
        imgAlbum.Visible=false;
        WriteIni("style",2);        
        txtAuthor.Visible=false;
        txtZhuanji.Visible=false;
        break;
  }
  UnLockRefresh;
}
function widgetOnDBClick()
{
  sty++;
  if(sty>2)sty=0;
  widgetStyle(sty);
}

function nMenu8OnClick()
{
  sty++;
  if(sty>2)sty=0;
  widgetStyle(sty); 
  Print(sty);
}

function OnAllSettingsSaved()
{
  SaveAllOption;
  try{
   //设置随机播放 
   if(GetSetting("valRnd")==1)
     wmp.settings.setMode("shuffle", true);
   else
     wmp.settings.setMode("shuffle", false);
   //设置重复播放  
   Print("loop:"+GetSetting("valRep")); 
   if(GetSetting("valRep")==1)
     wmp.settings.setMode("loop",true);
   else
     wmp.settings.setMode("loop",false);
    
   //获取背景设置 
    nbg=GetSettingText("valBg"); 
    if(nbg=="")nbg="noalbumart.png";
    imgAlbum.Src=nbg;   
  }
  catch(e)
  {
    Print("调整播放器随机播放模式失败"); 
  }
}