String.prototype.trim= function(){return this.replace(/(^\s*)|(\s*$)/g, "");}
String.prototype.trimFilter=function(){var s=this.replace(/<br>/ig,"\r\n  "),s=s.replace(/&nbsp;/ig," "),s=s.trim();return s;}
function  getFileExt(sfile){var reg=/([^\.]+)$/.exec(sfile);return RegExp.$1;} 
function createXmlObj() 
{ 
  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; 
  } 
  } 
  } 
  } 
  } 
}
var itemArr=[],indexArr=[],newsArr=[],hardArr=[],softArr=[],numberArr=[],shunArr=[],subjectArr=[],adArr=[];
var viewPicArr=[view_ad1,view_ad2,view_ad3,view_ad4,view_ad5];
var curArticle=0,curItem=0;
var titleArr=[title1,title2,title3,title4,title5,title6];
var dockTitleArr=[small1,small2,small3,small4,small5,small6,small7]; 
var dockItemArr=[];
function loadItem(p,isTemp){
  LockRefresh;
  var xmlObj = createXmlObj() ;
  if(fileExists( p )){      
    try{      
      xmlObj.load( p );      
      dockItemArr=xmlObj.documentElement.getElementsByTagName("item");
      if(dockItemArr.length>0){
        displayItemList(); 
        displayItemInfo();
        if(isTemp){copyFile(p, widgetPath+'index.xml');}         
      }else{
        dockItemArr=[];      
      }
    }catch(e){
      dockItemArr=[];
    }
  }else{
    DownLoadFileAsync('http://ep.cbifamily.com/yuyu/index.xml' , widgetPath+'temp.xml' ,"onDownMainXml" );
  }
}
function displayItemList(num){
  LockRefresh;
  if(dockItemArr.length>0){
    var itm= num || 0;
    var len=dockItemArr.length;
    for(i=0;i<7;i++){     
      dockTitleArr[i].bgColor='';    
      if(itm+i<len){
        dockTitleArr[i].caption = dockItemArr[itm+i].selectSingleNode("title").text.trim();
        dockTitleArr[i].tag=itm+i;
        dockTitleArr[i].tagStr=dockItemArr[itm+i].selectSingleNode("source").text.trim();
        dockTitleArr[i].Visible=true;
      }else{
        dockTitleArr[i].Visible=false;
      }
    }
    small1.bgColor='78,137,194';     
  }
  UnLockRefresh;
}
function displayItemInfo(num){
  LockRefresh;
  var itm= num || 0;
  try{
    if(dockItemArr.length>0 && itm<dockItemArr.length){
      smallTitle.tagStr=dockItemArr[itm].selectSingleNode("link").text.trim();
      smallTitle.caption=dockItemArr[itm].selectSingleNode("title").text.trim();
      editionCount.caption=dockItemArr[itm].selectSingleNode("description").text.trim();
      smallPubDate.caption=dockItemArr[itm].selectSingleNode("pubDate").text.trim();    
      var reNum=parseFloat(dockItemArr[itm].selectSingleNode("urindex").text.trim());
      progressBar0.percent=reNum*100;
      recommendNum.caption=reNum*10;
      curItem=itm;
      var picUrl=dockItemArr[itm].selectSingleNode("thumbnail").text.trim();
      var picExt = getFileExt(picUrl).toLowerCase();
      if(picExt!='jpg' && picExt!='bmp' && picExt!='gif' && picExt!='png' && picExt!='jpeg')picExt='jpg';
      var picPath=widgetPath+'pic\\' + MD5(picUrl)+'.'+picExt;
      if(fileExists(picPath )){
        smallView.Src = picPath;
      }else{
        smallView.tagStr=picPath;
        DownLoadFileAsync( picUrl , picPath ,'displaySmallThumbNail' ); 
      }             
    }else{
      Print('数组溢出');
      dockItemArr=[];    
    }
  }catch(e){
    Print('数组溢出');
    dockItemArr=[];
  }
  UnLockRefresh;
}
function displaySmallThumbNail(){
  LockRefresh;
  smallView.Src= smallView.tagStr;
  UnLockRefresh;
}
function prevItem(){
  curItem--;
  if(dockItemArr.length>0 && curItem>=0){
    if(small1.bgColor){
      displayItemList(curItem);
    }else{
      LockRefresh;    
      for(i=0;i<6;i++){
        if(dockTitleArr[i].bgColor){
          dockTitleArr[i].bgColor='';
          dockTitleArr[i-1].bgColor='78,137,194';
          break;
        }
      }
      UnLockRefresh;          
    }
    displayItemInfo(curItem);    
  }else{
    if(curItem<0 && dockItemArr.length>0){
      showTrayHint("鱼鱼温馨提示:","已经浏览完了一遍");
      curItem=dockItemArr.length;
      small1.bgColor='78,137,194';
      prevItem();
    }     
  }  
}
function nextItem(){
  curItem++;
  if(dockItemArr.length>0 && curItem<dockItemArr.length){
    if(small7.bgColor){
      displayItemList(curItem);
    }else{
      LockRefresh;    
      for(i=0;i<6;i++){
        if(dockTitleArr[i].bgColor){
          dockTitleArr[i].bgColor='';
          dockTitleArr[i+1].bgColor='78,137,194';
          break;
        }
      }
      UnLockRefresh;          
    }
    displayItemInfo(curItem);    
  }else{
    if(curItem>=dockItemArr.length){
      showTrayHint("鱼鱼温馨提示:","已经浏览完了一遍");
      curItem=-1;
      small7.bgColor='78,137,194';
      nextItem();
    }     
  }
}

