function RatingSummary(config) {
	if(!config){
		return false;
	}

	this.roleId = config.roleId;
	this.roleTitle = config.roleTitle;
	this.subjectId = config.subjectId;
	this.ratingType = config.ratingType || -1;
	this.minResultSetSize = config.minResultSetSize || -1;
	this.element = (config.element || "").replace("#", "");
	this.formatted = config.formatted || false;
	this.renderedHandler = config.renderedHandler || function(){};

	// Render the initial markup
	$("#" + this.element).append(this.initialMarkup.join(""));

	this.load();
}

/**
 * The url to make content request calls
 */
RatingSummary.prototype.url = GLOBALS.contextPath + "/ajax/loadRatingSummaries";

/**
 * The initial markup that displays on rating summary initialization.
 */
RatingSummary.prototype.initialMarkup =
	["<div class='Loading'>",
		 "rating summary ... ",
	     "<img src='" + GLOBALS.contextPath + "/images/wait24.gif' />",
     "</div>",
     "<div class='ratingSummaryContainer'></div>"];

RatingSummary.prototype.summary =
	["<table cellspacing=0 cellpadding=0>",
	     "<tbody>",
   			 "<tr>",
			     "<td>",
				     "<span class='ratingWordRated'>rated</span> ",
				     "<span class='ratingRated'>{replace-rated}</span> ",
				     "<span class='ratingWordFor'>for</span> ",
				     "<a href='" + GLOBALS.contextPath + "/rankingByCriteria?criteria={replace-criterionName}'>",
						"<span class='ratingCriterionName'>{replace-criterionName}</span>",
					 "</a> ",
				     "<span class='ratingTimes'>({replace-numberTimes})</span> ",
			     "</td>",
		     "</tr>",
	     "</tbody>",
     "</table>"].join("");

/**
 * Is responsible for loading the rating summaries.
 *
 * @return
 */
RatingSummary.prototype.load = function() {
	var scope = this;
	scope.loading = true;

	$.ajax({
		url : this.url,
		data: {
			roleId : this.roleId,
			subjectId : this.subjectId,
			ratingType : this.ratingType,
			minResultSetSize: this.minResultSetSize,
			formatted : this.formatted
		},
		success : function(response){
			scope.loaded(response);
			scope.render();
		},
		faliure : function(response){
			scope.loaded(response);
			scope.render();
		}
	});
}

RatingSummary.prototype.loaded = function(response){
	this.data = $.parseJSON(response);
}

/**
 * @param response
 * @return
 */
RatingSummary.prototype.render = function(){
	var roleContainer,
		summaryContainer,
		criterionContainerId,
		content,
		elementSelector;

	elementSelector = "#" + this.element;
	roleContainer = $(elementSelector);
	summaryContainer = roleContainer.find(".ratingSummaryContainer");

	if(this.data.results.ratingSummaries) {

		for(var i=0, len = this.data.results.ratingSummaries.length; i<len; i++) {
			var summary = this.data.results.ratingSummaries[i];

			// Create an identifier for this criterion and then create a container.
			summaryContainer.append("<div id='" + (this.element + "-" + summary.criterionId) + "'><div class='ratingSummary'></div></div>");

			// compile the content
			content = this.summary.replace("{replace-rated}", Math.round(summary.averageRating * 100) / 100)
			   					  .replace(/{replace-criterionName}/g, summary.criterionName)
			   					  .replace("{replace-numberTimes}", summary.numberRatings + (summary.numberRatings == 1 ? " time" : " times"));

			// apply the content to the element
			$(elementSelector + "-" + summary.criterionId).find(".ratingSummary").html(content);
		}
		roleContainer.find(".Loading").hide();
	} else {
		roleContainer.find(".Loading").remove();
		roleContainer.find(".ratingSummaryContainer").remove();
	}

	this.renderedHandler();
}

