﻿var g_szBlogDomain;
var g_iShowCount ;
if (g_szBlogDomain==undefined){ g_szBlogDomain='twpaddy.blogspot.com';}
if (g_iShowCount==undefined){ g_iShowCount=5;}
document.write('<div id="divrc">下載中...</div>');
var g_szAuthorsLink=new Array();
var g_szTitle= new Array();
var g_szOrgLink = new Array();
var g_iIndex=0; 

function arc_gethref(postlink,relstr)
{
  for (var i=0; i<postlink.length; i++)
  {
      if(postlink[i].rel == relstr)
         return postlink[i].href;
  }
  return "";
}

function a_comprc(a,b)
{    
  order= Date.parse(a.published.$t.replace(/^(\d{4})-(\d{2})-(\d{2})T([0-9:]*)([.0-9]*)(.)(.*)$/,  '$1/$2/$3 $4 GMT')) - Date.parse(b.published.$t.replace(/^(\d{4})-(\d{2})-(\d{2})T([0-9:]*)([.0-9]*)(.)(.*)$/,  '$1/$2/$3 $4 GMT'));
  return 0-order;
}
function a_rc(json) 
{ 
  g_szComments = json.feed.entry.sort(a_comprc);
}
function a_FindTitle(posts,orgLink)
{
  var ret= orgLink;
  for (var ii=0,post; post= posts.feed.entry[ii]; ii++) 
  {
    var hrefstr=arc_gethref(post.link,"alternate");
    if (hrefstr == orgLink)
    {
      ret= post.title.$t;
      break;
    }
  }    
  return ret;
}
function a_FindIndex(posts,orgLink)
{
  var ij=0;  
  for (ij=0; ij < g_iIndex; ij++)
  {    
    if (orgLink==g_szOrgLink[ij])        
       return ij;
  }
  g_szOrgLink[ij]=orgLink;
  g_szTitle[ij]= a_FindTitle(posts,orgLink);
  g_szAuthorsLink[ij]='◎';
  g_iIndex ++;
  return ij;
}
function b_rc(posts)
{
	var temp = "";
  for (var i=0,comment; comment = g_szComments[i], i < g_iShowCount; i++)  
  {
    var content = comment.title.$t;
    var link = arc_gethref(comment.link,"alternate");
    var orgLink= link;
    var link2 = link; 
    var iFind = link.indexOf('?');
    if (iFind<0)
        iFind = link.indexOf('#'); 
    if (iFind>0)
      orgLink = link.substring(0,iFind);
    var index = a_FindIndex(posts,orgLink);
    if (index>= g_iShowCount)
    {
       g_iIndex--;
       break;
    }
    var auther= comment.author[0].name.$t;
    var timestamp=comment.published.$t.substr(0,10) + ' ' +comment.published.$t.substr(11,5);
    g_szAuthorsLink[index] += '<a href="' + link2 + '" title="' + timestamp + '：' + content + '">' + auther + '</a> ? ';
	
	var auther= comment.author[0].name.$t;
	var timestamp=comment.published.$t.substr(0,10) + ' ' +comment.published.$t.substr(11,5);	
	temp += '<div id="recent-comment-body" >';
	temp += '<div id="recent-comment-auther">' + auther + ':</div>' + '<div id="recent-comment-content" ><a href="' + link2 + '" title="' + timestamp + '：' + content + '">' + content.substring(0,62) +'</a></div>';
	temp += '<div id="recent-comment-re" >Re: <a href="' + g_szOrgLink[index] + '">' + g_szTitle[index].substring(0,10)  + '</a> </div>';
	temp += '</div>';
  }

  
  document.getElementById("divrc").innerHTML = temp;
}
document.write('<script src="http://' + g_szBlogDomain + '/feeds/comments/summary?alt=json-in-script&callback=a_rc"></script><script src="http://' + g_szBlogDomain + '/feeds/posts/summary?alt=json-in-script&callback=b_rc&max-results=99"></script>');