jQuery.noConflict();

function reInitRating(content_id) {
	if(!window.location.toString().startsWith('file')) {
		var vUrl = buildRatingActionUrl(-1, content_id);
		jQuery.ajax({
			  url: vUrl,
			  success: function(data) {
			    jQuery("div[id^=avg" + content_id + "]").parent().parent().parent().html(data);
			  }
		});
	}
}
function buildRating(split, content_id, enableRating, timer) {
	jQuery("#avg" + content_id + timer).children().not(":input").hide();
	//jQuery("#rat" + timer).children().not("select, #messages" + timer).hide();
	
	// Create stars for: Average rating
	jQuery("#avg" + content_id + timer).stars({
		disabled: true,
		cancelShow: false,
		split: split
	});
	
	var	disabled = enableRating == 2 ? rating_disable : !enableRating;
	
	// Create stars for: Rate this
	jQuery("#rat" + content_id + timer).stars({
		inputType: "select",
		cancelShow: false,
		captionEl: jQuery("#caption" + timer ),
		disabled: disabled,
		callback: function(ui, type, value)
		{
			// Disable Stars while AJAX connection is active
			ui.disable();

			// Display message to the user at the begining of request
			//jQuery("#messages").text("Saving...").stop().css("opacity", 1).fadeIn(30);
			
			// Send request to the server using POST method
			var url = buildRatingActionUrl(value, content_id);
			rate_value = value;
			new Ajax.Request(url, {
				method : 'post',
				onFailure : function(t) {
//					alert('failure communication with the server ');
				},
				onException : function(t, exception) {
//					alert('exception communication with the server: ' + exception);
				},
				onSuccess : function(t) {
					var json = t.responseText.evalJSON();
					
					// Update other text controls...
					jQuery("#avg" + content_id + timer).stars("select", Math.round(json.rateAvg*split));
					jQuery("#all_votes" + content_id + timer).text(json.rateNumber);
					//jQuery("#all_avg" + content_id + timer).text(json.rateAvg);
					
					// Update text controls for other components
					jQuery(".avg" + content_id).stars("select", Math.round(json.rateAvg*split));
					jQuery(".all_votes" + content_id).text(json.rateNumber);
					jQuery(".all_avg" + content_id).text(json.rateAvg);
					jQuery(".rat" + content_id).stars("select", value);
					jQuery(".rat" + content_id).stars("disable");
					
					// Display confirmation message to the user
					//jQuery("#messages" + timer).text("Rating saved (" + value + "). Thanks!").stop().css("opacity", 1).fadeIn(30);
					
					// Hide confirmation message and disable stars for "Rate this" control, after 2 sec...
					setTimeout(function(){
						jQuery("#messages" + timer).fadeOut(1000, function(){ui.disable()})
					}, 2000);
				}.bind(this)
			});
		}
	});
	
	if(enableRating == 2) {
		if(rating_disable) {
			jQuery("#rat" + content_id + timer).stars("select", rating_session_rate);
		}
	}
	// Create element to use for confirmation messages
	//jQuery('<div id="messages' + timer + '"/>').appendTo("#rat" + timer);
}

function buildRatingActionUrl(value, content_id) {
	var newUrl = "";
	var result = rating_link.split(/[\?]/);
	for (var i = 0; i < result.length; i++)
    {
		if (i == 0) {
			newUrl = result[i];
			newUrl += "/" + value + "/" + content_id + "/" + site_id; 
		} else {
			newUrl += "?" + result[i];
		}
    }
	if(result.length > 1) {
		newUrl += "&siteId=" + site_id + "&" + (new Date()).getTime();
	} else {
		newUrl += "?siteId=" + site_id + "&" + (new Date()).getTime();
	}
	return newUrl;
}
