var hidethumbs = function(work_id){
	$.get('/comments/thumbed/' + work_id);
}

$(function(){
    var loading_html = $('<span><img style="float:left; margin-top:5px;" '
                + 'class="loader" src="/static/images/loading.gif" /></span>')

    //delete comment
    $('.comment-delete')
    .css('display', 'block')
    .click(function(e){
        e.preventDefault();
        var do_delete = confirm('Are you sure to delete the comment?')
        if(do_delete){
            var comment_id = $(this).attr('href');
            var loading = $(loading_html);
            var text_container = $('#text_' + comment_id);
            text_container.before(loading)
            $.post('/comment/delete/', { comment_id: comment_id }, function(data){
                if(!data.error){
                    $('#Comment_' + comment_id).html(data.message);
                }else{
                    text_container.before('<span class="error" style="margin-left:1em;">' + (data.error) +'</span>')
                }
            })
        }else{
            //do nothing
        }
    });

    //edit comment
    $('.comment-edit')
    .css('display', 'block')
    .click(function(e){
        e.preventDefault();
        var comment_id = $(this).attr('href');
        $('.error').remove()
        var text_container = $('#text_' + comment_id);
        var text = $.trim(text_container.text());
        var form = $('<form method="post" action="/comment/edit/'+ comment_id + '/" >'
            + '<textarea name="text" rows="10" cols="50" id="edit-text-' + comment_id +'">'
            + text
            +'</textarea>'
            + '<p><input type="submit" class="submit edit-submit" id="edit-submit-' + comment_id
            + '" value="Submit comment" /></p>'
            +'</from>'
        );
        text_container.html(form);
        $('#edit-submit-' + comment_id).click(function(e){
            e.preventDefault();
            $(this).attr('disabled', 'disabled').unbind('click');
            var loading = $(loading_html);
            $(this).after(loading);
            //console.log(form.serialize())
            //console.log($('#edit-text-' + comment_id).val())
            var new_text = $('#edit-text-' + comment_id).val();
            $.post('/comment/edit/', { comment_id: comment_id, text: new_text }, function(data){
                if(!data.error){
                    text_container.html(data.message);
                }else{
                    text_container.html(text);
                    text_container.before('<span class="error" style="margin-left:1em;">' + (data.error) +'</span>')
                }
            })
        })
    });
    
})

