//-------------------------------------
// Mako comment functions
//-------------------------------------

(function()
{
	/*
	* Toggle loading and hide form while sending a request
	*
	* @param int - status (on or off)
	* @return void
	* @requires jQuery
	*/
	
	Mako.toggleCommentLoading = function(status)
	{
		if(status == 1)
		{
			$("#user_comments").css({display:"none"});
			$("#loading").css({display:"block"});
			$("#comments").html(""); // quick fix for "popping"
		}
		else
		{
			$("#user_comments").css({display:"block"});
			$("#loading").css({display:"none"});
		}
	};
	
	/*
	* Ajax request that loads the newest comments
	*
	* @param int - article id
	* @return void
	* @requires jQuery
	*/
	
	Mako.getComments = function(id)
	{
		$.ajax({
			type: "GET",
			url: "/?act=news&do=getallcomments&id="+id, // url: "/?act=news&do=getcomments&id="+id,
			cache: false,
			error: function()
			{
				alert("Error: Failed to contact the server.");
			},
			success: function(data)
			{
				$("#comments").html(data);
			}
		});
	}
	
	/*
	* Ajax request that adds comment
	*
	* @param string - comment
	* @param int - article id
	* @return void
	* @requires jQuery
	*/
	
	Mako.addComment = function(c, id)
	{
		if(c.length > 10)
		{	
			$.ajax({
				beforeSend: function()
				{
					Mako.toggleCommentLoading(1);
				},
				type: "POST",
				url: "/?act=news&do=newcomment",
				data: "comment="+c+"&id="+id,
				error: function()
				{
					alert("Error: Failed to contact the server.");
				},
				success: function()
				{
					Mako.getComments(id);
				},
				complete: function()
				{
					$("#comment").val("");
					Mako.toggleCommentLoading(0);
				}
			});
		 }
		else
		{
			alert("Your comment is too short!");
			$("#comment").focus();
		}
	};
	
	/* 
	* Ajax request that gest all comments
	*
	* @param int - article id
	* @return void
	* @requires jQuery
	*/
	
	Mako.getAllComments = function(id)
	{
		$.ajax({
			beforeSend: function()
			{
				Mako.toggleCommentLoading(1);
			},
			type: "GET",
			url: "/?act=news&do=getallcomments&id="+id,
			cache: false,
			error: function()
			{
				alert("Error: Failed to contact the server.");
			},
			success: function(data)
			{
				$("#comments").html(data);
			},
			complete: function()
			{
				Mako.toggleCommentLoading(0);
			}
		});
	};
	
	/*
	* Ajax request that deletes comment
	*
	* @param string - auth key
	* @param int - comment id
	* @param int - article id
	* @return void
	* @requires jQuery
	*/
	
	Mako.deleteComment = function(key, id, aid)
	{
		var ok = confirm("Do you really want to delete this comment?");
		
		if(ok)
		{
			$.ajax({
				beforeSend: function()
				{
					Mako.toggleCommentLoading(1);
				},
				type: "POST",
				url: "/?act=news&do=deletecomment",
				data: "key="+key+"&id="+id,
				error: function()
				{
					alert("Error: Failed to contact the server.");
				},
				success: function()
				{
					Mako.getComments(aid);
				},
				complete: function()
				{
					Mako.toggleCommentLoading(0);
				}
			});
		}
		else
		{
			alert("Ok, no action was taken.");
		}
	};
	
	/*
	* Ajax request that gets the comment to edit
	*
	* @param int - comment id
	* @return void
	* @requires jQuery
	*/
	
	Mako.editComment = function(id)
	{
		$.ajax({
			type: "GET",
			url: "/?act=news&do=editcomment&id="+id,
			cache: false,
			error: function()
			{
				alert("Error: Failed to contact the server.");
			},
			success: function(data)
			{
				if(data == "ERROR")
				{
					alert("Error: You do not have the permission to edit this comment.");
				}
				else
				{
					$("#c"+id).fadeOut("slow", function(){$("#c"+id).html(data);});
					$("#c"+id).fadeIn("slow", function(){$("#ecomment"+id).focus();});
				}
			}
		});
	};
	
	/*
	* Will save the edited comment
	*
	* @param string - comment
	* @param int - comment id
	* @param int - article id
	*/
	
	Mako.saveEditedComment = function(c, id, aid)
	{
		if(c.length > 10)
		{
			$.ajax({
				beforeSend: function()
				{
					Mako.toggleCommentLoading(1);
				},
				type: "POST",
				url: "/?act=news&do=saveeditedcomment",
				data: "comment="+c+"&id="+id,
				error: function()
				{
					alert("Error: Failed to contact the server.");
				},
				success: function()
				{
					Mako.getComments(aid);
				},
				complete: function()
				{
					Mako.toggleCommentLoading(0);
				}
			});
		}
		else
		{
			alert("Your comment is too short!");
			$("#ecomment"+id).focus();
		}
	}
})();