$(document).ready(function($) {
// подзагрузка картинки
var img = new Image();
img.src = "images/dif/plus-full.png";

// ответить
var i = 1
$('#answer_form').submit(function() {
	text = $('#answer').val();

	if ( $('#user_name').length > 0 )
	{
		user = $('#user_name').val();
//		$.cookie("who", user, { expires: 300, path: '/' });
	}

	if ( text )
	{
		$.post('class/ajax/send_answer.php', 'id='+i+'&'+$(this).serialize()+'&user='+user, function(data) {
 			$('#new_answers').append(data);
			$('#node'+i).slideToggle("fast");
			i++;
		});
	}

	$('#answer').val('');
	return false;
});

// рисунок AJAX
$('#ajax_gif').hide().ajaxStart(function() {
	var top = $('#button').position().top-130;
	var left = $('#button').position().left+280;
	$(this).css({top:top,left:left}).show();
}).ajaxStop(function() {$(this).hide();});

// поставить плюс
$('.plus-img').click(function() {

	// для alert
	var top = $(this).position().top;
	var left = $(this).position().left+20;

	// если незалогинен
	if ( user_logged == 0 )
	{
		$('.alert').html("Необходимо <a href='./taolun/'>войти</a> или <a href='./taolun/member.php?action=register'>зарегистрироваться</a> чтобы оценивать<div class='small-alert'>(кликните, чтобы убрать)</div>").css({top:top,left:left}).show("fast");
		return 0;
	}

	// если уже плюсовал
	if ( $(this).attr('src') == 'images/dif/plus-full.png' )
	{
		$('.alert').html("Оценивать можно только один раз<div class='small-alert'>(кликните, чтобы убрать)</div>").css({top:top,left:left}).show("fast");
		return 0;
	}

	var what = $(this).siblings('input[name=what]').val();
	var id = $(this).siblings('input[name=id]').val();
	var whom = $(this).siblings('input[name=whom]').val();

	// если свое
	if ( whom == user )
	{
		$('.alert').html("Нельзя оценивать себя<div class='small-alert'>(кликните, чтобы убрать)</div>").css({top:top,left:left}).show("fast");
		return 0;
	}

	var img = $(this);

	// post запрос
	$.post('class/ajax/translation_plus.php', 'id='+id+'&what='+what+'&user='+user+'&whom='+whom, function(data) {
		img.attr('src', 'images/dif/plus-full.png');
		img.siblings('.plus-total').text(Number(img.siblings('.plus-total').text()) + 1)
	});

});

// убрать alert
$('.alert').click(function() {
	$(this).hide('fast');
});

// вставить комментирование
$('.comment-ref').live('click', function() {
	var info = $(this).attr('id').split('-');

	$('#comments-'+info[1]+'-'+info[2]).removeClass('nocomments').append("<form id='addcomment-"+info[1]+"-"+info[2]+"-"+info[3]+"' class='add-comment-form'><table><tr><td><textarea id='textarea-"+info[1]+"-"+info[2]+"' class='comment-textarea' rows='3' cols='50' name='answer'></textarea></td><td><input class='comment-submit' type='submit' value='Комментировать'></td></tr></table></form>")
	$('#textarea-'+info[1]+'-'+info[2]).focus()
	$(this).hide();

	return false;
});

// комментировать
var j = 1
$('.comment-submit').live('click', function() {

	var info = $(this).closest("form").attr('id').split('-');

	var text = encodeURIComponent($('#textarea-'+info[1]+'-'+info[2]).val());

	if ( $('#user_name').length > 0 )
	{
		user = $('#user_name').val();
//		$.cookie("who", user, { expires: 300, path: '/' });
	}

	var form = $(this).closest("form");
	if ( text )
	{
		$.post('class/ajax/send_translation_comments.php', 'q='+info[3]+'&js_id='+j+'&id='+info[2]+'&type='+info[1]+'&text='+text+'&user='+user, function(data) {
			form.hide().html('');
 			$("#comments-"+info[1]+"-"+info[2]).append(data);
			$('#node_com'+j).slideToggle("fast");
			j++;
			$('.comment-ref').show();
		});
	}
	return false;
});

// открыть редактирование
$('.edit-ref').live('click', function() {
	var info = $(this).attr('id').split('-');

	$(this).parent().siblings('.answer-body').children('div').html('');
	$('#formedit-'+info[1]+'-'+info[2]).show();
})

// открыть редактирование заголовка
$('.edit-title-img').live('click', function() {
	$(this).parent().siblings('form').show();
	$(this).parent().html('');
})

// открыть редактирование комментария
$('.edit-comment-img').live('click', function() {
	$(this).parent().siblings('form').show();
	$(this).parent().siblings('.comm-text').html('');
	$(this).parent().hide();
})

// редактировать
$('.edit-submit').live('click', function() {
	var form = $(this).closest("form");

	var info = form.attr('id').split('-');

	var text = $('#editarea-'+info[1]+'-'+info[2]).val();

	if ( text )
	{
		$.post('class/ajax/translation_edit.php', 'id='+info[2]+'&type='+info[1]+'&text='+text)
	}

	text = out_text_format(text);

	if ( info[1] == 'title' )
	{
		$('h1').html(text+" <img src='images/dif/edit.png' class='edit-title-img' />");
	}
	else if ( info[1] == 'comment' )
	{
		form.siblings('.comm-text').html(text);
		form.siblings('.comm-meta').show();
	}
	else
	{
		form.siblings('div').html(text);
	}
	form.hide();

	return false;
})

// формировать текст
function out_text_format(text)
{
	text = text.replace(/^>(.+)\n?/mg, "<div class='quote-div'>$1</div>");
	text = text.replace(/<\/div>\\n<div class='quote-div'>/g, "<br />");

	text = text.replace(/\\(.+?)\\/g, "<span class='quote-div'>$1</span>");

	// links
	text = text.replace(/\"(.+?)\":(http:\/\/[^ \n]+[^.,\n ()]+) /g, "<a href='$2'>$1</a>");
	text = text.replace(/(\W)(http:\/\/[^ \n]+[^.,\n ()]+)/g, "$1<a href='$2'>$2</a>");
	text = text.replace(/_([а-яА-Я一-鶴]+?)_/g, "<a href='slovo.php?ch=$1'>$1</a>");
	// wiki link
	text = text.replace(/\[\[(.+?)\]\]/g, function(match, p1)
		{
			return "<a href='wiki/page/"+p1.replace(/ /g, '_')+"'>"+p1+"</a>";
		}
	)

	text = text.replace(/\n/g, "<br />");

	text = text.replace(/:\)/g, ":smile:");
	text = text.replace(/:([^<>/]+?):/g, "<img src='images/smiles/$1.gif' />");

	return text;
}

});
