/**
 *	Скрипт обработки кнопок голосовалки комментариев.
 *	Для его работы в коде страницы необходимо определить JS-переменную UserID - id авторизованного пользователя
 */

$(document).ready(function(){

	if(typeof UserID != 'undefined' && UserID) {
		
		$('a.votePlusButton').each(function(){		
			$(this).click(function(){
				$(this).attr('CommentID', $(this).parents('td.comment').attr('id'));			
				$(this).attr('MarkType', $('td#' + $(this).attr('CommentID') + ' td.voteBlock select').val());
				send_mark(
						$(this).attr('CommentID'),
						UserID,
						$(this).attr('MarkType'),
						1
				);
				$('img',$(this).parent().get(0)).attr('title','Вы уже оценили этот комментарий');
			});
		});
		
		$('a.voteMinusButton').each(function(){

			$(this).click(function(){
				$(this).attr('CommentID', $(this).parents('td.comment').attr('id'));			
				$(this).attr('MarkType', $('td#' + $(this).attr('CommentID') + ' td.voteBlock select').val());
				send_mark(
						$(this).attr('CommentID'),
						UserID,
						$(this).attr('MarkType'),
						-1
				);
				$('img',$(this).parent().get(0)).attr('title','Вы уже оценили этот комментарий');
			});
		});
		
	}

	$('div.massageButton a').click(function(){
		$('td#' + $(this).parents('td.comment').attr('id') + ' div.massageButton a').toggle();
		$('td#' + $(this).parents('td.comment').attr('id') + ' div.message').toggle();
	});
	
	$('.alertTitle').click(function(){
		alert_title($('img',$(this)));
	});

	hide_comments();
	
});

function send_mark(CommentID, UserID, MarkType, Mark) {
	$.getJSON(
		'/ajax/save_mark.php', {
			'UserID': UserID,
			'CommentID': CommentID,
			'MarkType': MarkType,
			'Mark': Mark
		}, function(data) {
			update_rating(data.CommentID,data.Rating1,data.Rating2);
		}
	);
}

function update_rating(CommentID,Rating1,Rating2) {
	
	var Rating = new Array(Rating1, Rating2);
	var title;
		
	for(var i=1;i<3;i++) {
	// идем по всем типам голосования
				
		if(i==1) title='за актуальность: ' + Rating[i-1];
		if(i==2) title='за юмор: ' + Rating[i-1];
		
		$('td#' + CommentID + ' span.rating_' + i).html(Rating[i-1]  + '<img src="../images/icons/star.png" class="star">');
		//$('td#' + CommentID + ' span.rating_' + VoteTypes[i]).html(Rating[i]  + '<img src="../images/icons/star.png" class="star">')
			
		if (parseInt(Rating[i-1]) > 4){
		// если рейтинг > 4, то добавляем серебрянную звезду
			$('td#' + CommentID + ' span.rating_' + i).html('<img src="../images/icons/vote_star2.gif" alt="' + title + '" title="' + title + '">');
		}

		if (parseInt(Rating[i-1]) > 9){
		// если рейтинг > 9, то добавляем золотую звезду
			$('td#' + CommentID + ' span.rating_' + i).html('<img src="../images/icons/vote_star1.gif" alt="' + title + '" title="' + title + '">');
		}
	
		if (parseInt(Rating[i-1]) < 5){
		// если рейтинг < 5, то рисуем цифру
			$('td#' + CommentID + ' span.rating_' + i).html(Rating[i-1]);
		}		
		
	}
	
	// заменяем картинки + и - на серые
	$('td#' + CommentID + ' td.voteBlock a img').each(function() {
		$(this).attr('src',$(this).attr('src').replace('.jpg','_gray.jpg'));
	});
	
	
	// скрываем селект типа голосования, делаем кнопки + и - недоступными
	$('td#' + CommentID + ' td.voteBlock select').hide();	
	$('td#' + CommentID + ' td.voteBlock a').unbind();
	$('td#' + CommentID + ' td.voteBlock a').click(function() {
		alert_title($('img',$(this)));
	});	 
	
	
	//$('td#' + CommentID + ' td.commentBody').show();
	//$('td#' + CommentID + ' td.commentHidden').hide();
	
	/*
	if (parseInt(Rating[0]) <= -5){
		//$('td#' + CommentID + ' td.commentBody').html('Не актуально');
		$('td#' + CommentID + ' td.commentBody').hide();
		$('td#' + CommentID + ' td.commentHidden').html('Не актуально');		
		$('td#' + CommentID + ' td.commentHidden').show();
	}
	
	if (parseInt(Rating[1]) <= -5){
		$('td#' + CommentID + ' td.commentBody').hide();
		$('td#' + CommentID + ' td.commentHidden').html('Шутка не удалась');		
		$('td#' + CommentID + ' td.commentHidden').show();
	}
	*/
	
	hide_comments();
	
}

function alert_title(element) {
	alert($(element).attr('title'));
}

function hide_comments() {	
	
	$('span.rating_1').each(function(){
		
		$('td.commentBody font',$(this).parents('td.comment')).show();
		$('td.commentBody span',$(this).parents('td.comment')).hide();
		
		if(parseInt($(this).html()) <= -5) {

			$('td.commentBody font',$(this).parents('td.comment')).hide();
			$('td.commentBody span',$(this).parents('td.comment')).html('<a href=javascript:void(0) title="Показать" >[Не актуально]</a>').click(function(){
				$('td.commentBody font',$(this).parents('td.comment')).show();		
				$('td.commentBody span',$(this).parents('td.comment')).html('');
				$('td.commentBody span',$(this).parents('td.comment')).hide();
			});;
			$('td.commentBody span',$(this).parents('td.comment')).show();
		}
		else {
			$('span.rating_2',$(this).parents('td.comment')).each(function(){
		
				$('td.commentBody font',$(this).parents('td.comment')).show();		
				$('td.commentBody span',$(this).parents('td.comment')).hide();
		
				if(parseInt($(this).html()) <= -5) {

					$('td.commentBody font',$(this).parents('td.comment')).hide();
					$('td.commentBody span',$(this).parents('td.comment')).html('<a href=javascript:void(0) title="Показать" >[Шутка не удалась]</a>').click(function(){
						$('td.commentBody font',$(this).parents('td.comment')).show();		
						$('td.commentBody span',$(this).parents('td.comment')).html('');
						$('td.commentBody span',$(this).parents('td.comment')).hide();
					});;
					$('td.commentBody span',$(this).parents('td.comment')).show();
				}
		

		
			});
			
		}
		
	});
	
}