var wmp;
var oldWmpStat;


function WidgetOnStart()
{
  try   
  {           
    wmp = new ActiveXObject('WMPlayer.OCX');   
  }
  catch(e)   
  { 
    alert('请安装微软媒体播放器9.0或以上版本(Windows Media Player 9)!');
    return;   
  }   
  wmp.settings.autoStart = false;
  wmp.settings.setMode("loop", true);
  wmp.URL = GetSetting('radioUrl');// "mms://real.rednet.cn/1061";
  var   volume=GetSetting("volume");
  if(volume=="") 
    volume = "80";   
  wmp.settings.volume = parseInt(volume);
  if(GetSetting( "autoopen" )==1) 
    wmp.controls.play();
  btDown.tooltip = "音量减(" + wmp.settings.volume + "%)";
  btUp.tooltip = "音量加(" + wmp.settings.volume + "%)";
  	
}

function WidgetOnClose()
{
  try
  {
    wmp.controls.stop();
    wmp = null;  
  }
  catch(e) 
  {
  }
  
}



function nMenu1OnClick()
{
  wmp.URL = GetSetting('radioUrl');// "mms://real.rednet.cn/1061";
  wmp.controls.play();   
}

function nMenu2OnClick()
{
  wmp.controls.stop();  
}

function OnAllSettingsSaved()
{
  SaveAllOption; 
  var volume=GetSetting("volume");
  if(volume=="") 
    volume = "80";   
  wmp.settings.volume = parseInt(volume);

  btDown.tooltip = "音量减(" + wmp.settings.volume + "%)";
  btUp.tooltip = "音量加(" + wmp.settings.volume + "%)";


  //如果没有更改频道就不需要更新 
  if(wmp.URL==GetSetting('radioUrl'))
    return; 
  wmp.controls.stop();  
  wmp.URL = GetSetting('radioUrl');
  wmp.controls.play();  
}

function WidgetOnReLoad()
{
  try
  {
    wmp.controls.stop();
    wmp = null;     
  }
  catch(e){}
}


function WidgetOnDBClick()
{
  timer0.enabled = !timer0.enabled;
  info.caption = ''; 
  Refresh;
}

function RadioOn()
{
  bg.Src = 'res\\radio3_on.png';
  button.hotImgSrc = 'stop.png';
  button.tooltip = "停止收音"; 
} 

function RadioOff()
{
  bg.Src = 'res\\radio3_off.png';
  button.hotImgSrc = 'start.png';
  button.tooltip = "开始收音"; 
} 

function timer0OnTimer()
{
  if(wmp==null)
    return;
    
  if(oldWmpStat!=wmp.status)            
  {
//    var cm = wmp.currentMedia;
    oldWmpStat = wmp.status;
    var ps = wmp.playState;
//    if(ps==3)
//    {
//      if(cm.getItemInfo("Author"))
//      { 
//        dt=cm.getItemInfo("Author")+" - "+cm.getItemInfo("Title");
//      }
//      else
//      { 
//        dt=cm.getItemInfo("Title");
//      }
//    }
//    else
//    { 
//      dt= wmp.status;
//    }    
//    if(dt!=null) 
//        info.caption = dt;
  
    var str
    if(ps==3)
    {
      str = GetSettingText('radioUrl');
    }
    else
      str = wmp.status;
    
    if(str!=null) 
        info.caption = str;
        
    switch (ps) 
    {
     case 1:
      RadioOff();
      break;
     case 2:
      RadioOff();
      break;
     case 9:
      RadioOff();
      break;
     case 10:
      RadioOff();
      break;
     case 3:
      RadioOn();
      break;
     case 6:
      RadioOn();
      break;

     default: 
      RadioOff();
    }     
    
    Refresh;
    
  }  
}



function stopOnClick()
{
  if((wmp.playState==3)||(wmp.playState==6)||(wmp.playState==9))
  {
    button.hotImgSrc = 'start.png';
    button.tooltip = "开始收音"; 
    wmp.controls.stop();
  }
  else
  {
    button.hotImgSrc = 'stop.png';
    button.tooltip = "停止收音"; 
    wmp.controls.play(); 
  }
}


function btDownOnClick()
{
  if((wmp.playState==3)||(wmp.playState==6)||(wmp.playState==9))
  {
    try
    {
      wmp.settings.volume = wmp.settings.volume - 10;
      SetSetting("volume",wmp.settings.volume);
      btDown.tooltip = "音量减(" + wmp.settings.volume + "%)";
      print(wmp.settings.volume); 
    }
    catch(e)
    {
    }
  }
 
}

function btUpOnClick()
{
  if((wmp.playState==3)||(wmp.playState==6)||(wmp.playState==9))
  {
    try
    {
      wmp.settings.volume = wmp.settings.volume + 10;
      SetSetting("volume",wmp.settings.volume);
      btUp.tooltip = "音量加(" + wmp.settings.volume + "%)";
      print(wmp.settings.volume); 
    }
    catch(e)
    {
    }
  }
  
}