AJS.Popup.Image = {
    validate: function() {
        if (!AJS.$("#fileName").val()) {
            AJS.$("#popup-errorBox").show().find("#noImageFileSpecifiedError").show();
            return false;
        }
        return true;
    },

    submit: function() {
        var textAreaObject = window.opener.document.forms[AJS.params.formname].elements[AJS.params.fieldname];
        AJS.Popup.insertOrUpdateText("!" + this.getImageWikiText() + "!", textAreaObject);
    },

    getImageWikiText: function() {
        var text = AJS.$("#fileName").val();
        var alignment = AJS.$("#alignment").val();
        var isThumbnail = AJS.$("#thumbnail").attr("checked");

        if (isThumbnail)
            text += '|thumbnail';

        if (alignment != '')
            text += (isThumbnail ? ',' : '|') + 'align=' + alignment;

        return text;
    }
};
AJS.toInit(function($) {
    $("#thumbnail-grid td[title]").click(function () {
        $("#fileName").val($(this).attr("title"));
    });

    AJS.Popup.delegate = AJS.Popup.Image;
    AJS.Popup.init();
});