"use strict";

define(['jquery'], function($) {
    $.fn.uploadify = function(url) {
        var $elem = $(this), $input = $elem.find('input');
        if ($elem.data('uploader'))
            return;

        var BOUNDARY = ' -- 598275094719306587185414';
        // Stolen from http://demos.hacks.mozilla.org/openweb/imageUploader/js/extends/xhr.js
        function buildMultipart(filedata) {
            var body = '--' + BOUNDARY;

            body += '\r\nContent-Disposition: form-data; name=\"file\"';
            body += '; filename=\"file\"\r\nContent-type: image/png';
            body += '\r\n\r\n' + filedata + '\r\n--' + BOUNDARY;

            return body;
        }

        function upload(e) {
            function uploadCompleteObjectURL() {
                var $img = $("<img>", { src: window.URL.createObjectURL(file) });
                $img.bind('load', function() {
                    window.URL.revokeObjectURL(this.src);
                    if ($img[0].width > $img[0].height)
                        $img.attr('width', '300');
                    else
                        $img.attr('height', '200');
                    $elem.children().first().replaceWith($img);
                });
            }

            function uploadCompleteFileReader(url) {
                function _img(url) {
                    var $img = $("<img>", { src: url });
                    $img.bind('load', function() {
                        $elem.children().first().replaceWith($img);
                    });
                }

                if (url) {
                    _img(url);
                } else {
                    var filereader = new FileReader();
                    filereader.onload = function(e) {
                        var url = e.target.result;
                        _img(url);
                    };
                }
            }

            var uploadComplete = ((window.URL.createObjectURL !== undefined) ? 
                                  uploadCompleteObjectURL :
                                  uploadCompleteFileReader);

            var dt, file;
            if (e.originalEvent.dataTransfer)
                dt = e.originalEvent.dataTransfer;
            else
                dt = this;

            file = dt.files[0];

            if (!file)
                return false;

            if (window.FormData) {
                var fd = new FormData();
                var headers = {};
                fd.append('file', file);

                var df = $.ajax(url, { type: 'POST',
                                       // Let the XMLHttpRequest figure out the mimetype from the FormData
                                       // http://dev.w3.org/2006/webapi/XMLHttpRequest-2/Overview.html#the-send-method
                                       contentType: false,
                                       processData: false,
                                       data: fd });
                df.done(function() {
                    uploadComplete();
                });
            } else {
                var filereader = new FileReader();
                filereader.onload = function(e) {
                    var url = e.target.result;
                    var df = $.ajax(url, { type: 'POST',
                                           contentType: 'multipart/form-data; boundary="' + BOUNDARY + '"',
                                           data: buildMultipart(url) });
                    df.done(function() {
                        uploadComplete(url);
                    });
                };
                filereader.readAsBinaryString(file);
            }

            return false;
        }

        var $drop = $('<span>', { 'class': 'drop' }).hide().appendTo($elem);

        var inClick = false;
        $elem.bind({
            click: function() {
                // Prevent the handler from running twice from bubbling.
                if (inClick)
                    return;

                inClick = true;
                $input[0].click();
                inClick = false;
                return false;
            },

            dragover: function() {
                // Have to prevent default action to define a drop target.
                return false;
            },

            dragenter: function(e) {
                if (e.relatedTarget == $elem[0])
                    $drop.fadeIn();

                return false;
            },

            dragleave: function(e) {
                if (e.relatedTarget == $elem[0])
                    $drop.fadeOut();

                return false;
            },

            drop: upload
        });

        $input.bind('change', upload);

        $elem.data('uploader', true);
    };
});

@@ -727,32 +727,25 @@
 ion 
-uploadCompleteObjectURL(
+replaceImage($img
 ) %7B%0A
@@ -769,232 +769,205 @@
 ar $
-img
+old
  = $
-(%22%3Cimg%3E%22, %7B src: window.URL.createObjectURL(file) %7D);%0A                $img.bind('load', function() %7B%0A                    window.URL.revokeObjectURL(this.src);%0A                    if ($img%5B0%5D.width %3E $img%5B0%5D
+elem.children().first();%0A                if ($old.width() %3C $old.height())%0A                    $img.width($old.width());%0A                else%0A                    $img.height($old
 .height
-)%0A  
+());%0A
     
@@ -982,38 +982,28 @@
     
-      $img.attr('width', '300'
+$img.replaceAll($old
 );%0A 
@@ -1009,32 +1009,35 @@
             
+%7D%0A%0A
         
 else%0A       
@@ -1028,121 +1028,268 @@
     
-else%0A                        $img.attr('height', '200');%0A                    $elem.children().first().
+    function uploadCompleteObjectURL() %7B%0A                var $img = $(%22%3Cimg%3E%22, %7B src: window.URL.createObjectURL(file) %7D);%0A                $img.bind('load', function() %7B%0A                    window.URL.revokeObjectURL(this.src);%0A                    
 replace
-With
+Image
 ($im
@@ -1553,44 +1553,20 @@
     
-$elem.children().first().
 replace
-With
+Image
 ($im

Fix up uploader to work for arbitrary images.