﻿jQuery.fn.add_comment = function(){
	jQuery(this).click(
	function () {
		$('#adcom').css('display','');    
		})	  
	Thover(jQuery(this));
}

jQuery.fn.remove_comment = function(){
	jQuery(this).click(
	function () {
		/* AJAX. Удаление комментария */	  	
		var idtemp = "#"+jQuery(this).parent().parent().attr('id');
		var obj = jQuery(this).parent().parent().parent();
		obj.find('dt'+idtemp).replaceWith("");
		obj.find('dd'+idtemp).replaceWith("");
    })
}

jQuery.fn.gallery_remove_comment = function(){
	jQuery(this).click(
	function () {
		/* AJAX. Удаление комментария */	  	
		if(confirm('Вы уверены, что хотите удалить комментарий?')) {
			var comm_id = jQuery(this).parent().parent().attr('id');
			var obj = jQuery(this).parent().parent().parent();
			$.ajax({
	            type: "GET",
	            dataType: "json",
	            url: "/engine/ajax/ajax_controller.php",
	            data: "m=gallery&a=removecomment&comm_id=" + comm_id,
	            success: function(json){
	                if( json.success == true ) {
	                    obj.find('dt#' + comm_id).replaceWith("");
						obj.find('dd#' + comm_id).replaceWith("");
	                } else {
	                    alert(json.error);
	                }
	            }
	        });
		}
    })
}

jQuery.fn.edit_comment = function(){
	jQuery(this).click(
	function () {	
		
		var all = jQuery(this).parent().parent().parent();
		all.children().find('a.compose').css('visibility','visible');
		all.find('div.admin').css('visibility','visible').show();;
		all.find('div.admin2').css('visibility','hidden').hide();
		all.children().find('form.die').replaceWith("<p>" + all.children().find('span').text() + "</p>");
		all.children().find('span').replaceWith("");
		
	
		var obj = jQuery(this).parent().parent();
	  	obj.find('p').replaceWith("<form action='' class='die' name='editcomment' method='post'><fieldset class='text'><input type='hidden' name='subaction' value='addcomment' /><input type='hidden' name='comment_id' id='comment_id' value='"+jQuery(this).attr('id')+"' /><input type='hidden' name='subsubaction' value='editcomment' /><textarea name='comments' id='" + obj.attr("id") + "_comm'>" + jQuery(this).parent().parent().find('p').text() + "</textarea></fieldset></form>");
		obj.find('form.die').after("<span>"+jQuery(this).parent().parent().find('textarea').text()+"</span>");
		obj.find('span').css('display','none');
		obj.find('a.compose').css('visibility','hidden');
		obj.find('div.admin').hide();
		obj.find('div.admin2').css('visibility','visible').show();
		
    })
	
}
jQuery.fn.cancel_comment = function(){
	jQuery(this).click(
	function () {
		var obj = jQuery(this).parent().parent();
		obj.find('a.compose').css('visibility','visible');
		obj.find('div.admin').css('visibility','visible').show();
		obj.find('div.admin2').hide();
		obj.find('form.die').replaceWith("<p>" + jQuery(this).parent().parent().find('span').text() + "</p>");
		obj.find('span').replaceWith("");
	})
	Thover(jQuery(this));
}
jQuery.fn.ok_comment = function(){
	jQuery(this).click(
	function () {
		var obj = jQuery(this).parent().parent();
		obj.find('span').replaceWith("");
		obj.find('a.compose').css('visibility','visible');
		obj.find('div.admin').css('visibility','visible').show();
		obj.find('div.admin2').css('visibility','hidden').hide();
		
		/* AJAX. Ниже идет присвоение измененного текста в textarea параграфу <p> */
		obj.find('form').replaceWith("<p>" + jQuery(this).parent().parent().find('textarea').val() + "</p>");
	})
	Thover(jQuery(this));
}

jQuery.fn.gallery_ok_comment = function(){
	jQuery(this).click(
	function () {
		var obj = jQuery(this).parent().parent();
		obj.find('span').replaceWith("");
		var textarea = obj.parent().find('textarea');
		var text = textarea.val();
		var comm_id = textarea.attr('id');
		$.ajax({
            type: "POST",
            dataType: "json",
            url: "/engine/ajax/ajax_controller.php?m=gallery&a=editcomment" + "&comm_id=" + comm_id,
            data: "text=" + text,
            success: function(json){
                if( json.success == true ) {
                    obj.find('a.compose').css('visibility','visible');
					obj.find('div.admin').css('visibility','visible').show();
					obj.find('div.admin2').hide();
					obj.find('form').replaceWith("<p>" + obj.parent().find('textarea').val() + "</p>");
                } else {
                    alert(json.error);
                }
            }
        });
	})
	Thover(jQuery(this));
}

jQuery.fn.comments_fix = function(){
	var h = jQuery(this).height();
	jQuery(this).find('div').height(h+"px");
	var temp = h-240;
	jQuery(this).find('p').css('height', temp+"px");
	jQuery(this).find('p.more').css('height', '20px');
}
function Thover(t){
	t.hover(
	function () {
	  	t.css('border-bottom', '1px dashed #fff');
    },
	function () {
	  	t.css('border-bottom', '1px dashed #662D91');	
    })
}
jQuery.fn.rhover = function(){
	jQuery(this).parent().children('a').hover(
	function () {
	  	jQuery(this).css('border-bottom', '1px dashed #fff');
    },
	function () {
	  	jQuery(this).css('border-bottom', '1px dashed #662D91');	
    })
}
