/**
 * @fileoverview
 * This file contains the Pluck article manager class.
 */

/**
 * Pluck manager for articles. It is useful for creating custom widgets with
 * information about comments and ratings.
 * @constructor
 * @author Matt York
 * @version 1.0
 */
com.rogers.socialmedia.core.pluck.manager.PluckDiscoveryManager = function(config){
    this.inheritsFrom = com.rogers.socialmedia.core.pluck.manager.PluckBaseManager;
    this.inheritsFrom(config);
	
		this.getMostCommented = function(maxResults, age, responseFunction){
        var searchSections  = new Array();
        var  searchCategories = new  Array();
        var activity =  new Activity("Commented");
        var contentType  = new ContentType("Article");
        var limitToContributors =  new Array();
		var requestBatch = new RequestBatch();   
        requestBatch.AddToRequest(new DiscoverContentAction(searchSections,  searchCategories, limitToContributors, activity,  contentType,  age, maxResults));
        requestBatch.BeginRequest(this.getDirectProxyURL(), function(responseBatch){
			if(responseFunction)
			{
				if(typeof(responseFunction) == "function"){
					responseFunction(responseBatch);
				}
				else if(typeof(responseFunction) == "string"){	
					eval(responseFunction + "(" + JSON.stringify(responseBatch) + ");");
				}
			}
        });
    }
	
	this.getMostCommentedTemplate = function(template, elementId, maxResults){
		var searchSections  = new Array();
        var  searchCategories = new  Array();
        var activity =  new Activity("Commented");
        var contentType  = new ContentType("Article");
        var limitToContributors =  new Array();
		var requestBatch = new RequestBatch();   
        requestBatch.AddToRequest(new DiscoverContentAction(searchSections,  searchCategories, limitToContributors, activity,  contentType, 15, maxResults));
        requestBatch.BeginRequest(this.getDirectProxyURL(), function(responseBatch){
				//alert("Status: " + responseBatch.Messages[0].Message + ", JSON: " + JSON.stringify(responseBatch));
				//alert("template: " + template);
				var result = "";
				responseBatch = JSON.parse("{\"ResponseBatch\":" + JSON.stringify(responseBatch) + "}");
				result = TrimPath.parseTemplate(template).process(responseBatch);
				//jQuery("#" + elementId).html(result);
				document.getElementById(elementId).innerHTML += result;
        });
	}
};