function toggleButtons(state)
{
	$(".commentReplyButton").attr("disabled",state);		
	$(".commentReportButton").attr("disabled",state);
	$(".commentEditButton").attr("disabled",state);
	$(".commentDeleteButton").attr("disabled",state);
	$(".commentPlus").attr("disabled",state);
	$(".scommentMinus").attr("disabled",state);
}
function treatEvent(parent,id,button,eventRE)
{
	if( $("."+eventRE,parent).length==0 )
	{
		toggleButtons("disabled");
		$(".displayCommentInput").replaceWith("");
		$(".commentAddButton").fadeIn();
		button.hide();
		button.parent().append("<img class='loadgif' src='"+WWW_ROOT+"images/ajax-loader.gif' style='margin:15px;' />");
		$.post(WWW_ROOT+"ajax/getCommentReplyBox.php",{id:id,action:eventRE},function(data)
		{
			parent.append(data);
			$("."+eventRE,parent).hide();
			$("."+eventRE,parent).fadeIn();
			$(".loadgif").replaceWith("");
			button.show();
			toggleButtons("enabled");
		});
	}
	else
		$(".displayCommentInput").fadeOut(function()
		{
			$(".displayCommentInput").replaceWith("");
			$(".commentAddButton").fadeIn();
		});
}
function loadJQ()
{
	//ADD CSS for comments
	$(".commentPlus").html("<img src='"+WWW_ROOT+"admin/images/icons/add.png'>");
	$(".commentPlusDisabled").html("<img src='"+WWW_ROOT+"admin/images/icons/addDisabled.png'>");
	$(".commentMinus").html("<img src='"+WWW_ROOT+"admin/images/icons/delete.png'>");
	$(".commentMinusDisabled").html("<img src='"+WWW_ROOT+"admin/images/icons/deleteDisabled.png'>");
	$(".commentReportButton").html("<img src='"+WWW_ROOT+"admin/images/icons/bell_delete.png'> report");
	$(".commentEditButton").html("<img src='"+WWW_ROOT+"admin/images/icons/comment_edit.png'> edit");
	$(".commentDeleteButton").html("<img src='"+WWW_ROOT+"admin/images/icons/comment_delete.png'> delete");
	$(".commentReplyButton").html("<img src='"+WWW_ROOT+"admin/images/icons/pencil_go.png'> reply");
	$(".commentAddButton").prepend("<img src='"+WWW_ROOT+"admin/images/icons/comment_add.png'>");
/*	$(".commentRating").editable(WWW_ROOT+"admin/ajax/editComments.php",{
		indicator	: "Saving...",
		tooltip		: 'Click to edit...',
		style		: "display: inline;width:40px;",
	});	
	*/	
	$(".commentAddButton").click(function()
	{
		var parent = $(this).parent();
		var table  = $(this).attr("id").split("_")[0];
		var id     = $(this).attr("id").split("_")[1];

		$(".displayCommentInput").replaceWith("");
		$(this).hide();
		$(".commentsGroup").prepend("<img class='loadgif' src='"+WWW_ROOT+"images/ajax-loader' style='margin:4px; margin-left:15px'/>");
		$.post(WWW_ROOT+"ajax/getCommentReplyBox.php",{id:id,table:table,action:'add'},function(data)
		{
			parent.prepend(data);
			$(".reply",parent).hide();
			$(".loadgif").replaceWith("");
			$(".reply",parent).fadeIn();
		});
	});
	$(".commentReplyButton").click(function()
	{
		var parent = $(this).parent().parent(); 
		var id = parent.attr("id").split("_")[1];
		var button = $(this);
		treatEvent(parent,id,button,"reply")
	});	
	$(".commentEditButton").click(function()
	{
		var parent = $(this).parent().parent(); 
		var id = parent.attr("id").split("_")[1];
		var button = $(this);
		treatEvent(parent,id,button,"edit")
	});
	$(".commentDeleteButton").click(function()
	{
		var parent = $(this).parent().parent().parent();
		var id = $(this).parent().parent().attr("id").split("_")[1];
		parent.fadeOut(function()
		{
			parent.replaceWith("");
		});
		$.post(WWW_ROOT+"ajax/editComments.php",{action:'delete',id:id});	
	});
	$(".commentPlus").click(function()
	{
		var parent = $(this).parent().parent().parent();
		var id = parent.attr("id").split("_")[1];
		$(this).parent().html("<img src='"+WWW_ROOT+"admin/images/icons/addDisabled.png'> <img src='"+WWW_ROOT+"images/ajax-loader+-.gif'/> <img src='"+WWW_ROOT+"admin/images/icons/deleteDisabled.png'/>");
		$.post(WWW_ROOT+"ajax/editComments.php",{action:'+',parent_id:id},function(data)
		{
			$(".commentsGroup").replaceWith(data);
			loadJQ();
		});
		
	});
	$(".commentMinus").click(function()
	{
		var parent = $(this).parent().parent().parent();
		var id = parent.attr("id").split("_")[1];
		$(this).parent().html("<img src='"+WWW_ROOT+"admin/images/icons/addDisabled.png'> <img src='"+WWW_ROOT+"images/ajax-loader+-.gif'/> <img src='"+WWW_ROOT+"admin/images/icons/deleteDisabled.png'/>");
		$.post(WWW_ROOT+"ajax/editComments.php",{action:'-',parent_id:id},function(data)
		{
			$(".commentsGroup").replaceWith(data);
			loadJQ();												
		});		
	});
	$(".commentReportButton").click(function()
	{
		var parent = $(this).parent().parent().parent();
		var id = parent.attr("id").split("_")[1];
		if(confirm("Are you sure you want to report this comment as spam?"))
		{
			alert("Fine! Comment Reported! An admin will take care of that mo-fo!");
			$.post(WWW_ROOT+"ajax/editComments.php",{action:'report',id:id});
		}	
	});
}

$(function(){loadJQ()});