function smallOnMouseDown(sender)
{
  for(i=0;i<7;i++){dockTitleArr[i].bgColor='';} 
  sender.bgColor='78,137,194';
  displayItemInfo(sender.tag);
}

function readDetailInfo(sender){
  var xmlUrl=dockItemArr[curItem].selectSingleNode("source").text.trim();
  var path= widgetPath+'data\\'+MD5(xmlUrl)+'.xml'; 
  if( fileExists( path )){
    pageIndex=2;
    loadXml(path);
  }else{
    DownLoadFileAsync( xmlUrl , path ,'onDownXmlFile');
  }  
}
 
function onDownXmlFile(s){
  if( fileExists( s )){
    pageIndex=2;  
    loadXml(s);
  }  
}

function loadXml(p){
  LockRefresh;
  var xmlObj = createXmlObj() ;
  if(fileExists( p )){      
    try{      
      xmlObj.load( p );      
      Print( p );
      itemArr=xmlObj.documentElement.getElementsByTagName("item");
      Print('dd');
      var tempPart;
      for(i=0;i<itemArr.length;i++){
        tempPart=itemArr[i].selectSingleNode("part").text.trim();
        switch(tempPart){
          case "电子广告":adArr.push(i);break;
          case "硬件广场":hardArr.push(i);break;
          case "新闻中心":newsArr.push(i);break;
          case "专题策划":subjectArr.push(i);break;
          case "软件网络":softArr.push(i);break;
          case "数字家庭":numberArr.push(i);break;
          case "顺江苑":shunArr.push(i);break;                                                        
          default:
            Print('未知类型');
        }
      }          
      bindNavTag();       
      displayTitleList();              
      displayInfo();
    }catch(e){
      Print('数据异常!');
      clearArr();
      UnLockRefresh;
      return false;    
    }
  }
  UnLockRefresh;
}
function clearArr(){
  itemArr=[],indexArr=[],newsArr=[],hardArr=[],softArr=[],numberArr=[],shunArr=[],subjectArr=[],adArr=[];
}
function displayTitleList(num){
  LockRefresh;
  if(itemArr.length>0){
    var itm= num || 0;
    var len=itemArr.length;
    for(i=0;i<6;i++){
      titleArr[i].bgColor='';    
      if(itm+i<len){
        titleArr[i].caption = itemArr[itm+i].selectSingleNode("title").text.trim();
        titleArr[i].tag=itm+i;
        titleArr[i].Visible=true;
      }else{
        titleArr[i].Visible=false;
      }
    }
    title1.bgColor='78,137,194';  
  }
  UnLockRefresh;
}
function bindNavTag(){
  nav_index.tag=0;
  nav_news.tag=newsArr[0] || 0;
  nav_hardware.tag=hardArr[0] || 0;
  nav_soft.tag=softArr[0] || 0;
  nav_number.tag=numberArr[0] || 0;
  nav_shun.tag=shunArr[0] || 0;
  nav_subject.tag=subjectArr[0] || 0;
  nav_ad.tag=adArr[0] || 0;
}
function selectNav(str){
  nav_index.fontColor='$0023597E';
  nav_news.fontColor='$0023597E';
  nav_hardware.fontColor='$0023597E';
  nav_soft.fontColor='$0023597E';
  nav_number.fontColor='$0023597E';
  nav_shun.fontColor='$0023597E';
  nav_subject.fontColor='$0023597E';
  nav_ad.fontColor='$0023597E';           
  switch(str){
    case "电子广告":nav_ad.fontColor='$009F5400';break;
    case "硬件广场":nav_hardware.fontColor='$009F5400';break;
    case "新闻中心":nav_news.fontColor='$009F5400';break;
    case "专题策划":nav_subject.fontColor='$009F5400';break;
    case "软件网络":nav_soft.fontColor='$009F5400';break;
    case "数字家庭":nav_number.fontColor='$009F5400';break;
    case "顺江苑":nav_shun.fontColor='$009F5400';break;                                                        
    default:
      nav_index.fontColor='$009F5400';
    }
}
function displayInfo(num){
  var itm= num || 0;
  try{
    if(itemArr.length>0 && itm<itemArr.length){
      LockRefresh;
      for(i=0;i<viewPicArr.length;i++){
        viewPicArr[i].Visible=false;
      }
      selectNav(itemArr[itm].selectSingleNode("part").text.trim());
      if(itm==0)nav_index.fontColor='$009F5400';      
      article_title.caption = itemArr[itm].selectSingleNode("title").text.trim();
      article_title.tooltip = itemArr[itm].selectSingleNode("title").text.trim();
      article_content.caption = itemArr[itm].selectSingleNode("content").text.trimFilter();
      author_value.caption =  itemArr[itm].selectSingleNode("author").text.trim();
      readnum_value.caption = itemArr[itm].selectSingleNode("readTimes").text.trim();                       
      term_description.caption = smallTitle.caption+" "+itemArr[itm].selectSingleNode("source").text.trim();
      read_more.tagStr = itemArr[itm].selectSingleNode("moreUrl").text.trim();
      totalUrl = itemArr[itm].selectSingleNode("totalUrl").text.trim(); 
      var viewPic = itemArr[itm].selectSingleNode("thumbnail").text.trim();
      if(viewPic)checkThumbPic(viewPic);
      var relativePic = itemArr[itm].selectSingleNode("relativePic").text.trim();
      if(relativePic)checkRelativePic(relativePic);     
      curArticle=itm;       
      UnLockRefresh;
      HtmlPanelUrl.Visible=false;
      HtmlPanelUrl=totalUrl;
      HtmlPanelVisible=true;
    }else{
      Print('数组溢出');
      clearArr();
    }  
  }catch(e){
    Print('数组为空');
    clearArr();
  }
}
function checkThumbPic(u){
  var picExt = getFileExt(u).toLowerCase();
  if(picExt!='jpg' && picExt!='bmp' && picExt!='gif' && picExt!='png' && picExt!='jpeg')picExt='jpg';
  var picPath=widgetPath+'pic\\thumbpic\\' + MD5( u)+'.'+picExt;
  if(fileExists( picPath )){
    view.Src = picPath;
  }else{
    view.tagStr=picPath;
    DownLoadFileAsync( u , picPath ,'displayThumbNail' ); 
  }
}                           
function checkRelativePic(list){
  var picUrlArr=list.split(',');
  var len= (picUrlArr.length>=5)?5:picUrlArr.length;
  for(i=0;i<len;i++){
    var picExt = getFileExt(picUrlArr[i]).toLowerCase();
    if(picExt!='jpg' && picExt!='bmp' && picExt!='gif' && picExt!='png' && picExt!='jpeg')picExt='jpg';
    var picPath=widgetPath+'pic\\relative\\'+MD5( picUrlArr[i])+i+'.'+picExt;
    viewPicArr[i].tagStr=picPath;
    viewPicArr[i].tag=i;
    if(fileExists( picPath )){
      viewPicArr[i].Src=picPath;
      viewPicArr[i].Visible=true;
    }else{
      DownLoadFileAsync( picUrlArr[i] , picPath ,'displayRelativePic' ); 
    }
  }
}

