jQuery(function($) {
    var contextPath = $("#confluence-context-path").attr("content");
    var openActiveArrowIconPath = contextPath +  "/images/icons/arrow_opening_active_16.gif";
    var closeActionArrowIconPath = contextPath +  "/images/icons/arrow_closing_active_16.gif";
    
    $("tr.currentAttachmentRow").each(function() {
        $(this).find("img.toggleAttachmentHistoryImage").each(function() {
            $(this).click(function() {
                var arrowIcon = $(this);

                arrowIcon.parent().parent().nextAll().each(function() {
                    var historyAttachmentRow = $(this);

                    if (historyAttachmentRow.hasClass("historicalAttachmentRow")) {
                        if (historyAttachmentRow.is(":hidden")) {
                            historyAttachmentRow.show();
                        } else {
                            historyAttachmentRow.hide();
                        }
                    } else {
                        return false; /* Stop iterating */
                    }
                });

                arrowIcon.attr("src", arrowIcon.attr("src") != openActiveArrowIconPath ? openActiveArrowIconPath : closeActionArrowIconPath);
            });
        });
    });

    $("a.deleteAttachmentLink").each(function() {
        var params = {};
        var deleteLink = $(this);

        deleteLink.parent().find("fieldset").find("input").each(function() {
            params[this.name] = this.value;
        });

        deleteLink.click(function() {
            return confirm(params["i18n-deleteConfirmMessage"]);
        });
    });
});