jQuery(function() {
    jQuery("form#commentform").jcomm();
});

jQuery.fn.jcomm = function(e) {
    jQuery(this).submit(function(e){
	e.preventDefault();
        Growl.register();
	jQuery.ajax({
	    type: "POST",
            timeout: 5000,
	    url: scriptpath + "/comments-ajax.php", 
	    data: jQuery.param(jQuery('#commentform').formToArray()), 
	    dataType: "json",
	    success: function(json) {
		var r = json.response[0];
    		if (r.error == "true") {
		    Growl.show('<p>' + r.msg + '</p>');
		} else {
		    var cls = 'alt';
		    if (jQuery('ul#commentlist li:last-child').attr('class') == "alt") {cls = ''};
		    jQuery('ul#commentlist').append('<li class="'+cls+'" id="comment-'+r.id+'" style="display:none;"><p class="comment-meta"><span class="commenter"><cite>Comment by <a href="'+r.link+'">'+r.author+'</a></cite></span><span class="commentDate"> at <a href="#comment-'+r.id+'"">'+r.date+'</a></span></p>'+r.text+'</li>');
	    	    jQuery('ul#commentlist li:last-child').fadeIn("slow", function() {jQuery(this).slideDown()});
		    if (json.response[1] && json.response[1].alert == "true") {
			Growl.show('<p>' + json.response[1].msg + '</p>');
		    }
		    jQuery('textarea#comment').val("");
		    jQuery('span#comments-number').html((Math.round(jQuery('#comments').text())+1));
	    	    jQuery('li#nocomment').remove();
	    	    jQuery('li#hidelist').remove();
		}
	    }
	});
    });
}