var object_rates = new Array();

function RatingClass(url) {
	var self = this;
	this.script_url = url;
	this.object_id = 0;
	this.rate_names = ['','невыносимо (1)', 'очень плохо (2)', 'плохо (3)', 'слабо (4)', 'средне (5)', 'нормально (6)', 'неплохо (7)', 'хорошо (8)', 'отлично (9)', 'идеал (10)'];

	this.makeRate = function(object_id, rate) {
		if (user_id > 0) {
			this.object_id = object_id;
			this.postRate(object_id);
		}
	}

	this.postRate = function(object_id) {
		if (old_rate != object_rates[object_id]) {
			var rate = object_rates[object_id];
			var url = this.script_url + object_id + '/' + rate + '/';
			jQuery.post(url, {}, this.postRateCallback, 'json');

			if(object_rates[object_id] != 0)
				Alert.show(AlertText.vote.title, AlertText.vote.ok, 'message');
			else
				Alert.show(AlertText.vote.title, AlertText.vote.movie, 'notice');
		} else {
			if(object_rates[object_id] == 0)
				Alert.show(AlertText.vote.title, AlertText.vote.movie, 'notice');
			else
				Alert.show(AlertText.vote.title, AlertText.vote.similar, 'notice');
		}
	}

	this.postRateCallback = function(response) {
		var html = String(parseFloat(response.score.score).toFixed(2))+ '<span>(' + String(parseInt(response.score.num_votes))+ ')</span>';
		jQuery("#rating_all_" + self.object_id).html(html);
	}


	this.rateMouseOver = function(id, rate) {
		current_rate = rate;
		this.setRateName(id, current_rate);
		jQuery("#RateControl" + self.type + '_'+ id).children().each(this.redrawStar);
	}

	this.rateMouseOut = function(id, rate) {
		current_rate = object_rates[id];
		this.setRateName(id, current_rate);
		jQuery("#RateControl" + self.type + '_'+ id).children().each(this.redrawStar);

	}

	this.rateClick = function(id, rate) {
		old_rate = object_rates[id];
		object_rates[id] = rate;
		this.setRateName(id, rate);
		this.makeRate(id, object_rates[id]);
	}

    this.setRateName = function(id, value) {
        if (value > 0) {
			if (object_rates[id] == value) {
				jQuery("#RateName" + self.type + '_' + id).html("Ваша оценка: " + "<span>" + this.rate_names[value] + "</span>");
			} else {
				jQuery("#RateName" + self.type + '_' + id).html("Ваша оценка: " + "<span>" + this.rate_names[value] + "</span>");
			}
		} else {
			if (object_rates[id] == 0 && value == 0) {
				jQuery("#RateName" + self.type + '_' + id).html("Ваша оценка: " + "<strong>" + "Не выставлена" + "</strong>");
			} else {
				jQuery("#RateName" + self.type + '_' + id).html(this.rate_names[value]);
			}
		}
	}

	this.redrawStar = function() {
		var rate = self.extractRate(this.id);
		if (rate <= current_rate && rate > 0) {
			$(this).removeClass('rating0');
			$(this).addClass('rating' + rate);
		} else {
			$(this).removeClass('rating0');
			$(this).addClass('rating0');
		}
	}

	this.extractRate = function(str) {
		var arr = str.split(/_/);
		return parseInt(arr[2]);
	}

	this.bindAction = function(container_id) {
		$('#RateControl' + self.type + '_' + container_id + ' li').each(function(){
			var tmp1 = this.id.split(/_/);
			$(this).bind("mouseenter", function(e){
       			self.rateMouseOver(tmp1[1],tmp1[2]);
    		}).bind("mouseleave", function(e){
       			self.rateMouseOut(tmp1[1],tmp1[2]);
    		}).bind("click", function(e){
       			self.rateClick(tmp1[1],tmp1[2]);
    		});
		});
	}

	this.drawRateControl = function(id, rate) {
		var html = '';
		rate = (user_id > 0) ? parseInt(rate) : 0;
		object_rates[id] = rate;
		var star_rate = (rate > 0) ? rate : 0;
		html += '<p class="rating_user_text" id="RateName' + self.type + '_' + id + '"></p>';
		html += '<ul class="rating_user" id="RateControl' + self.type + '_' + id + '">';
		for (var i = 1; i <= star_rate; i++) {
			html += '<li id="Rate_' + id + '_' + i + '" class="rating'+i+'"></li>';
		}
		for (var i = star_rate + 1; i <= 10; i++) {
			html += '<li id="Rate_' + id + '_' + i + '" class="rating0"></li>';
		}
		html += '</ul>';
		document.write(html);
		this.bindAction(id);
		this.setRateName(id, rate);
	}
}