var object_rates = new Array();
var objects_array = new Array();

function RatingClass(url,type) {
	var self = this;
	
	this.script_url = url;
	this.type = type;
	this.rate_names = ['','невыносимо (1)', 'плохо (2)', 'средне (3)', 'хорошо (4)', 'отлично (5)'];

	this.makeRate = function(object_id, rate) {
		if (user_id > 0) {
			this.postRate(object_id);
		}
	}
	
	this.postRate = function(object_id) {
		if (old_rate != object_rates[object_id]) {
			var params = {};
			params.object_id = object_id;
			params.rate = object_rates[object_id];
			params.old_rate = old_rate;

			$.get(this.script_url, params, 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) {
		jQuery("#rating_all_" + response.object_id).html(String(parseFloat(response.rate))+ '<span>(' + String(parseInt(response.count))+ ')</span>');
	}
	
	
	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("Ваша оценка:<br/>" + "<span>" + this.rate_names[value] + "</span>");
			} 
			else {
				jQuery("#RateName" + self.type + '_' + id).html("Ваша оценка:<br/>" + "<span>" + this.rate_names[value] + "</span>");
			}
		}
	 	else {
			if (object_rates[id] == 0 && value == 0) {
				jQuery("#RateName" + self.type + '_' + id).html("Ваша оценка:<br/>" + "<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().addClass('rate');
		}
		else {
			$(this).removeClass();
		}
	}
	
	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]);
       			$('.grade-comment').show();
    		});
		});
	}
	
	this.drawRateControl = function(id, rate, comment, neededit) {
		var html = '';

		rate = (user_id > 0) ? parseInt(rate) : 0;

		object_rates[id] = rate;
		objects_array[id] = this;

		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="rated"></li>';
		}
		
		for (var i = star_rate + 1; i <= this.rate_names.length - 1; i++) {
			html += '<li id="Rate_' + id + '_' + i + '"></li>';
		}
		html += '</ul>';
		
		comment = comment || '';
		if (neededit>0) {
		html += this.drawCommentForm(id, rate, comment);
		}
		html += this.drawComment(id, comment);

		document.write(html);
		if (comment.length > 0) {$('.grade-comment').show();} /* если коммент есть - то показываем его */
		if (user_id > 0) {
			this.bindAction(id);
		}
		this.setRateName(id, rate);
	}
	
	this.drawComment = function(id, comment) {
		var html = '';
		html += '<div id="grade-comment-' + id + '" class="grade-comment">';
		if (comment.length > 0) {
			html += comment;
			html += '<div class="edit fright"><a href="#" class="editsmall" title="Редактировать" onclick="return objects_array['+id+'].showCommentForm('+id+')">&nbsp;</a></div>';
		} else {
			html += '<a href="#" onclick="return objects_array['+id+'].showCommentForm('+id+')" class="addsmall">&nbsp;</a><a href="#" onclick="return objects_array['+id+'].showCommentForm('+id+')" class="green">Добавить комментарий</a>';
		}
		html += '</div>';
		return html;
	}
	
	this.drawCommentForm = function(id, rate, comment) {
		var html = '<div id="RateCommentForm_' + id + '" class="opinion_edit">';
		html +='	<form action="add" method="get">';
		html +='		<div class="bg"></div>';
		html +='		<div class="input">';
		html +='			<input type="text" name="RateComment_' + id + '" id="RateComment_' + id + '" value="' + comment + '">';
		html +='		</div>';
		html +='		<div class="comment_control">';
		html +='			<a id="goRateComment_' + id + '" class="ilink" href="javascript://" onclick="objects_array['+id+'].postRateComment('+id+'); return false;">Сохранить</a>&nbsp;&nbsp;&nbsp;';
		html +='			<a id="clearRateComment_' + id + '" class="ilink" href="javascript://" onclick="return objects_array['+id+'].hideCommentForm('+id+')";">Отмена</a>';
		html +='		</div>';
		html +='	</form>';
		html +='</div>';
		return html;
	}

	this.showCommentForm = function(id) {
		$('#RateCommentForm_' + id).show();
		$('#grade-comment-' + id).hide();
		return false;
	}
	
	this.hideCommentForm = function(id) {
		$('#RateCommentForm_' + id).hide();
		$('#grade-comment-' + id).show();
		return false;
	}
	
	this.postRateComment = function(object_id) {
		var params = {};
		params.object_id = object_id;
		params.rate_commet = $('#RateComment_' + object_id).val();
		params.type = this.type;
		$.post(
			'/grade/comment/', 
			params, 
			function(response) {
				Alert.show(AlertText.vote.title, AlertText.vote.notice, 'message');
				self.hideCommentForm(response.id);
				$('#grade-comment-' + response.id).html(response.html);
			},
			'json'
		);
	}
}