$(document).ready(function(){
	$("a.viewLoginForm").click(function(){
		$("div#commentLoginForm").animate({opacity: 'toggle'}, 'slow');
		if($("form[name='login']")){
			$("form[name='login'] input[name='user']").focus();
		}
	});
	
	$("form#articleCommentForm input#saveArticleComment").click(function(){
		var commentVal = $("form#articleCommentForm textarea#articleComment").val();
		var articleId = $(this).attr('articleid');
		var phpsessionid = $(this).attr('phpsid');
		
		if (commentVal != '' && !isNaN(articleId) && phpsessionid != '') {
			$("ul#articleComments li#empty").hide();
			
			$.post("/includes/article/ajax-comment.php", { PHPSESSID: phpsessionid, articleid: articleId, comment: commentVal },
			  function(data){
			  	if(data != 0){
					$("span#newArticleCommentStatus").hide();
					$("ul#articleComments").append(data);
					$("ul#articleComments li:last").fadeIn('slow');
					$("form#articleCommentForm textarea#articleComment").val('');
				} else {
					$("span#newArticleCommentStatus").html('Kunne ikke lagre kommentaren, prøv igjen.').show();
				}
			  });
		} else {
			alert('Du må skrive en kommentar før du kan lagre den.');
		}
	});
});

