var pluckCommentBatch = [];

function getCommentCount(articleId) {
	//requests an Article record from Pluck
	var articleRequest = new PluckSDK.ArticleRequest();
	articleRequest.ArticleKey = new PluckSDK.ExternalResourceKey();
	articleRequest.ArticleKey.Key = articleId;
			
	//Add to array batch
	pluckCommentBatch.push(articleRequest);

	//Send Batch if it exceeds 20
	if (pluckCommentBatch.length >= 20) {
		submitPluckBatch();
	}
}
function submitPluckBatch() {
//	if (window.console && window.console.warn) window.console.warn("Pluck batch running " + pluckCommentBatch.length + " requests");
	if (pluckCommentBatch.length >= 1) {
		PluckSDK.SendRequests(pluckCommentBatch, articleCallback);
		pluckCommentBatch = [];
	}
}
function articleCallback(responses) {
//	if (window.console && window.console.warn) window.console.warn("Pluck batch returning " + responses.length + " responses");
	for (var i=0; i<responses.length; i++) {
	//Loop through all the responses in the batch
		//if there were any exceptions...
		if (responses[i].ResponseStatus.StatusCode != PluckSDK.ResponseStatusCode.OK) {
			var commentCount = '0';
		} else {
			if (responses[i].Article != null) {
				//make sure we have an Article object first.
				var myArticle = responses[i].Article;
				var spanId = "commentCount" + myArticle.ArticleKey.Key;
				var commentCount = myArticle.Comments.NumberOfComments;
				if (!(!(document.getElementById(spanId)))) {
					//if element exists - add the comment count
					document.getElementById(spanId).innerHTML = commentCount;
				}
			}
		}
	}
}

function discoverMostPop() {
	if (document.getElementById('most_popular')) {
		var mostPopRequest1 = new PluckSDK.DiscoverContentActionRequest();
		mostPopRequest1.Activity = PluckSDK.DiscoveryActivity.Commented;
		mostPopRequest1.Type = PluckSDK.ContentType.Article;
		mostPopRequest1.Age = 1;
		mostPopRequest1.MaximumNumberOfDiscoveries = 10;
	
		var mostPopRequest2 = new PluckSDK.DiscoverContentActionRequest();
		mostPopRequest2.Activity = PluckSDK.DiscoveryActivity.Commented;
		mostPopRequest2.Type = PluckSDK.ContentType.Article;
		mostPopRequest2.Age = 7;
		mostPopRequest2.MaximumNumberOfDiscoveries = 10;
	
		var mostPopRequest3 = new PluckSDK.DiscoverContentActionRequest();
		mostPopRequest3.Activity = PluckSDK.DiscoveryActivity.Commented;
		mostPopRequest3.Type = PluckSDK.ContentType.Article;
		mostPopRequest3.Age = 30;
		mostPopRequest3.MaximumNumberOfDiscoveries = 10;
	
		PluckSDK.SendRequests([mostPopRequest1, mostPopRequest2, mostPopRequest3], mostPopCallback);
	}
}
function mostPopCallback(responses) {
	for (var i=0; i<3; i++) {
		var iteration = i + 1;
		var mostPopResponse = responses[i];
		if (mostPopResponse.ResponseStatus.StatusCode != PluckSDK.ResponseStatusCode.OK) {
//			if (window.console && window.console.warn) window.console.warn("Unexpected error in discovery request");
		} else {
			printMostPopSummary(mostPopResponse, iteration);
		}
	}
}	
function printMostPopSummary(mostPopResponse, iteration) {
	var items = mostPopResponse.DiscoveredContent;
	var listName = "olMostCommented" + iteration;
	var listSpace = document.getElementById(listName);
	for (var itemIndx = 0; itemIndx < items.length; itemIndx++) {
		var article = items[itemIndx];
		var headline = article.Title.replace(" - Winnipeg Free Press", "");
		var newItem = document.createElement("li");
		newItem.innerHTML = '<a href="' + article.Url + '">' + headline + '</a>';
		listSpace.appendChild(newItem);
	}
}

function getLeaderboardCommenters() {
	if (document.getElementById('most_popular')) {
		var leaderboardRequest = new PluckSDK.LeaderboardRankingsPageRequest();
		leaderboardRequest.LeaderboardKey = new PluckSDK.LeaderboardKey();
		leaderboardRequest.LeaderboardKey.Key = '1bf17b6f-4039-4ccb-8153-c62f23fe1d36';
		leaderboardRequest.OneBasedOnPage = 1;
		leaderboardRequest.ItemsPerPage = 15;
		
		var requests = [];
		requests.push(leaderboardRequest);
	
		PluckSDK.SendRequests(requests, leaderboardCallback);
	}
}
function leaderboardCallback(responses) {
//	if (window.console && window.console.warn) window.console.warn("Pluck leader returning " + responses.length + ' response, ' + responses[0].TotalItems + ' items.');
	var items = responses[0].Items;
	var listSpace = document.getElementById('toppop_comments');
	for (var itemIndx = 0; itemIndx < 10; itemIndx++) {
		var item = items[itemIndx];
		var newItem = document.createElement("li");
		newItem.innerHTML = '<img src="' + item.User.AvatarPhotoUrl + '" width="20" height="20" /> <a href="' + item.User.PersonaUrl + '">' + item.User.DisplayName + '</a>';
		listSpace.appendChild(newItem);
	}
}	

jQuery(document).ready(function() { discoverMostPop(); });
//jQuery(document).ready(function() { getLeaderboardCommenters(); });

