﻿/// jQuery plugin to add support for SwfUpload
/// (c) 2008 Steven Sanderson

(function ($) {

    $.fn.makeAsyncUploader = function (options) {
        return this.each(function () {

            var id = $(this).attr("id");
            var container = $("<span class='swfupload hide-on-overlay'/>");
            container.append($("<span id='" + id + "_swf'/>"));
            container.append($("<input type='hidden' name='" + id + "_filename'/>"));
            container.append($("<input type='hidden' name='" + id + "_guid'/>"));
           
            var loading = $(this).parents(".swfupload-container").find(".swfupload-loading");
            loading.append($("<span id='" + id + "_uploading'>Laddar upp...</span>"));
            loading.append($("<div class='progress-bar'> <div>&nbsp;</div> </div>"));

            $(this).before(container).remove();
           
            loading.hide();

            var swfu;
            var width = 80, height = 18;
            var defaults = {
                flash_url: "/swf/swfupload.swf",
                upload_url: "",
                file_size_limit: "5 MB",
                file_types: "*.*",
                file_types_description: "Alla filer",
                debug: false,

                button_image_url: "/images/swfupload.png",
                button_width: width,
                button_height: height,
                button_placeholder_id: id + "_swf",
                button_text: "",
                button_text_left_padding: 5,
                button_text_top_padding: 1,

                file_queued_handler: function (file) { swfu.startUpload(); },

                file_queue_error_handler: function (file, code, msg) {
                    alert("Sorry, your file wasn't uploaded: " + msg);
                },

                upload_error_handler: function (file, code, msg) {
                    alert("Sorry, your file wasn't uploaded: " + msg);
                },

                upload_start_handler: function () {
                    swfu.setButtonDimensions(0, height);
                    $("input[name$=_filename]", container).val("");
                    $("input[name$=_guid]", container).val("");
                    $("div.progress-bar div", loading).css("width", "0px");
                    loading.show();
                },

                upload_success_handler: function (file, response) {
                    var oResp = eval("(" + response + ")");
                    if (oResp.status == "ok") {
                        $("input[name$=_filename]", container).val(file.name);
                        $("input[name$=_guid]", container).val(oResp.result);
                        options.upload_complete();
                    }
                    else {
                        alert(oResp.result);
                    }
                },

                upload_complete_handler: function () {
                    var clearup = function () {
                        loading.hide();
                        swfu.setButtonDimensions(width, height);
                    };
                    if ($("input[name$=_filename]", container).val() != "") {
                        $("div.progress-bar div", loading).animate({ width: "100%" }, { duration: "fast", queue: false, complete: clearup });
                    }
                    else {
                        clearup();
                    }
                },

                upload_progress_handler: function (file, bytes, total) {
                    var percent = 100 * bytes / total;
                    $("div.progress-bar div", loading).animate({ width: percent + "%" }, { duration: 500, queue: false });
                }
            };

            swfu = new SWFUpload($.extend(defaults, options || {}));

            /*
            $("span[id$=_uploading] A", container).click(function () {
                swfu.cancelUpload(null, false);
            });
            */

        });
    }

})(jQuery);