/**
 * @fileoverview Blog-specific scripts.  Set in the "sbNaHeadIncludes" global template.
 */

/** Sets up an array of blog ids we'll later get multiple comment counts with */
var gBlogIds = new Array();

/**
 * Gets comment counts for multiple articles in one Pluck request.  Used on the blogs to fetch
 * counts for all the displayed posts.
 * @param {Number[]} articleIds a list of story ids to fetch comments for
 */
function getMultipleCommentCounts (articleIds) {
	if (articleIds.length) {
		var requestBatch = new RequestBatch();

		for (var x = 0; x < articleIds.length; x++) {
			requestBatch.AddToRequest(new ArticleKey('sacbee.com_' + articleIds[x]));
		}

		requestBatch.BeginRequest(serverUrl,
			function (responseBatch) {
				if (responseBatch.Responses.length == 0) {
					// No articles returned and thus no comments.  Possible case.
					//console.log("No articles returned.");
				} else {
					for (var x = 0; x < responseBatch.Responses.length; x++) {
						var article = responseBatch.Responses[x].Article;

						var article_id = article.ArticleKey.Key.replace(document.domain + '_', '');
						var comment_num = article.Comments.NumberOfComments;

						jQuery('#cn_' + article_id + ' span').html(comment_num);
					}
				}
			}
		);
	}
}

jQuery(document).ready(function () {
	getMultipleCommentCounts(gBlogIds);
});