function displayThumbNail(){
  LockRefresh;
  view.Src=view.tagStr;
  UnLockRefresh;
}

function displayRelativePic(p){
  LockRefresh;
  var pos=parseInt(p.substr(parseInt(p.lastIndexOf('.')-1),1));
  if(pos>=0 && pos<5){
    viewPicArr[pos].Src=viewPicArr[pos].tagStr;
    viewPicArr[pos].Visible=true;
  }
  UnLockRefresh;
}
function nextArticle(){
  curArticle++;
  if(itemArr.length>0 && curArticle<itemArr.length){
    if(title6.bgColor){
      displayTitleList(curArticle);
    }else{
      LockRefresh;    
      for(i=0;i<5;i++){
        if(titleArr[i].bgColor){
          titleArr[i].bgColor='';
          titleArr[i+1].bgColor='78,137,194';
          break;
        }
      }
      UnLockRefresh;          
    }
    displayInfo(curArticle);    
  }else{
    if(curArticle>=itemArr.length){
      showTrayHint('鱼鱼温馨提示:','已经浏览完一遍');
      curArticle=-1;
      title6.bgColor='78,137,194';
      nextArticle(); 
    }
  }
}
function prevArticle(){
  curArticle--;
  if(itemArr.length>0 && curArticle>=0){
    if(title1.bgColor){
      displayTitleList(curArticle);
    }else{
      LockRefresh;    
      for(i=0;i<5;i++){
        if(titleArr[i].bgColor){
          titleArr[i].bgColor='';
          titleArr[i-1].bgColor='78,137,194';
          break;
        }
      }
      UnLockRefresh;          
    }
    displayInfo(curArticle);    
  }else{
    if(itemArr.length>0 && curArticle<0){
      showTrayHint('鱼鱼温馨提示:','已经浏览完一遍');
      curArticle=itemArr.length;
      title1.bgColor='78,137,194';
      prevArticle(); 
    }
  }
}
function button0OnClick(sender)
{
  loadXml();
}

