/**
 * Copyright 2017 Christoph M. Becker
 *
 * This file is part of Uploader_XH.
 *
 * Uploader_XH is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Uploader_XH is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Uploader_XH.  If not, see <http://www.gnu.org/licenses/>.
 */

jQuery(function ($) {
    function replaceWidget(element, html) {
        initWidget($(html).replaceAll($(element)).filter(".uploader_widget").get(0));
    }

    function initWidget(element) {
        $(".uploader_type, .uploader_subdir, .uploader_resize", element).change(function () {
            $.get($(this).data("url").replace("FIXME", encodeURIComponent(this.value)), {}, function (data) {
                replaceWidget(element, data);
            });
        });

        var config = $(element).data("config");
        config.container = $(".uploader_buttons", element).get(0);
        config.browse_button = $(".uploader_pickfiles", element).get(0);
        config.drop_element = element;
        config.init = ({
            PostInit: function (uploader) {
                $(".uploader_uploadfiles", element).click(function () {
                    uploader.start();
                    return false;
                });
            },
            FilesAdded: function (uploader, files) {
                $.each(files, function () {
                    var file = this;
                    $(".uploader_row_template", element).clone()
                        .removeClass("uploader_row_template").addClass("uploader_row")
                        .attr("id", this.id)
                        .find(".uploader_filename").text(this.name).end()
                        .find(".uploader_size").text(plupload.formatSize(this.size)).end()
                        .find(".uploader_progress").text("0%").end()
                        .find(".uploader_remove").click(function () {
                            uploader.removeFile(file);
                            $(this).parents(".uploader_row").remove();
                        }).end()
                        .appendTo($(".uploader_filelist", element));
                });
            },
            UploadProgress: function (up, file) {
                $("#" + file.id).find(".uploader_progress").text(file.percent + "%");
            },
            Error: function (up, err) {
                $(".uploader_console", element).text("Error #" + err.code + ": " + err.message + "\n");
            }
        });

        new plupload.Uploader(config).init();
    }

    $(".uploader_placeholder").each(function () {
        var placeholder = this;
        $.get(location.href, {uploader_serial: $(this).data("serial")}, function (data) {
            replaceWidget(placeholder, data);
        });
    });
});

@@ -937,24 +937,285 @@
 (element) %7B%0A
+        var uploadButton = $(%22.uploader_uploadfiles%22, element);%0A%0A        function updateUploadButton(uploader) %7B%0A            uploadButton.prop(%22disabled%22,%0A                    uploader.files.length %3C= uploader.total.uploaded + uploader.total.failed);%0A        %7D%0A%0A
         $(%22.
@@ -1797,43 +1797,85 @@
     
-$(%22.
 upload
-er_uploadfiles%22, element)
+Button%0A                    .prop(%22disabled%22, true)%0A                    
 .cli
@@ -1903,32 +1903,36 @@
                 
+    
 uploader.start()
@@ -1929,24 +1929,28 @@
 er.start();%0A
+    
             
@@ -1979,33 +1979,106 @@
                 
-%7D
+    %7D);%0A                uploader.bind(%22UploadComplete%22, updateUploadButton
 );%0A            %7D
@@ -2394,12 +2394,12 @@
    .
-attr
+prop
 (%22id
@@ -2836,24 +2836,82 @@
 ).remove();%0A
+                            updateUploadButton(uploader);%0A
             
@@ -3012,32 +3012,78 @@
             %7D);%0A
+                updateUploadButton(uploader);%0A
             %7D,%0A 
@@ -3121,16 +3121,22 @@
 tion (up
+loader
 , file) 
@@ -3219,24 +3219,70 @@
 ent + %22%25%22);%0A
+                updateUploadButton(uploader);%0A
             
@@ -3315,16 +3315,22 @@
 tion (up
+loader
 , err) %7B

Disable upload button if there is nothing to upload