var UploadDialogController = function()
{
  var dialog = null;  
  var button = null;
  var file_list_tpl = new Ext.Template(
    "<div class='file-list-entry'>File {name} successfuly uploaded.</div>"
  );
  file_list_tpl.compile();

  function hideLoadingMask()
  {
    var loading = Ext.get('loading');
    var mask = Ext.get('loading-mask');
    mask.remove();
    loading.remove();
  }

  function getDialog()
  {
    if (!dialog) {
      dialog = new Ext.ux.UploadDialog.Dialog(null, {
        autoCreate: true,
        closable: true,
        collapsible: false,
        draggable: true,
        minWidth: 400,      
        minHeight: 200,
        width: 400,
        height: 350,
        proxyDrag: true,
        resizable: true,
        constraintoviewport: true,
        title: 'File upload queue.',
        url: 'upload-dialog-request.php',
        reset_on_hide: false,
        allow_close_on_upload: true,
        upload_autostart: true
      });
      
      dialog.on('uploadsuccess', onUploadSuccess);
    }
    return dialog;
  }
  
  function showDialog(button)
  {
    getDialog().show(button.getEl());
  }
  
  function onUploadSuccess(dialog, filename, resp_data)
  {
    var parts = filename.split(/\/|\\/);
    if (parts.length == 1) {
      filename = parts[0];
    }
    else {
      filename = parts.pop();
    }
    file_list_tpl.append('file-list', {name: filename});
  }

  return {
    init : function()
    {
      Ext.QuickTips.init();
      button = new Ext.Button('show-dialog-btn', {
        id: 'show-button',
        text: 'Show dialog',
        handler: showDialog
      });
      hideLoadingMask();
      Ext.get(document.body).setStyle('overflow', 'auto');
    }
  }
}();

Ext.BLANK_IMAGE_URL = 'ExtJS/resources/images/s.gif';
Ext.onReady(UploadDialogController.init);