function insertComments() {

    $$('div.comments').each( function(comment_holder) {     
        comment_holder.getElements('input[name=post_id]').each( function(post_id_input) {
            var post_id = Number(post_id_input.value); 
            var comments = getPostComments(post_id);     
                           
            addCommentLinkText($(post_id+'_comment_link'), comments.length);

            addComments(comment_holder, comments); 
            
            var inputs = new Element('div', {'id':post_id+'_inputs'});
            inputs.set('html', addCommentInputs(comment_holder));
            inputs.injectInside(comment_holder);
        });                                                                              
    });
    
}

function addCommentLinkText(comment_link, comment_length) {
    if (comment_length == 0) {
        comment_link.innerHTML = 'Add a comment';
    } else if (comment_length == 1) {
        comment_link.innerHTML = '1 Comment';
    } else {
        comment_link.innerHTML = comment_length + ' Comments';
    }                                             
    //else if (comment_length > 0 && comment_length < 2) {
    //     comment_link.innerHTML = 'A Couple Comments';
    // }                                             
    //else if (comment_length >= 2){
    //    comment_link.innerHTML = 'A Handful of Comments';
    //}
}
   
function toggleComments(post_id) {
    if ($(post_id+'_comments').getStyle('display').test('none')) {
        $(post_id+'_comments').setStyle('display', 'block');
        $(post_id+'_comment_link').innerHTML = 'Hide Comments';    
    }
    else {
        $(post_id+'_comments').setStyle('display', 'none');
        addCommentLinkText($(post_id+'_comment_link'), (getPostComments(post_id)).length);
    }
} 

function addComments(comment_holder, comments) {
    	comments.each ( function(comment) {
        	comment_holder.innerHTML += '<div class="comment"><div class="comment_name">' + comment.name + '</div> <div class="comment_text">' + comment.comment + '</div></div><div class="clear">&nbsp;</div>';
    	});
}
   
function addCommentInputs(comment_holder) { 

    return '<div class="comment_name"><input type="text" class="name" name="'+comment_holder.id+'_name" id="'+comment_holder.id+'_name" value="Name" onFocus="if(this.value == \'Name\') { this.value = \'\'; }"></input></div>' + 
           '<div class="comment_text"><input type="text" class="text" name="'+comment_holder.id+'_body" id="'+comment_holder.id+'_body" value="Comment" onFocus="if(this.value == \'Comment\') { this.value = \'\'; }"></input>' + 
           '<input type="button" value="Add" onclick="addComment(\''+comment_holder.id+'\')" /></div>' +
		   '<div class="clear">&nbsp;</div>';
}  
   
function addComment(comment_holder_id) {

    var post_id = comment_holder_id.replace(/\D/g, "");
    var name    = $(comment_holder_id+"_name").value;
	var body    = $(comment_holder_id+"_body").value; 
	
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'http://www.tylerneu.com/cgi-bin/addcomment.pl?postid='+encodeURIComponent(post_id)+'&name='+encodeURIComponent(name)+'&body='+encodeURIComponent(body);
    document.getElementsByTagName('head')[0].appendChild(script);      
    
    // Inject the new comment at the bottom of the comment list, but before the inputs
    var comment = new Element('div', { 'class' : 'comment' });
    comment.set('html', '<div class="comment_name">' + name + '</div> <div class="comment_text">' + body + '</div><div class="clear"></div>'); 

    comment.injectBefore($(post_id+'_inputs'));
    
	$(post_id+'_comments_name').value = '';
	$(post_id+'_comments_body').value = '';
}

window.addEvent('domready', function(){
  insertComments();
});
