function comments_setup() {
    
    $('.comments-link').live('click', comments_show);
    $('.f-comment').live('submit', comments_post);
    $('.f-comment input[type="submit"]').live('click', comments_post); /* IE needs this */
    
}

function comments_show(e) {
    
    // Sometimes e.target points to an element within the <a> instead of the <a>
    if($(e.target).is('a')) {
        var comments_link = e.target; 
    } else {
        var comments_link = $(e.target).parents('.comments-link').get(0);
    }
    
    var item = $(comments_link).parents('.item').get(0);
    
    if($(item).hasClass('detail')) {
        
        $(item).toggleClass('detail');
        $(item).children('.item-comments').slideUp('fast', function() {
            $(comments_link).siblings('.comments-arrow').remove();
            $(comments_link).attr('title', 'Show comments for this item');
            $(this).remove();
        });
        
    } else {
    
        $.get(comments_link.href, function(data) {
            if(data.success) {
                $(item).toggleClass('detail');
                var comments = $('<div class="item-comments">' + data.comments_list + data.comments_form + '</div>').hide();
                $(comments_link).parent().append('<div class="comments-arrow"></div>');
                $(comments_link).attr('title', 'Hide comments for this item');
                comments.appendTo($(item)).slideDown('fast');
            }
    	}, 'json');
    
    }
    
    if(e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
    return false;
    
}

function comments_refresh(item) {
    
    var comments_link = $(item).find('.comments-link').get(0);
    
    $.get(comments_link.href, function(data) {
        if(data.success) {
            
            $(item).find('.comment-list').fadeOut('fast').remove();
            
            if($(comments_link).hasClass('no-comments')) {
                $(comments_link).removeClass('no-comments');
                $(comments_link).attr('title', 'Hide comments for this item');
                $(comments_link).prepend('<span class="count">1</span>');
                $(comments_link).children('.rest').text(" comment");
            }
            
            $(item).find('.comments-link .count').text(data.comments_count);
            var comments_list = $(data.comments_list).hide();
            comments_list.prependTo($(item).children('.item-comments')).fadeIn('slow');
            
        }
	}, 'json');
    
}

function comments_post(e) {
    
    // Sometimes e.target points to an element within the <form> instead of the <form>
    if($(e.target).is('form')) {
        var comments_form = e.target; 
    } else {
        var comments_form = $(e.target).parents('.f-comment').get(0);
    }
    
    var item = $(comments_form).parents('.item').get(0);
    $(item).find('.comment-notice').remove();
    
    $(comments_form).after('<div class="comment-posting" style="display: none">Your comment is being posted.</div>');
    
    $(comments_form).fadeOut('fast');
    $(item).find('.comment-posting').fadeIn('fast');
    
    $.post(comments_form.action, $(comments_form).serialize(), function(data) {
        
        if(data.success) {

            $(comments_form).before('<p class="notice success comment-notice" style="display: none">Your comment has been posted successfully.');
            $(comments_form).find('.errorlist').remove();
            $(comments_form).find('li').removeClass('error');
            comments_form.reset();
            
        } else {
        
            $(comments_form).before('<p class="notice warning comment-notice" style="display: none">Your comment could not be posted. Ensure you\'ve filled out all required fields correctly.');
            
            for(field in data.errors) {
                $(comments_form).find('#id_' + field).after(data.errors[field]);
                $(comments_form).find('#id_' + field).parents('li').first().addClass('error');
            }
            
        }
        
        $(item).find('.comment-posting').fadeOut('fast', function() { $(this).remove(); });
        $(item).find('.comment-notice').fadeIn('fast');
        
        if(data.success) {
            $(item).find('.comment-notice').fadeOut(4000, function() { $(this).remove(); });
        }
        
        $(comments_form).fadeIn();
        
        if(data.success) {
            comments_refresh(item);
        }
        
    }, 'json');
    
    if(e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
    return false;
    
}
