var ClassUploadCallback = Class.create();
ClassUploadCallback.prototype = Object.extend({

  initialize:function() {

    $("statusImageFinished").hide();
    Element.setStyle("progressbar", { width: 0 });
    FOA.overlayContext.uploadStep(2);

  },

  fileQueueError:function(fileObj, error_code, message) {

    try {

      var error_name = "";

      switch(error_code) {
        case SWFUpload.ERROR_CODE_QUEUE_LIMIT_EXCEEDED:
          console.log("You have attempted to queue too many files.");
        break;
      }

      if (error_name !== "") {
        console.log(error_name);
        return;
      }

      switch(error_code) {
        case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
          console.log('zero byte file');
        break;
        case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
          console.log('too big');
        break;
        case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
        case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
        default:
          console.log(message);
        break;
      }
    }
    catch (ex) { this.debug(ex); }

  },

  fileDialogComplete:function(num_files_queued) {

    try {
      if (num_files_queued > 0) {

        // size des gesamten uploadstreams
        $("total").firstChild.nodeValue = format_bytes(this.fileQueueSize);

        // starte uploadtimer
        this.uploadTimer = new ClassUploadTimer();

        // für cancel upload
        $A($$('a.uploadCancel')).each(function(oneLink) {
          Event.observe(oneLink, 'click', function(e) {
                                            Event.stop(e);
                                            FOA.overlayContext.uploadStep(1);
                                            this.cancelUpload();
                                          }.bindAsEventListener(this));
        }.bind(this));

        $("statusImageFinished").show();
        this.startUpload();
      }
      else {
        FOA.overlayContext.uploadStep(1);
      }
    }
    catch (ex) { this.debug(ex); }

  },

  uploadProgress:function(fileObj, bytesLoaded) {

    try {
      // actual filname
      $("statusImage").firstChild.nodeValue  = fileObj.name;

      // size from loaded files + loaded bytes from current loading file
      var fileQueueLoadedBytes = this.fileQueueLoadedSize + bytesLoaded;

      // loaded Bytes in %
      var percent = Math.ceil(((fileQueueLoadedBytes) / this.fileQueueSize) * 100);

      // loader-bar
      Element.setStyle("progressbar", { width: (percent / 100 * 330) + 'px' });
      $("current").firstChild.nodeValue = format_bytes(fileQueueLoadedBytes);

      if (this.uploadTimer.timer.getUTCSeconds() > 0) {

        // speed
        var uploadSpeed = Math.round(fileQueueLoadedBytes / this.uploadTimer.timer.getUTCSeconds(), 2);
        $("speed").firstChild.nodeValue  = format_bytes(uploadSpeed);

        // remaining time
        var remainingBytes = (this.fileQueueSize - fileQueueLoadedBytes);
        var remainingTime  = remainingBytes / uploadSpeed;
        var remainingDate  = new Date();

        remainingDate.setTime(remainingTime * 1000);
        this.uploadTimer.setTime($("remain"), remainingDate);
      }

      // formblitz
      if (percent == 100) {
        ImageTools.checkUploadFinished(function(transport) {
          if (transport) {
            XMLDoc = transport.responseXML;
          }
          FOA.overlayContext.uploadStep(3);
        });
      }
    }
    catch (ex) { this.debug(ex); }

  },

  uploadSuccess:function(fileObj, server_data) {

    try {
      //console.log("uploadSuccess");
    }
    catch (ex) { this.debug(ex); }
  },

  uploadComplete:function(fileObj) {

    try {
      /*  I want the next upload to continue automatically so I'll call startUpload here */
      if (this.getStats().files_queued > 0) {
        this.startUpload();
      } else {
        clearTimeout(this.uploadTimer.clock);
        FOA.overlayContext.uploadStep(3);
      }
    }
    catch (ex) { this.debug(ex); }

  },

  uploadError:function(fileObj, error_code, message) {

    try {
      switch(error_code) {
        case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
          try {
            console.log("error, upload abgebrochen");
          }
          catch (ex) { this.debug(ex); }
        break;
        case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
          console.log("upload limit");
        break;
        default:
          console.log(message);
        break;
      }

    } catch (ex) { this.debug(ex); }

  }
});

var ClassUploadTimer = Class.create();
ClassUploadTimer.prototype = Object.extend({

  initialize:function() {

    this.timer = new Date();
    this.clock;

    // Timer initialisieren
    var timer = new Date();
    this.uploadStartTime = timer.getTime();
    this.updateTime();

  },

  updateTime:function() {

    var timer = new Date();
    this.timer.setTime(timer.getTime() - this.uploadStartTime);

    try {
      this.setTime($("time"), this.timer);
      this.clock = setTimeout(function () { this.updateTime(); }.bind(this), 1000);
    }
    catch (ex) { this.debug(ex); }

  },

  setTime:function(el, timer) {

    hours   = timer.getUTCHours();
    minutes = timer.getUTCMinutes();
    seconds = timer.getUTCSeconds();

    if (hours   < 10) hours   = "0" + hours;
    if (minutes < 10) minutes = "0" + minutes;
    if (seconds < 10) seconds = "0" + seconds;

    el.firstChild.nodeValue = hours + ":" + minutes + ":" + seconds;

  }

});
