/**
 * @fileoverview
 * This file contains the Pluck rating manager class.
 */

/**
 * Pluck manager for ratings.
 * @constructor
 * @author Matt York
 * @version 1.0
 */
com.rogers.socialmedia.core.pluck.manager.PluckRatingManager = function(config){
    this.inheritsFrom = com.rogers.socialmedia.core.pluck.manager.PluckBaseManager;
    this.inheritsFrom(config);
    
    /**
     * Displays the rating input and current existing rating widgets.
     * @param {String} contentId  A unique key used for referncing the page (Article, story etc.).
     */
    this.getRating = function(contentId){
       gSiteLife.Rating("ExternalResource", contentId); 
	}
    
    // NOTE: this is how responseFunction handling should happen to allow Java
    // function name definition
    this.getCustomRating = function(contentId, responseFunction){
        var requestBatch = new RequestBatch();             
        requestBatch.AddToRequest(new ArticleKey(contentId));   
        requestBatch.BeginRequest(this.getDirectProxyURL(), function(responseBatch){
                //alert("status: " + responseBatch.Messages[0].Message + "responseFunction: " + responseFunction);
                if(typeof(responseFunction) == "function"){
                    responseFunction(responseBatch);
                }
                else if(typeof(responseFunction) == "string"){
                    eval(responseFunction + "(" + JSON.stringify(responseBatch) + ");");
                }
        });
    }
	
	this.getCurrentRating = function(contentId, template, elementId, responseFunction){
        var requestBatch = new RequestBatch();    
        var articleKey = new ArticleKey(contentId);
        requestBatch.AddToRequest(articleKey);     
        requestBatch.BeginRequest(this.getDirectProxyURL(), function(responseBatch){
                //alert("status: " + responseBatch.Messages[0].Message);
				try{
				var result = null;
				responseBatch = JSON.parse("{\"ResponseBatch\":" + JSON.stringify(responseBatch) + "}");
				var compiledTemplate = TrimPath.parseTemplate(template);
				var result = compiledTemplate.process(responseBatch);
				//alert("result: " + result);
				//document.getElementById(elementId).innerHTML = result;
				jQuery("#" + elementId).html(result);/*, function(){
					alert("cb");
					jQuery('.ratingStar').rating({
					  callback: function(value, link){
						alert("rating: " + value);
					  }
					});
				});*/
				}catch(e){alert(e.message)};
				if(responseFunction){
					if(typeof(responseFunction) == "function"){
						responseFunction(responseBatch);
					}
					else if(typeof(responseFunction) == "string"){
						eval(responseFunction + "(" + JSON.stringify(responseBatch) + ");");
					}
				}
        });
	}
	
	this.rate = function(contentId, rating, pageURL, pageTitle, responseFunction) {   
		 var articleKey = new ArticleKey(contentId);  
		 var requestBatch = new RequestBatch();              
		 var rateAction = new RateAction(articleKey, rating);  
		 requestBatch.AddToRequest(rateAction); 
		 var updateArticleAction = new UpdateArticleAction(articleKey, pageURL, pageTitle);
		 requestBatch.AddToRequest(updateArticleAction); 
		 requestBatch.BeginRequest(this.getDirectProxyURL(), function(responseBatch){
			if(typeof(responseFunction) == "function"){
				responseFunction(responseBatch);
			}
			else if(typeof(responseFunction) == "string"){
				eval(responseFunction + "(" + JSON.stringify(responseBatch) + ");");
			}
		});
    }
};