﻿Ext.namespace('LUL');

LUL.Uploader = function(path, callback) {
    var uploader = new Ext.ux.SwfUploadPanel({
        border: false,
        debug: false,

        // Uploader Params				
        upload_url: '/Lib/FileBrowser/Upload.ashx', //relativ till flash_url
        post_params: { 'path': path },
        flash_url: "Lib/SwfUpload/swfupload.swf",

        // Custom Params
        single_file_select: false, // Set to true if you only want to select one file from the FileDialog.
        confirm_delete: false, // This will prompt for removing files from queue.
        remove_completed: false // Remove file from grid after uploaded.			
    });

    uploader.on('swfUploadLoaded', function() {
        uploader.addPostParam('path', path);
    });

    uploader.on('allUploadsComplete', function() {
        //        LVN.Msg.show('Uppladdningar klara', 'Alla uppladdningar är klara. Stäng fönstret om du inte vill ladda up fler filer.', win.body);
        //        me.fireEvent('upload');
        callback.call();
    }, this);

    // visa filval direkt
    //    uploader.on('render', function() { /*uploader.suo.selectFiles();*/ });

    var win = new Ext.Window({
        title: 'Ladda upp filer',
        width: 400,
        height: 300,
        modal: true,
        layout: 'fit',
        items: uploader
    });

    win.show();
}

LUL.initDirectUpload = function(editId, folderPath, desc, types, uploadComplete, scope) {  

    var suo;

    function startUpload() {
        suo.startUpload();
    }
  
    var id = Ext.id();
    var em = Ext.get(editId + 'Upload').child('em');
    em.setStyle({
        position: 'relative',
        display: 'block'
    });
    em.createChild({
        tag: 'div',
        id: id
    });
    suo = new SWFUpload({
        button_placeholder_id: id,
        button_width: 70,
        button_height: 18,
        button_cursor: SWFUpload.CURSOR.HAND,
        button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,

        flash_url: 'Lib/SwfUpload/swfupload.swf',
        upload_url: '/Lib/FileBrowser/Upload.ashx', //relativ till flash_url
        post_params: { 'path': folderPath, "ASPSESSID": sessionID },
        file_post_name: 'Filedata',
        file_size_limit: '1048576', //100MB
        file_queue_limit: '0',
        file_types: types,
        file_types_description: desc,
        file_upload_limit: '0',

        file_dialog_complete_handler: startUpload.createDelegate(this),
        upload_complete_handler: uploadComplete.createDelegate(scope)
    }, this);

    Ext.get(suo.movieName).setStyle({
        position: 'absolute',
        top: 0,
        left: 0
    });
    
    return suo;
}
