$(document).ready(function() {
	var allowanonymous = false;
	function testing() {
		if (!allowanonymous) {		
			//allowanonymous = true;
			//$.facebox({ ajax: '/modules/mod_addcomment_anonymous.html' });
			//return false;			
		} else {
			return true;
		}
	}
	
	function assignCommentAjax() {
		
		   $('#anonymouscommentform').ajaxForm({
			beforeSubmit:testing, 
			success:postcomment
			});
    
			
		$('#commentform').ajaxForm({
           success:postcomment
        });
		
	}
	
	assignCommentAjax();
	
/*
		$("#comment").focus(function() {
			var theheight = $("#comment").height();
			if (theheight != 120) {
				$("#comment").animate({"height": "120"}, "fast");
			}
		});*/
	
		function postcomment(responseText, statusText) {			
			//alert(responseText);
			// Disable the textarea and comment button for now - to prevent double adding.
			//$("input").attr("disabled","disabled");
			$("#commentsubmit").attr("disabled","true");
			$("#comment").attr("disabled","true");
			$("#comment").addClass('progress');
            $("#commentsubmit").addClass('disabledcommentsubmit');
			$("#commentsubmit").addClass('progress');
						
			// Take the inserted comment ID and append it			
			if (IsNumeric(responseText)) {
				var theurl = "/modules/mod_comments.php?commentid=" + responseText;
				// Ajax load the last comment into the #commentsHolder			
				$.ajax({
					url: theurl,
					cache: false,
					success: function(html){
						// Shrink and clear the textarea
						$("#comment").val("");
						$("#nocomments").hide();
						$("#commentsHolder").append(html);
						
					}, 
					complete: function() {					
						// When completed the ajax insertation, animate the showing of the new comment
						//$("div[id=singlecomment][class=hidden]").slideDown("fast");					
						$("div[id=singlecomment][class=hidden]").animate({"height": "toggle", "opacity": "toggle"}, "fast");	
						$("#commentsubmit").removeAttr("disabled");
						$("#comment").removeAttr("disabled");
						$("#comment").removeClass('progress');
						$("#commentsubmit").removeClass('disabledcommentsubmit');
						$("#commentsubmit").removeClass('progress');						
					}
				});} else {
				// We didn't get a numeric response
				alert("error occured");
				$("#commentsubmit").removeAttr("disabled");
				$("#comment").removeAttr("disabled");
				$("#comment").removeClass('progress');
				$("#commentsubmit").removeClass('disabledcommentsubmit');
				$("#commentsubmit").removeClass('progress');
			}				
		}		
    });
