(function ($) {
    function show_comments () {
        $(this).hide();
        $('#comments').show();
        return true;
    }
    
    var converter = new Showdown.converter();
    function update_preview () {
        var text = $('#id_content').val().replace(/</g,'&lt;');
        $('#preview-body').html(converter.makeHtml(text));
    }
    
    var comment_preview_timer = false;
    function comment_preview () {
        clearTimeout(comment_preview_timer);
        comment_preview_timer = window.setTimeout(update_preview, 400);
    }
    
    $(function () {
        $('body.collapse-comments .comments').hide();
        $('body.collapse-comments .show-comments-link').click(show_comments).css('display', 'block');
        
        $('#id_content').keyup(comment_preview);
    });


    // set body height for short pages
    // most of this thanks to andrew at http://brehaut.net
    var body_height;
    function check_content_size(){
        var browser_height = $(window).height();
        body_height = body_height || $('#content').height();
        if (browser_height > body_height) {
            $('#content').height(browser_height);
        }
    }
    $(document).ready(check_content_size);
    $(window).resize(check_content_size);

})(jQuery);
