/**
 * @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.PluckArticleManager = function(config){
    this.inheritsFrom = com.rogers.socialmedia.core.pluck.manager.PluckBaseManager;
    this.inheritsFrom(config);
    
    this.getCustomArticle = function(contentId, responseFunction){
        var requestBatch = new RequestBatch();             
        requestBatch.AddToRequest(new ArticleKey(contentId));   
        requestBatch.BeginRequest(this.getDirectProxyURL(), function(responseBatch){
                //alert("status: " + responseBatch.Messages[0].Message);
                if(typeof(responseFunction) == "function"){
                    responseFunction(responseBatch);
                }
                else if(typeof(responseFunction) == "string"){	
                    eval(responseFunction + "(" + JSON.stringify(responseBatch) + ");");
                }
        });
    }
	
	this.getCustom = function(contentId, template, elementId){
		var requestBatch = new RequestBatch();    
        requestBatch.AddToRequest(new ArticleKey(contentId));     
        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;
        });
	}
	
	this.getCustomWCallback = function(contentId, templateNodeId, responseFunction){ // template
		var requestBatch = new RequestBatch();    
        requestBatch.AddToRequest(new ArticleKey(contentId));     
        requestBatch.BeginRequest(this.getDirectProxyURL(), function(responseBatch){
				//alert("Status: " + responseBatch.Messages[0].Message + ", JSON: " + JSON.stringify(responseBatch));
				//alert("template: " + template);
				responseBatch = JSON.parse("{\"ResponseBatch\":" + JSON.stringify(responseBatch) + "}");
				//var result = TrimPath.parseTemplate(template).process(responseBatch);
				var result = TrimPath.parseDOMTemplate(templateNodeId).process(responseBatch);
				if(responseFunction)
				{
					if(typeof(responseFunction) == "function"){
						responseFunction(result);
					}
					else if(typeof(responseFunction) == "string"){	
						eval(responseFunction + "(" + result + ");");
					}
				}
        });
	}
};