$(document).ready(function() {
    $('form#commentform').jcomm();
    $("#comment-personaldetails").hide();
    $("span#showinfo").bind("click", function() {
	$("#comment-personaldetails").slideDown();
	$(this).hide();
	$("#hideinfo").show();
	return false;
    }).mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");}).show();
    $("span#hideinfo").bind("click", function() {
	$("#comment-personaldetails").slideUp("slow");
	$(this).hide();
	$("#showinfo").show();
	return false;
    }).mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");}).hide();
});

$.fn.jcomm = function(e) {
    $(this).submit(function(e){
	e.preventDefault();
	$.ajax({
	    type: "POST",
	    url: scriptpath + "/comments-ajax.php", 
	    data: $.param($('#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 ($('ul#commentlist li:last-child').attr('class') == "alt") {cls = ''};
		    $('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>');
		    $('#comment-tabs').triggerTab(1);
	    	    $('ul#commentlist li:last-child').fadeTo("fast",1, function() {$(this).ScrollTo(800)});
		    if (json.response[1] && json.response[1].alert == "true") {
			Growl.show('<p>' + json.response[1].msg + '</p>');
		    }
		    $('textarea#comment').val("");
		    $('span#comments').html((Math.round($('#comments').text())+1));
		    $('h3#postcomment').html('Comment Added');
	    	    $('li#nocomment').remove();
	    	    $('li#hidelist').remove();
		}
	    }
	});
    });
}
$.fn.formToArray = function() {
    var a = [];
    var q = 'input,textarea';
    $(q, this).each(function() {
        var n = this.name;
        var t = this.type;
        a.push({name: n, value: this.value});
    });
    return a;
};
