$(function() {
  //return; // disable flash upload for now ...
  if (!$("#progress-upload").get(0)) return;

  //var startTime = null;

  function dialogComplete(numFilesSelected, numFilesQueued) {
    if (numFilesSelected == 1) {
      showProgress(null, 0, 100);
      this.startUpload();
    } else if (numFilesSelected > 1) {
      alert("Es können nicht mehrere Dateien gleichzeitig hochgeladen werden.")
    }
  }

  function initializeFlashUploadUI() {
    $("#default-upload-wrapper").hide();
    $("#progress-upload-wrapper .hide-first").css({ display:"block" });
    $("#progress-upload-classic").show().click(function() {
      $("#default-upload-wrapper").show();
      $("#progress-upload-wrapper").css({ display:"none" });
      return false;
    });
  }

  function showProgress(f, current, total) {
    //if (current == 0) startTime = (new Date()).getTime();
    //var diff = (new Date()).getTime() - startTime;

    var text = "Hochladen: " + Math.round(current * 100 / total) + "%";
    $("#progress-upload-progress").css({ display:"block" }).html(
          "<span class='bar' style='width:" + current * 200 / total +"px;'></span>" +
          "<span class='text'>" + text + "</span>");
  }

  var swfu = new SWFUpload({
      // Backend Settings
      upload_url: progressUploadSettings.uploadUrl,	// Relative to the SWF file or absolute
      //post_params: progressUploadSettings.postParams,

      // File Upload Settings
      file_size_limit : "50 MB",
      file_types : "*.*",
      file_types_description : "STL-Dateien",
      file_upload_limit : "0",
      file_queue_limit: "1",

      // Event Handler Settings - these functions as defined in Handlers.js
      //  The handlers are not part of SWFUpload but are part of my website and control how
      //  my website reacts to the SWFUpload events.
      swfupload_loaded_handler: initializeFlashUploadUI,
      file_queue_error_handler: function() { this.setButtonDisabled(false); alert(arguments); },
      file_dialog_complete_handler: dialogComplete,
      upload_start_handler: function() { this.setButtonDisabled(true); },
      upload_progress_handler: function(f, current, total) { showProgress(f, current * 95 / 100, total) }, // show max. 95%
      upload_error_handler: function(f, st, msg) { this.setButtonDisabled(false); alert(msg); },
      upload_success_handler: function(f, data) { this.setButtonDisabled(false); $("#progress-upload-progress").hide(); eval(data); },
      upload_complete_handler: function() { showProgress(null, 100, 100); },

      // Button Settings
      button_image_url : "/images/button/button240.png",	// Relative to the SWF file
      button_placeholder_id : "progress-upload",
      button_width: 240,
      button_height: 25,
      button_text : '<span class="button">STL-Datei hochladen <span class="buttonSmall">(max. 50 MB)</span></span>',
      button_text_style : '.button { font-family: Helvetica, Arial, sans-serif; font-size: 12pt; } .buttonSmall { font-size: 10pt; }',
      button_text_top_padding: 3,
      button_text_left_padding: 18,
      button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
      button_cursor: SWFUpload.CURSOR.HAND,

      // Flash Settings
      flash_url : "/javascripts/swfupload/swfupload.swf",

      custom_settings : {
        upload_target : "progress-upload"
      },

      // Debug Settings
      debug: false
    });
})