﻿/// jQuery plugin to add support for SwfUpload
/// (c) 2008 Steven Sanderson

 function renderUploader(options, idElement, chooseFileText, chooseTextButton, choosenFileText, cancelUploadText) {
     // Put in place a new container with a unique ID
     var id = idElement;
        // $("#uploadprogressbar").progressBar(persents);
        //$("#uploadprogressbar").progressBar();
        //$("#uploadprogressbar").progressBar({ barImage: '/Themes/Default/Images/ProgressBar/progressbg_orange.gif' });
        //$("#uploadprogressbar").fadeIn();<span id='uploadprogressbar'></span>        
        var container = "<div id='asyncUploader'>";
        container += "<div id='progressInfo' style='float:left;'>" + chooseFileText + "</div>";   
        container += "<div id='uploadButton' style='float:left; margin-left:5px;'><span id='" + id + "_swf'/></div>";
        container += "<input type='hidden' name='" + "uploadfile" + "_filename'/>";
        container += "<input type='hidden' name='" + "uploadfile" + "_guid'/>";
        container += "</div>";
        $("#" + idElement).html(container);
        
        // Instantiate the uploader SWF
        var swfu;
        var width = 109, height = 22;
        if (options) {
            width = options.width || width;
            height = options.height || height;
        }
        var defaults = {
            flash_url: "/Scripts/swfupload.swf",
            upload_url: "/Video/AsyncUpload",
            file_size_limit: "400 MB",
            file_types: "*.*",
            file_types_description: "All Files",
            debug: false,

            //button_image_url: "/Themes/Default/Images/blankButton.png",
            button_width: width,
            button_height: height,
            button_placeholder_id: id + "_swf",
            button_text: "<font face='Arial' size='13pt'>" + chooseTextButton + "</font>",
            button_text_left_padding: 15,
            button_text_top_padding: 1,

            // Called when the user chooses a new file from the file browser prompt (begins the upload)
            file_queued_handler: function(file) {
                $("#progressInfo").html(choosenFileText + file.name + ".");
                $("#startUploadLocalVideoButtton").show();
            },

            // Called when a file doesn't even begin to upload, because of some error
            file_queue_error_handler: function(file, code, msg) {
                alert("Ошибка при попытке закачать файл: " + msg);
            },

            // Called when an error occurs during upload
            upload_error_handler: function(file, code, msg) { alert("Ошибка при попытке закачать файл: " + msg); },

            // Called when upload is beginning (switches controls to uploading state)
            upload_start_handler: function() {
                swfu.setButtonDimensions(0, 0);
                $("input[name$=_filename]").val("");
                $("input[name$=_guid]").val("");
                // Готовим прогресс бар
                $("#progressInfo").html("");
                //$("#uploadButton").hide();
                var progressInfoHtml = "<span id='uploadprogressbar'></span>";
                $("#progressInfo").html(progressInfoHtml);
                $("#uploadprogressbar").progressBar({ barImage: '/Themes/Default/Images/ProgressBar/progressbg_orange.gif' });
                $("#uploadprogressbar").fadeIn();
            },

            // Called when upload completed successfully (puts success details into hidden fields)
            upload_success_handler: function(file, response) {
                $("input[name$=_filename]").val(file.name);
                $("input[name$=_guid]").val(response);
                $("#stopUploadLocalVideoButton").hide();
                $("#deleteUploadedLocalVideoButton").show();
                SetFileUploaded(1);
            },

            // Called when upload is finished (either success or failure - reverts controls to non-uploading state)
            upload_complete_handler: function() {
                if ($("input[name$=_filename]").val() != "") { // Success
                    $("#uploadprogressbar").progressBar(100);
                } else 
                    ShowLocalFilePanel(true);
            },

            // Called periodically during upload (moves the progess bar along)
            upload_progress_handler: function(file, bytes, total) {
                var percent = 100 * bytes / total;
                $("#uploadprogressbar").progressBar(parseInt(percent));
            }
        };

        swfu = new SWFUpload($.extend(defaults, options || {}));

        // Give the effect of preserving state, if requested
        if (options.existingFilename || "" != "") {
            $("input[name$=_filename]").val(options.existingFilename);
        }
        if (options.existingGuid || "" != "")
            $("input[name$=_guid]").val(options.existingGuid);
        return swfu;
    };