function showAddForm(sender,code)
{
	if ($(sender).attr("showed")=='0')
	{
		$('a[showed=1]').attr('showed', '0');
        var t = $('span.commentBox');
        //var inp = $('input.commentBox');
        for (i=0; i<(t.length-1); i++)
        {
            $(t[i]).remove();
            //$(inp[i]).remove();
        }
        var html = $("#formForComments").html();
		var parent = $(sender).parent();
		parent = $(parent).parent();
		parent = $(parent).parent();
        //$(parent).css("display",'none');
		$(parent).append(html.replace('!CODE!',code));
		//$(parent).slideDown('fast');
		$(sender).attr("showed",1);
	}
	else
	{
		var parent = $(sender).parent();
		parent = $(parent).parent();
		parent = $(parent).parent();
		$(parent).children('span.commentBox').slideUp('fast',function(){
		$(this).remove();
		});
		$(sender).attr("showed",0);
	}
}

function showEditForm(sender,id)
{
	if ($(sender).attr("showed")=='0')
	{
        var html = "<textarea id=edit_"+id+">"+$("#comment_text_"+id).html()+"</textarea><input type=submit value='Сохранить' onClick=saveEdit('"+id+"')>";
		while (html.indexOf('<br>')!=-1)
		{
			html = html.replace('<br>','');
		}
		while (html.indexOf('<BR>')!=-1)
		{
			html = html.replace('<BR>','');
		}
		while (html.indexOf('<BR />')!=-1)
		{
			html = html.replace('<BR />','');
		}
		while (html.indexOf('<BR/>')!=-1)
		{
			html = html.replace('<BR/>','');
		}
		while (html.indexOf('<br />')!=-1)
		{
			html = html.replace('<br />','');
		}
		while (html.indexOf('<br/>')!=-1)
		{
			html = html.replace('<br/>','');
		}
		html = html.replace('<br />','');
		html = html.replace('<br/>','');
		$("#comment_text_"+id).html(html);
		$(sender).attr("showed",'1');

	}


}

function saveEdit(id)
{
	var html = $("#edit_"+id).attr("value");
	if (html.length < 10) {jAlert('Минимальная длина комментария - 10 символов','Ошибка'); return false;}
	$.post(siteURL+'/comments/edit/',
	{
		session:session,
		id:id,
		comment:html
	},function(data){
	//	alert(data);
		});
	var parent = $("#edit_"+id).parent();
	$(parent).empty();
	while (html.indexOf('\n')!=-1)
	html =  html.replace('\n','<br>');
	$(parent).html(html);
	$("#edit_btn_"+id).attr("showed",'0');
}

function deleteComment(sender,id)
{
	jConfirm("Вы уверены, что желаете удалить этот комментарий?","Требуется подтверждение",
	function(flag){
		if (flag==true)
		{
			jConfirm("Что сделать с ответами на этот комментарий? Ok - удаление, Cancel - они будут перемещены на уровень выше.",
			"Как поступим?",function(flag){
				if (flag==true) var f='true'; else f = 'false';
				$.post(siteURL+'/comments/del/',
				{

					id:id,
					flag:f,
					session:session
				},function(data){
					var div = $('div.comment[code='+id+']');
					if (flag==false)
					{

						$(div).attr("class","");
						$(div).children('.info').remove();
						$(div).children('.rating').remove();
						$(div).children('.commentText').remove();
						$(div).children('.actions').remove();
					}
					else
					{
						$(div).remove();
					}
				});
			});
		}
	});
}

function saveComment(sender,code)
{
	var textarea = $(sender).parent().children('textarea');
	var comment = $(textarea).attr("value");
    $("input.commentBox[type=submit]").attr("disabled","disabled");
	if (comment.length<10)
	{
		$("input.commentBox[type=submit]").removeAttr("disabled");
        jAlert('Минимальная длина комментариев - 10 символов!','Ошибка');
		return false;
	}
	var check = '';
	var captcha = $(sender).parent().children('input[name=captcha]');
	if (captcha.length>0) var check = $(captcha).attr("value");



	$.post(siteURL+'/comments/save/',
	{
		session:session,
		parent:code,
        pageId:$('input[name=pageId]').attr('value'),
		comment:comment,
		captcha:check
	},function(data){
			if (data=='captcha')
			{
				$("input.commentBox[type=submit]").removeAttr("disabled");
                jAlert('Проверочный код введен неверно!',"Ошибка");
				return false;
			}
			$(sender).parent().slideUp('fast',function(){
			$(sender).parent().parent().children('a[showed=1]').attr("showed",0);
			$(sender).parent().remove();
            $("input.commentBox[type=submit]").removeAttr("disabled");
            var c = $("img[alt=captcha]");
            var src = $(c).attr("src");
            $(c).attr("src","a");
            $(c).attr("src",src+"?"+ Math.random());
			});
	 		var div = $('div[code='+code+']');
			$(div).append(data);
			//}
			//else
		});
}

$(document).ready(function(){

	$('.actions').live('mouseover',function(){
            var a = $(this).children('.actions2');
			$(this).children('.actions2').css("display","inline");
		});
	$('.actions').live('mouseout',function(){
			$(this).children('.actions2').css("display","none");
		});
});

function comments_rating(id,action)
{
	if (action=='+')
	{
		$("#"+id+"_minus").attr("src",siteURL+"/images/comments_minus_inactive.gif").click(function(){comments_rating_again()}).attr("onClick","return false;");
		$("#"+id+"_plus").attr("src",siteURL+"/images/comments_plus.gif").click(function(){comments_rating_again()}).attr("onClick","return false;");
	}
	else
	{
		$("#"+id+"_minus").attr("src",siteURL+"/images/comments_minus.gif").click(function(){comments_rating_again()}).attr("onClick","return false;");
		$("#"+id+"_plus").attr("src",siteURL+"/images/comments_plus_inactive.gif").click(function(){comments_rating_again()}).attr("onClick","return false;");
	}
	var rating = $("#"+id+"_plus").parent().children().html();
	if (action == '-') rating--;
	else rating++;
	if (rating==0) var c='null';
	if (rating>0) { var c='plus'; rating = "+"+rating;  }
	if (rating<0) var c='minus';
	$("#"+id+"_plus").parent().children().attr("class",c);
	$("#"+id+"_plus").parent().children().html(rating);
	$.post(siteURL+'/comments/rate/',{
		session:session,
		id:id,
		action:action
	},function(data){});
}

function comments_rating_your()
{
	jAlert('Вы не можете голосовать за собственные комментарии','Ууупс!');
}

function comments_rating_again()
{
	jAlert('Вы уже голосовали за этот комментарий','Ууупс!');
}

function comments_rating_guest()
{
	jAlert('Гости не могут голосовать за комментарии','Ууупс!');
}

function trim(str) {
    var newstr = str.replace(/^\s*(.+?)\s*$/, "$1");
    if (newstr == " ") {
        return "";
    }
    return newstr;
}

function drop_spaces(str) {
    var newstr = trim(str); //функцию trim() см. выше
    return newstr.replace(/(\s)+/g, "");
}