var noop_handler = function(e) {
    e.stopPropagation();
    e.preventDefault();
};

// get the content of a file and pass it to cb
function file_to_content(file, cb) {
    if (window.FileReader) {
	var reader = new FileReader();
	reader.onload = function(e) {
            // if (file.size > 10*1024*1024) return;
            cb(file.name, file.type, file.size, e.target.result);
	};
	reader.readAsDataURL(file);
    } else {
	console.log("Uploading "+file.name);
	var http = new XMLHttpRequest();
	var form = new FormData();
	form.append('file', file);
	http.open('POST', '/upload');
	http.send(form);
    }
};

##register hook_file_drop : string, (-> void), (string, string, int, string -> void) -> void
##args(sel, waiting_cb, cb)
{
    $(document).on("drop", function(e) {
        e.stopPropagation();
        e.preventDefault();
        if (!$(e.target).hasClass("dropzone") &&
	    !$(e.target).parent().hasClass("dropzone")) return;
        waiting_cb();
        var oe = e.originalEvent;
        $(oe.dataTransfer.files).each(
	    function(key, file) {
		file_to_content(file, cb);
	    }
        );
    }).on("dragenter, dragexit, dragover", noop_handler);
}

##register hook_file_chooser : string, (string, string, int, string -> void) -> void
##args(sel, cb)
{
    $(sel).on("change", function(e) {
        for (var f in e.target.files) {
            var file = e.target.files[f];
            file_to_content(file, cb);
        }
    });
}

@@ -1317,22 +1317,10 @@
     
-for (var f in 
+$(
 e.ta
@@ -1334,65 +1334,44 @@
 les)
- %7B%0A            var file = e.target.files%5Bf%5D;%0A            
+.each(%0A%09    function(key, file) %7B%0A%09%09
 file
@@ -1397,24 +1397,32 @@
 b);%0A        
-%7D
+    %7D%0A%09);
 %0A    %7D);%0A%7D%0A

fix for file drop