function view_adOnMouseEnter(sender)
{
  if(fileExists( sender.tagStr )){
    LockRefresh;
    largePanel.Src= sender.tagStr;
    largePanel.Visible=true;
    UnLockRefresh;
  }  
}

function view_adOnMouseLeave(sender)
{
  largePanel.Visible=false;
}

function read_moreOnMouseDown(sender)
{
  if(sender.tagStr)OpenLink( sender.tagStr );
}

function viewOnMouseDown(sender)
{
  nextArticle();
}

function titleOnMouseDown(sender)
{
  LockRefresh;
  for(i=0;i<6;i++){
    titleArr[i].bgColor='';
  }
  sender.bgColor='78,137,194';
  displayInfo(sender.tag);
  UnLockRefresh;
  Refresh;  
}

function navOnMouseDown(sender)
{
  displayTitleList(sender.tag);
  displayInfo(sender.tag);
}

function enlargeOnMouseDown(sender)
{
  pageIndex=1;
}
function widgetOnLeave()
{
  largePanel.Visible=false; 
}

function widgetOnInit()
{
  pageIndex=1;
  loadItem(widgetPath+'index.xml');
}

function fresh(){
  if(pageIndex==1){
    DownLoadFileAsync('http://ep.cbifamily.com/yuyu/index.xml' , widgetPath+'temp.xml' ,"onDownMainXml" );
  }else{
    if(dockItemArr.length>0 && curItem<dockItemArr.length){
      var xmlUrl=dockItemArr[curItem].selectSingleNode("source").text.trim();
      var path= widgetPath+'data\\'+MD5(xmlUrl)+'.xml';      
      DownLoadFileAsync(xmlUrl ,path ,"onDownXmlFile" );    
    }
  }
}
function onDownMainXml(p){
  if(fileExists(p)){
    loadItem(p,true);
  }
}

function view_adOnPopupMenu(sender)
{
  if(sender.tag>=0 && sender.tag<5){
    custPopMenu.clear;
    custPopMenu.addMenuItem('重新加载','reLoadPic',sender.tag);
    custPopMenu.popup;  
  }
}

function reLoadPic(pos){
  var list=itemArr[curArticle].selectSingleNode("relativePic").text.trim();
  var picUrlArr=list.split(',');
  var len= (picUrlArr.length>=5)?5:picUrlArr.length;
  if(pos.tag<len){
    DownLoadFileAsync(picUrlArr[pos.tag], viewPicArr[pos.tag].tagStr ,'displayRelativePic' );   
  }
}

function smallViewOnMouseDown(sender)
{ 
  if(smallTitle.tagStr){OpenLink( smallTitle.tagStr );}else{ OpenLink('http://www.fishdesk.com/')}
}

function btMinOnMouseDown(sender)
{
  OpenFile( xdeskshowPath+'widgets\\{1939F8E6-194C-45DF-A934-107D39D56460}\\XWidget.xds');
  closeWidget();
}