$(document).ready(function() {
    commentBusy = false;
    $('#comment-form').show();
    $('div#comment-form form').submit(function() {
        ajaxComment({'media': '/static/'});
        return false;
    });
});

$(document).ready(function() {
    $('a.gallery_elements').each(function() {
        var curr_title = $(this).parent().parent().find('.image-name').html();
        $(this).fancybox({
            'titlePosition': 'inside',
            'title': curr_title
        });
    });
});

$(document).ready(function() {
    $('#show-add-link-form').click(function() {
        $('#show-add-link-form').hide();
        $('#add-link-form').show();
        return false;
    });
    $('#close-add-link-form').click(function() {
        $('#add-link-form').hide();
        $('#show-add-link-form').show();
        return false;
    });
    $('#add-link-form').submit(function() {
        ajaxAddLink({'media': '/static/'});
        return false;
    });
});

$(document).ready(function() {
    $('#show-add-code-snippet-form').click(function() {
        $('#show-add-code-snippet-form').hide();
        $('#add-code-snippet-form').show();
        return false;
    });
    $('#close-add-code-snippet-form').click(function() {
        $('#add-code-snippet-form').hide();
        $('#show-add-code-snippet-form').show();
        return false;
    });
    $('#add-code-snippet-form').submit(function() {
        ajaxAddCodeSnippet({'media': '/static/'});
        return false;
    });
});

$(document).ready(function() {
    $('#select-library').change(function() {
        $(window.location).attr('href', $(this).val());
    });
});

$(document).ready(function() {
    $('a.delete-link').live('click', function() {
        var th = $(this);
        var url = $(this).attr('href');
        $.ajax({
            type: 'GET',
            url: url,
            success: function(data) {
                if (data.success === true) {
                    if (th.closest('ul.pattern-links').children('li.pattern-link').size() === 1) {
                        th.closest('ul.pattern-links').children('li.placeholder').show();
                    }
                    th.closest('li.pattern-link').remove();
                }
            },
            dataType: 'json'
        });
        return false;
    });
});

$(document).ready(function() {
    $('a.delete-image').live('click', function() {
        var th = $(this);
        var url = $(this).attr('href');
        $.ajax({
            type: 'GET',
            url: url,
            success: function(data) {
                if (data.success === true) {
                    if (th.closest('div#image-gallery').children('div.gallery-image-container').size() === 1) {
                        th.closest('div#image-gallery').children('div#no-images').show();
                    }
                    th.closest('div.gallery-image-container').remove();
                    $('#image-header-line').html(data.header_line);
                }
            },
            dataType: 'json'
        });
        return false;
    });
});

$(document).ready(function() {
    $('a.delete-comment').live('click', function() {
        var th = $(this);
        var url = $(this).attr('href');
        $.ajax({
            type: 'GET',
            url: url,
            success: function(data) {
                if (data.success === true) {
                    if (th.closest('ul.comments-list').children('li.comment-li').size() === 1) {
                        th.closest('ul.comments-list').children('li#no-comments-yet').show();
                    }
                    th.closest('li.comment-li').remove();
                }
            },
            dataType: 'json'
        });
        return false;
    });
});

