﻿/****************************************/
/* Blogger latest comments builder V1.1*/
/* www.enjoyitsimply.com                */
/* enjoyitsimply@gmail.com              */
/****************************************/

document.write('<div id="eis_latest_comments"><p align="center"><img src="http://enjoyitsimplycom.googlepages.com/ajax-loading1.gif"/></p></div>');

var eis_json_comments;
var eis_json_posts;

var eis_feed = '/feeds/comments/summary?alt=json-in-script&callback=eis_show_comments&max-results=' + eis_maxcomments;
var eis_script = document.createElement('script');
eis_script.setAttribute('src', eis_feed);
eis_script.setAttribute('type', 'text/javascript');
document.documentElement.firstChild.appendChild(eis_script);

function eis_show_comments(json) {
	eis_json_comments = json.feed.entry;
	
	var eis_feed="/feeds/posts/summary?alt=json-in-script&orderby=published&callback=eis_comments_handler&max-results=500";
	var eis_script = document.createElement('script');
	eis_script.setAttribute('src', eis_feed);
	eis_script.setAttribute('type', 'text/javascript');
	document.documentElement.firstChild.appendChild(eis_script);
}

function eis_comments_handler(json) {
	eis_json_posts = json.feed.entry;
	eis_get_commends(eis_json_posts);
}

function eis_get_commends(input) {
	var eis_post_title = new Array();
	var temp = '';
	
	if(eis_json_comments.length < eis_maxcomments)
		eis_maxcomments = eis_json_comments.length;

	for (var i=0; i<eis_maxcomments; i++)
	{ 
		eis_post_url = eis_json_comments[i].link[2].href.substring(0, eis_json_comments[i].link[2].href.indexOf('?'));
		eis_post_title[i] = eis_get_commends_title(input, eis_post_url);
	}

	for(var i=0,url,authorname,content,date; i<eis_maxcomments; i++)
	{
		url = eis_json_comments[i].link[2].href;
		authorname = eis_json_comments[i].author[0].name.$t;
		content=eis_json_comments[i].summary.$t;
		date = (eis_is_showdate == 1) ? ' on ' + eis_month_notow(eis_json_comments[i].published.$t.substring(5,7)) + ' ' + eis_json_comments[i].published.$t.substring(8,10) : ''; 
		
		var entry = '<li>' + authorname + ' 回 <a href="' + url + '" title="' + content + '">' + eis_post_title[i] + '</a>' + date + '</li>'; 
		temp = temp + entry;
	}

	document.getElementById('eis_latest_comments').innerHTML = '<ul>' + temp + '</ul>';
}

function eis_get_commends_title(input, eis_post_url)
{
	for(var i=0; i<input.length; i++)
	{
		if(input[i].link[4].href == eis_post_url)
		{
			return input[i].title.$t;
		}
	}
}

function eis_month_notow(input) {
	var month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
	
	return month[(parseInt(input,10)-1)];
}