var url,rss,link;
var iPageIndex=1;  


//将rss条目显示出来 
function showRssItems(aPageIndex)
{
  //Print(aPageIndex );
  if(rss==null)
  {
    Print( "读取RSS失败" );
    return;
  } 
  
  try
  {
    var elist = rss.getElementsByTagName('item');
    if(elist.length < 1)
      return;
  }
  catch(e)
  {
    return;
  }
  
  LockRefresh;
  
  clearRssText() ;
  
  iPageIndex =aPageIndex;

  try 
  {
    txtItem.caption = elist[iPageIndex].selectSingleNode('title').text;
    txtItem.tooltip = txtItem.caption  + '\n点击查看全文...';
  }
  catch(exception){  clearRssText() ; }  
    
  try 
  {
    txtDes.caption = guolvHtml( elist[iPageIndex].selectSingleNode('description').text );
    txtDes.tooltip = txtItem.caption+ '\n双击查看下一条...';
  }
  catch(exception){  clearRssText() ; }    
  
  try 
  {
    link = elist[iPageIndex].selectSingleNode('link').text ;
  }
  catch(exception){  link = ''; }    
  
  

  UnLockRefresh;


}



function widgetOnDock()
{
  LockRefresh
  
  bg.Src = 'bg.png';
   
  txtItem.fontSize = 12;
  txtItem.hotFontSize = 12; 
  txtItem.left = 10;
  txtItem.top = 32;
  txtItem.width = 120;
  txtItem.height = 14;
  
  txtDes.fontSize = 10; 
  txtDes.left = 10;
  txtDes.top = 48;
  txtDes.width = 120;
  txtDes.height = 49;
  
  widgetWidth = bg.Width;
  widgetHeight = bg.Height;
   
  
  UnLockRefresh;    
}

function widgetOnUnDock()
{
  LockRefresh
  
  bg.Src = 'bg2.png'; 
   
  txtItem.fontSize = 14; 
  txtItem.hotFontSize = 14; 
  txtItem.left = 10;
  txtItem.top = 32;
  txtItem.width = 180;
  txtItem.height = 14;
 
 
  txtDes.fontSize = 12;
  txtDes.left = 10;
  txtDes.top = 48;
  txtDes.width = 185;
  txtDes.height = 93; 
 
  widgetWidth = bg.Width;
  widgetHeight = bg.Height;
   
  
  UnLockRefresh; 
 
  
}

function tmRefreshOnTimer()
{
   DownLoadFileAsync( url , 'last.xml' , "onDownFile" );
}

function  onDownFile(fileName,errorCode)
{
  if(errorCode<1)
  {
  } 
  rss.load(fileName);
  tmPageOnTimer();
  //showRssItems(1);  
}


/******************************************************************************* 
Object CreateXMLParser(void) 
创建尽可能高版本的XMLDOM解析器 
********************************************************************************/ 
function CreateXMLParser() 
{ 
  try{ 
    return new ActiveXObject('MSXML2.DOMDocument.4.0'); 
  }catch(e){ 
  try{ 
    return new ActiveXObject('MSXML2.DOMDocument.3.0'); 
  }catch(e){ 
    try{ 
      return new ActiveXObject('MSXML2.DOMDocument.2.6'); 
  }catch(e){ 
  try{ 
    return new ActiveXObject('MSXML2.DOMDocument'); 
  }catch(e){ 
    try{ 
      return new ActiveXObject('Microsoft.XMLDOM'); 
  }catch(e){ 
  return null; 
  } 
  } 
  } 
  } 
  } 
} 


function widgetOnInit()
{
  rss = CreateXMLParser() ;//new ActiveXObject("Msxml2.DOMDocument");
    
  OnAllSettingsSaved();  

  try
  {
    if(fileExists( widgetPath + "last.xml" ))
      rss.load( widgetPath + "last.xml");
  }
  catch(e)
  {
  }  
  tmPageOnTimer();
  //showRssItems(iPageIndex);
  
  refreshRSS();    
}

function refreshRSS()
{
  tmRefreshOnTimer();
}

function OnAllSettingsSaved()
{
  url = GetSetting('url');
  tmRefresh.interval = parseInt(GetSetting('refreshtm')) * 60 * 60 * 1000;
  tmPage.interval = parseInt(GetSetting('pagetm')) * 1000;
}

function clearRssText() 
{
  txtItem.caption = "";
  txtDes.caption = "";
}

function widgetOnClose()
{
  try
  {
    rss = null;
  }
  catch(e){ } 
}

function tmPageOnTimer()
{
  var index = iPageIndex + 1;
  var count = getRssPageCount();
  if(count==0)
    return;

  if(GetSetting('random')=='1')
  {
    index = Math.round(Math.random()*count) - 1;
    //防止重复 
    if(index==iPageIndex)
      index = Math.round(Math.random()*count) - 1;
    //防止重复 
    if(index==iPageIndex)
      index = Math.round(Math.random()*count) - 1;
  }
  
  if((index>count)||(index<0))
    index = 1;
    
  showRssItems(index);
}

function getRssPageCount()
{
  try
  {
    var count = rss.getElementsByTagName('item').length;
    return  count;
  }
  catch(e)
  {
    return 0; 
  }
}

function guolvHtml(str)
{ 
  str = str.replace(/\n/g, "");
  return  str.replace(/<.*?>/g,"");  
}

function txtItemOnMouseDown()
{
  OpenLink( link );
}