function getCommentCount(articleKey, enabled, lblID, storyToolID)
{
	//alert(serverUrl);
	var requestBatch = new RequestBatch();
	requestBatch.AddToRequest(new ArticleKey(articleKey));
	requestBatch.BeginRequest(pluckServerUrl, respHandler);
	function respHandler(responseBatch){
		if(responseBatch.Responses.length > 0)
		{
			setCommentHeading(lblID, storyToolID, responseBatch.Responses[0].Article.Comments.NumberOfComments, enabled);
		}
		else
		{
			//comments on this article don't exist, set commments to 0
			setCommentHeading(lblID,storyToolID, 0, enabled);
		}

	}
}

function setCommentHeading(lblid, liid, count, enabled)
{
	var lbl = jQuery('#'+lblid);
	var li = jQuery('#'+liid);

	var lblhtml = '';
	var lihtml = '';

	if(count > 0)
	{
		lblhtml = "<a href=\"javascript:jumpToAnchor('#Comments')\">Comments (" + count + ")</a>";
		lihtml = "<a href=\"javascript:jumpToAnchor('#Comments')\"><span>Comments (" + count + ")</span></a>";
	} else if(enabled) {
		lblhtml = "<a href=\"javascript:jumpToAnchor('#Comments')\">Be the first to post a comment</a>";
		lihtml = "<a href=\"javascript:jumpToAnchor('#Comments')\"><span>Comments (0)</span></a>";
	}

	lbl.html(lblhtml);

	if(li != null && lihtml.length > 0)
	{
		li.html(lihtml);
		li.show();
	}
}



function setMostCommented(div, max, age, sections, debug)
{
    
		if(sections == undefined)
			sections = '';
        
		if(debug == undefined)
			debug = false;

		var requestBatch =  new RequestBatch();


		var searchSections = new Array();
		//add section filter if exists
		if(sections.length > 0)
		{
			sec = sections.split("|");
			for(x = 0; x < sec.length; x ++)
				searchSections[x] = new Section(sec[x]);
		}

        var searchCategories = new Array();
        var activityDisco = new Activity("Commented");
        var contentTypeDisco = new ContentType("Article");
        var limitToContributorsDisco =  new Array();
        requestBatch.AddToRequest(new DiscoverContentAction(searchSections,  searchCategories, limitToContributorsDisco, activityDisco,  contentTypeDisco, age, max, "true"));
         var  processDiscoveryResponse = function(responseBatch)
        {
		   if(debug)
			   console.dir(responseBatch);
           if (responseBatch.Messages[0].Message == "ok")
           {
             discoveredContent  = responseBatch.Responses[0].DiscoverContentAction.DiscoveredContent;
             discoveryHTML  = '<ul class="bullet">';

			 var comment_count = responseBatch.Responses[0].DiscoverContentAction.DiscoveredContent.length;

             // display loop
             for (i = 0; i < comment_count; i++) {
				var titletext = discoveredContent[i].PageTitle.split('#');
                discoveryHTML += "<li><a href=\"" + discoveredContent[i].PageUrl + "\"><span>" + titletext[0]  + "</span></a></li>";
             }
			document.getElementById(div).innerHTML =  discoveryHTML + "</ul>";
          }
           
        }
       
       
        requestBatch.BeginRequest(pluckServerUrl, processDiscoveryResponse);

}


var articleKeys = new Array();
var requestsCompleted = 0;
var commentCounts = new Array();
var totalUniqueKeys = 0;

function getCommentCounts()
{
	commentCounts = new Array(articleKeys.length);
	var uniqueArticleKeys = articleKeys.uniqueArr(); //avoiding looking up duplicate keys
	totalUniqueKeys = uniqueArticleKeys.length;
	for (var x = 0; x < totalUniqueKeys; x = x + 20)
	{
		var requestBatch = new RequestBatch();

		for (var y = x; y < x+20; y++)
		{
			if(y == uniqueArticleKeys.length)
				break;
			requestBatch.AddToRequest(new ArticleKey(uniqueArticleKeys[y]));
		}
		requestBatch.BeginRequest(pluckServerUrl, respHandler);
	}
}

function respHandler(responseBatch)
{
	for (var x = 0; x < responseBatch.Responses.length; x++) //for each successful response...
		for (var y = 0; y < articleKeys.length; y++) //check for matching keys...
			if (responseBatch.Responses[x].Article.ArticleKey.Key.toUpperCase() == articleKeys[y]) //if keys match...
				commentCounts[y] = responseBatch.Responses[x].Article.Comments.NumberOfComments; //set comment count at the article index. keeping looking incase of duplicate count lookups

	requestsCompleted = requestsCompleted + responseBatch.Messages.length + responseBatch.Responses.length;
	if (requestsCompleted == totalUniqueKeys) //once all requests for the entire page have been processed, update comment counts
		updateCommentCounts();
}

function updateCommentCounts()
{
	jQuery("span[class^='SP6']").each(function(idx){ //update each comment
		jQuery(this).html(commentCounts[idx] || "0");
	});
}

jQuery(document).ready(function() {
	jQuery("span[class^='SP6']").each(function(idx){ //for each comment
		articleKeys.push(this.className.toUpperCase());
		jQuery(this).html("0"); //clear comment count
	});
	getCommentCounts();
});

//remove duplicate elements in array
Array.prototype.uniqueArr = function ()
{
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i])
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}