﻿function NodePointRatingActive(id, value, count, rating) {
	this.init = function() {
		this.fillInactive();
		this.fillActive();

		$("#pointRatingContainer" + this.id).css("display", "inline");
		$("#pointRatingVotedContainer" + this.id).css("display", "none");

		this.showInactive();

		var rating = this;
		$("#pointRatingContainer" + this.id).bind("mouseenter", function() { rating.showActive() });
		$("#pointRatingActiveHint" + this.id).mouseover(function() { return false; });
		$("#pointRatingContainer" + this.id).bind("mouseleave", function() { rating.showInactive() });
		$("#pointRatingInactiveHint" + this.id).mouseover(function() { return false; });
	}

	this.showActive = function() {
		$("#pointRatingActive" + this.id).css("display", "block");
		$("#pointRatingInactive" + this.id).css("display", "none");
		$("#pointRatingLoading" + this.id).css("display", "none");
	}

	this.showInactive = function() {
		$("#pointRatingInactive" + this.id).css("display", "block");
		$("#pointRatingActive" + this.id).css("display", "none");
		$("#pointRatingLoading" + this.id).css("display", "none");
	}

	this.showLoading = function() {
		$("#pointRatingActive" + this.id).css("display", "none");
		$("#pointRatingInactive" + this.id).css("display", "none");
		$("#pointRatingLoading" + this.id).css("display", "block");
	}

	this.fillInactive = function() {
	    var maxRating = 5;
	    var splitStar = 4;

	    var starsHtml = "";
	    for (var i = 1; i <= (maxRating * splitStar); i++) {
	        var value = i / splitStar;
	        if (value == this.value) {
	            starsHtml += "<input name=\"inactive" + this.id + "\" type=\"radio\" class=\"starInactive\"/ value=\"" + value + "\" checked=\"checked\" disabled=\"disabled\">";
	        } else {
	            starsHtml += "<input name=\"inactive" + this.id + "\" type=\"radio\" class=\"starInactive\"/ value=\"" + value + "\" disabled=\"disabled\">";
	        }
	    }

	    if (this.rating) {
	        $("#pointRatingInactiveHint" + this.id).addClass("showRating").html("голосов: " + this.count + "<br />рейтинг: " + this.rating);
	    } else {
	        $("#pointRatingInactiveHint" + this.id).html("голосов: " + this.count);
	    }
	    
	    $("#pointRatingInactive" + this.id + " div").remove();
	    $("#pointRatingInactive" + this.id).prepend(starsHtml);

	    $("#pointRatingInactive" + this.id + " .starInactive").rating(
			{
			    split: splitStar,
			    required: true
			});
	}

	this.fillActive = function() {
	    var maxRating = 5;

	    var starsHtml = "";
	    for (var i = 1; i <= maxRating; i++) {
	        starsHtml += "<input name=\"active" + this.id + "\" type=\"radio\" class=\"starActive\"/ value=\"" + i + "\">";
	    }

	    $("#pointRatingActive" + this.id + " div").remove();
	    $("#pointRatingActive" + this.id).prepend(starsHtml);

	    var rating = this;
	    $("#pointRatingActive" + this.id + " .starActive").rating(
			{
			    required: true,
			    focus: function(value) {
			        $("#pointRatingActiveHint" + rating.id, $(this).parent()).text(value);
			    },
			    blur: function() {
			        $("#pointRatingActiveHint" + rating.id, $(this).parent()).text("");
			    },
			    callback: function(value) {
			        $("#pointRatingContainer" + rating.id).unbind("mouseenter");
			        $("#pointRatingContainer" + rating.id).unbind("mouseleave");
			        rating.showLoading(rating.id);

			        $.getJSON("/rating/node/" + rating.id + "/addpoint/" + value, function(data) {
			            if (rating.rating) {
			                new NodePointRatingActiveVoted(data.id, data.rating, data.count, data.selected, false, rating.rating);
			            } else {
			                new NodePointRatingActiveVoted(data.id, data.rating, data.count, data.selected);
			            }
			        })
			    }
			});
	}

	this.id = id;
	this.value = value;
	this.count = count;
	this.rating = rating;

	this.init();
}








