var currentContentID = 0;
var currentCommunityID = 0;
var currentModuleID = 0;
var currentRatingValue = 0;


function RateMe(communityID, moduleID, contentID, ratingValue){
	//update Attribute values
	currentContentID = contentID;
	currentCommunityID = communityID;
	currentModuleID = moduleID;
	currentRatingValue = ratingValue;	
	
	//show the ajax spinner 
	spinner = "systemWorking_" + moduleID + "_" + contentID;
	Element.show(spinner);
	
	
	var url = '/COP/Personal/Rating/SetRating.fusion';
	//var params = 'ContentID=' + contentID + '&CommunityID=' + communityID;
	var params = 'ContentID=' + contentID + '&CommunityID=' + communityID+ '&ModuleID=' + moduleID+ '&RatingValue=' + ratingValue;
	
	var rateAjax = new Ajax.Request(
	url, 
	{
		method: 'get', 
		parameters: params, 			
		onComplete: showResponse
	});	  
}

/*	
	The avg rating value is passed back with every ajax call to SetRating.cfm in XML formatted string.
	Need to make it an XML object to parse through it.
*/
function showResponse(originalRequest){	
	var returnedXML = getXMLFromString(originalRequest.responseText);	
	
	//update the currentRatingElement (grey stars) with the avg rating. 
	currentRatingElement = $("current-rating_" + currentModuleID + "_" + currentContentID);
	currentRatingElementValue = ((returnedXML.getElementsByTagName('ajax-response')[0].childNodes[0].getAttribute("value")));
	currentRatingElement.style.width = ( roundToHalf(currentRatingElementValue) * 20) + "px";
	
	//update the vote count 
	currentVoteCountElement = $("totalVotes_" + currentModuleID + "_" + currentContentID);
	var numVotes =((returnedXML.getElementsByTagName('ajax-response')[0].childNodes[1].getAttribute("value")));
	var numVotesLabel = " votes";
	if(numVotes == 1){
		numVotesLabel = " vote";	
	}
	currentVoteCountElement.innerHTML = numVotes + numVotesLabel;
	//hide the ajax spinner
	spinner = "systemWorking_" + currentModuleID + "_" + currentContentID;
	Element.hide(spinner);	
}

function reportError(){
	alert("There has been a problem with your vote.  Please notify a community facilitator.");
}

function getXMLFromString(xmlString){
	xmlString = trim(xmlString.replace(new RegExp("[\\r\\n\\t]", "g"), ""));
	if (document.implementation.createDocument){ 
		// Mozilla, create a new DOMParser 
		var parser = new DOMParser(); 
		myDocument = parser.parseFromString(xmlString, "text/xml"); 
	} else if (window.ActiveXObject){ 
		// Internet Explorer, create a new XML document using ActiveX 
		// and use loadXML as a DOM parser. 
		myDocument = new ActiveXObject("Microsoft.XMLDOM") 
		myDocument.async="false"; 
		myDocument.loadXML(xmlString);   
		//alert(myDocument.getElementsByTagName('ajax-response')[0].childNodes[0].getAttribute("value"));
	}
	return myDocument;
}

function roundToHalf(value) {
   var converted = parseFloat(value); // Make sure we have a number
   var decimal = (converted - parseInt(converted, 10));
   decimal = Math.round(decimal * 10);
   if (decimal == 5) { return (parseInt(converted, 10)+0.5); }
   if ( (decimal < 3) || (decimal > 7) ) {
      return Math.round(converted);
   } else {
      return (parseInt(converted, 10)+0.5);
   }
} 


function trim(sString){
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

 