/* reviews */
var soc_starRating = 3;
var soc_starRatingChanged = false;
function soc_initRatings() 
{
	var i = 1;
	$$('li.starRating').each(function(star)
	{
		star.rating = i;
		star.observe("mouseover",
				function(event) {
					$$('li.starRating').each(function(other)
					{
						if(other.rating <= star.rating) {
							other.addClassName("active");
						} else { 
							other.removeClassName("active");
						}
					});
			})
			.observe("mouseout",
				function(event) {
					$$('li.starRating').each(function(other)
					{
						if(other.rating > soc_starRating) {
							other.removeClassName("active");
						} else {
							other.addClassName("active");
						}
					});
			})
			.observe("click",
				function(event) { 
					soc_starRating = star.rating;
					soc_starRatingChanged = true;
					$$('li.starRating').each(function(other)
					{
						if(other.rating <= star.rating) {
							other.addClassName("active");
						} else { 
							other.removeClassName("active");
						}
					});
			});
		i++;
	});
}

function soc_submitRating(callback) 
{
	if(!soc_starRatingChanged) {
		alert("Please click a star to indicate a rating for this item before continuing.");
	} else {
		var url = '/s';
		var form = $("contentSubmission");
		var cid = form.getInputs('hidden', 'id')[0].value;
		var destid = form.getInputs('hidden', 'destid')[0].value;
		new Ajax.Request( url, {
			method: 'post',
			parameters: { 
				'id': cid,  // cms.content.id
				'type': 'content', 
				'destID': destid, // website section ID
				'action': 'addRatingAJAX',
				'isAjax': 'Y',
				'rating': soc_starRating },
			onSuccess: function(transport) { 
				var json = transport.responseText.evalJSON(true);
				if(json.success) {
					callback();
				} else if(json.error = "Internal Error") {
					// user has already rated this item; gracefully fail, still allow the comment to go through
					callback();
				} else {
					alert("An unknown error occured while submitting your rating. Please try again in a few moments.");
				}
			}
		});
	}	
}

Event.observe(window,'load',function() { soc_initRatings(); });