function NodePointRatingActiveVoted(id, value, count, selected, isDisabled, rating) {
	this.init = function() {
		this.fillInactive();

		this.showInactive();

		$("#pointRatingContainer" + this.id).css("display", "none");
		$("#pointRatingVotedContainer" + this.id).css("display", "inline");
	}

	this.showLoading = function() {
		$("#pointRatingVotedInactive" + this.id).css("display", "none");
		$("#pointRatingVotedLoading" + this.id).css("display", "block");
	}

	this.showInactive = function() {
		$("#pointRatingVotedInactive" + this.id).css("display", "block");
		$("#pointRatingVotedLoading" + this.id).css("display", "none");
	}

	this.fillInactive = function() {
	    var maxRating = 5;
	    var splitStar = 4;

	    var starsHtml = "";
	    var seed = Math.round(Math.random() * 1000000);
	    for (var i = 1; i <= (maxRating * splitStar); i++) {
	        var value = i / splitStar;
	        if (value == this.value) {
	            starsHtml += "<input name=\"inactiveVoted" + seed + "\" type=\"radio\" class=\"starInactive\"/ value=\"" + value + "\" checked=\"checked\">";
	        } else {
	            starsHtml += "<input name=\"inactiveVoted" + seed + "\" type=\"radio\" class=\"starInactive\"/ value=\"" + value + "\">";
	        }
	    }

	    if (this.rating) {
	        $("#pointRatingVotedInactiveHint" + this.id).addClass("showRating").html("голосов: " + this.count + "<br />рейтинг: " + this.rating);
	    } else {
	        $("#pointRatingVotedInactiveHint" + this.id).html("голосов: " + this.count);
	    }

	    $("#pointRatingVotedInactive" + this.id + " div").remove();
	    $("#pointRatingVotedInactive" + this.id).prepend(starsHtml);
	    this.showMarkOrDisabled();

	    var rating = this;
	    $("#pointRatingVotedInactive" + this.id + " .starInactive").rating(
			{
			    split: splitStar,
			    readOnly: true,
			    showCancel: !isDisabled,
			    callback: function(value) {
			        rating.isLoading = true;
			        $("#pointRatingVotedHelp" + rating.id).html("&nbsp;");
			        rating.showLoading(rating.id);

			        $.getJSON("/rating/node/" + rating.id + "/removepoint", function(data) {
			            rating.isLoading = false;
			            if (rating.rating) {
			                new NodePointRatingActive(data.id, data.rating, data.count, rating.rating);
			            } else {
			                new NodePointRatingActive(data.id, data.rating, data.count);
			            }
			        })
			    },
			    focus: function(value) {
			        if (!value)
			            rating.showCancelVote();
			        else
			            rating.showMarkOrDisabled();
			    },
			    blur: function(value) {
			        if (!value && !rating.isLoading)
			            rating.showMarkOrDisabled();
			    }
			});
	}

	this.showMarkOrDisabled = function(mark) {
		if (this.isDisabled) {
			$("#pointRatingVotedHelp" + this.id).text("вы не можете голосовать");
		} else {
			$("#pointRatingVotedHelp" + this.id).html("ваша оценка: " + this.selected);
		}
	}

	this.showCancelVote = function() {
		$("#pointRatingVotedHelp" + this.id).text("отменить голос");
	}

	this.id = id;
	this.value = value;
	this.count = count;
	this.selected = selected;
	this.isDisabled = isDisabled;
	this.rating = rating;

	this.isLoading = false;

	this.init();
}







function NodePointRatingForList(id, value, count, rating) {
	this.init = function() {
		this.fillInactive();

		$("#pointRatingForListContainer" + this.id).css("display", "inline");
	}

	this.fillInactive = function() {
	    var maxRating = 5;
	    var splitStar = 4;

	    var starsHtml = "";
	    var seed = Math.round(Math.random() * 1000000);
	    for (var i = 1; i <= (maxRating * splitStar); i++) {
	        var value = i / splitStar;
	        if (value == this.value) {
	            starsHtml += "<input name=\"inactiveForList" + seed + "\" type=\"radio\" class=\"starInactive\"/ value=\"" + value + "\" checked=\"checked\">";
	        } else {
	            starsHtml += "<input name=\"inactiveForList" + seed + "\" type=\"radio\" class=\"starInactive\"/ value=\"" + value + "\">";
	        }
	    }

	    if (this.rating) {
	        $("#pointRatingForListInactiveHint" + this.id).addClass("showRating").html("голосов: " + this.count + "<br />рейтинг: " + this.rating);
	    } else {
	        $("#pointRatingForListInactiveHint" + this.id).html("голосов: " + this.count);
	    }
	    $("#pointRatingForListInactive" + this.id + " div").remove();
	    $("#pointRatingForListInactive" + this.id).hide();
	    $("#pointRatingForListInactive" + this.id).prepend(starsHtml);

	    var rating = this;
	    $("#pointRatingForListInactive" + this.id + " .starInactive").rating(
			{
			    split: splitStar,
			    readOnly: true
			});

	    $("#pointRatingForListInactive" + this.id).show();
	}

	this.id = id;
	this.value = value;
	this.count = count;
	this.rating = rating;
	
	this.init();
}

