﻿function initUploader(element, attachmenttype, objectid, temptoken, sessionid) {
    var id = element.id + "_uploader";
    var progress = "<div id='" + id + "'>Its loading</div>";
    $(element).after(progress).hide();
    loadUploader(id, attachmenttype, objectid, temptoken, sessionid);
}

var uploadScriptLoaded = false;

function loadUploader(id, attachmenttype, objectid, temptoken, sessionid) {
    if (!uploadScriptLoaded) {

        $.getScript("/Content/Components/swfupload/upload.js", function() {
            uploadScriptLoaded = true;
            startUploader(id, attachmenttype, objectid, temptoken, sessionid);
        });
    }
    else {
        startUploader(id, attachmenttype, objectid, temptoken, sessionid);
    }
}

function startUploader(id, attachmenttype, objectid, temptoken, sessionid) {

    var current_domain = location.protocol + "//" + location.hostname;

    $("#" + id).html('<ol id="log_' + id + '"><ol><input type="button" id="button_' + id + '" /><div id="attachments_' + id + '"></div>');
    $('#' + id).swfupload({
        file_size_limit: "10240",
        flash_url: current_domain + "/Content/Components/swfupload/swfupload.swf",
        upload_url: current_domain + "/Handlers/Upload.aspx",
        post_params: { "ASPSESSID": sessionid, "TemporaryToken": temptoken, "ObjectID": objectid, "AttachmentType": attachmenttype},
        file_size_limit: "20 MB",
        file_types: "*.jpg;*.gif;*.png;*.doc;*.docx;*.pdf;*.txt;*.rtf;*.xlsx;*.xls;*.ppt;*.pptx;*.odt;*.odp;*.odp;",
        file_types_description: "Images & Documents",
        file_upload_limit: 20,
        file_queue_limit: 0,
        button_text: '<span class="theFont">Browse files</span>',
        button_text_style: ".theFont { margin: 100px; width:400px; height:100px; font-family: Georgia; font-size: 12; color:#FF0000; cursor: pointer; }",
        button_text_left_padding: 1,
        button_text_top_padding: 1,
        button_cursor: SWFUpload.CURSOR.HAND,
        button_width: 80,
        button_height: 22,
        button_placeholder: $('#button_' + id)[0],
        debug: false
    })
    //            .bind('swfuploadLoaded', function(event) {
    //                $('#log').append('<li>Loaded</li>');
    //            })
		    .bind('fileQueued', function(event, file) {
		        //		        $('#log').append('<li>File queued - ' + file.name + '</li>');
		        // start the upload since it's queued
		        $(this).swfupload('startUpload');
		    })
		    .bind('fileQueueError', function(event, file, errorCode, message) {
		        $('#log_' + id).append('<li>File queue error - ' + message + '</li>');
		    })
    //		    .bind('fileDialogStart', function(event) {
    //		        $('#log').append('<li>File dialog start</li>');
    //		    })
    //		    .bind('fileDialogComplete', function(event, numFilesSelected, numFilesQueued) {
    //		        $('#log').append('<li>File dialog complete</li>');
    //		    })
    //		    .bind('uploadStart', function(event, file) {
    //		        $('#log').append('<li>Upload start - ' + file.name + '</li>');
    //		    })
    //		    .bind('uploadProgress', function(event, file, bytesLoaded) {
    //		        $('#log').append('<li>Upload progress - ' + bytesLoaded + '</li>');
    //		    })
    //		    .bind('uploadSuccess', function(event, file, serverData) {
    //		        $('#log').append('<li>Upload success - ' + file.name + '</li>');
    //		    })
		    .bind('uploadComplete', function(event, file) {
		        $('#log_' + id).append('<li>Upload complete - ' + file.name + '</li>');
		        // upload has completed, lets try the next one in the queue
		        $(this).swfupload('startUpload');

		        var swfu = $.swfupload.getInstance('#' + id);

		        if (swfu.getStats().files_queued === 0) {
		            $.ajax({
		                type: "GET",
		                url: "/Handlers/AsyncControl.ashx",
		                cache: false,
		                data: "ObjectID=" + objectid + "&TemporaryToken=" + temptoken + "&AttachmentType=" + attachmenttype,
		                success: function(msg) {
		                    $("#attachments_" + id).html(msg);
		                }
		            });
		        }
		    })
		    .bind('uploadError', function(event, file, errorCode, message) {
		        $('#log_' + id).append('<li>Upload error - ' + message + '</li>');
		    });
}