
/**
 * TUploadStatusDialog
 */
var TUploadStatusDialog = TClass.extend(TDialog,
{
    /**
     * Constructor
     */
    initialize: function(aUploaderName)
    {
        $C(TDialog).initialize.call(this);

        this.fFileList = new Object();

        this.setClassName("cssDialogInfo");
        this.setTitle(page.getI18N("FlashUploadDialogTitle"));
        this.setContentBox(500, 200);
        this.setDraggable(true);
        this.setResizable(true);
        this.setClassName("cssDialogInfo");
        this.fUploaderName = aUploaderName;
        this.fErrorFiles = new Object();
        this.isDone = false;
        this.closeHandler = null;

        var _self = this;
        this.btnID = this.addButton(page.getI18N("FlashUploadDialogBtnCancel"), function () {
              if (!_self.isDone) {
                  var movie = document.getElementById(_self.fUploaderName);
                  if (movie) {
                      movie.cancelUpload();
                      _self.close();
                  }
              }
              else {
                  _self.close();
              }

              if (_self.closeHandler) {
                  _self.closeHandler.call();
              }
        });
    },


    /**
     * Add file
     */
    addFile: function(aFileName)
    {
        this.fFileList[aFileName] = {"id":page.createUID(), "uploaded":0, "total":0, "status":"0%"};
        this.refresh();
        page.centerElement(document.getElementById(this.Name));
    },


    /**
     * Update file progress
     */
    updateFileProgress: function(aFileName, bytesUploaded, bytesTotal)
    {
        var percent = (bytesTotal!=0?Math.round(bytesUploaded*100/bytesTotal):0);

        this.fFileList[aFileName]["uploaded"] = bytesUploaded;
        this.fFileList[aFileName]["total"] = bytesTotal;
        this.fFileList[aFileName]["status"] = "<div class='progressbar' style='text-align:left;width:" + percent + "%;height:20px'>" + percent + "%</div>";

        this.refresh();
    },


    /**
     * Update file status
     */
    updateFileStatus: function(aFileName, status)
    {
        this.fFileList[aFileName]["status"] = "<div class='progressbar' style='text-align:left;width:100%;height:20px'>" + status + "</div>";

        this.refresh();
    },


    /**
     * Update file error
     */
    updateFileError: function(aFileName, error)
    {
        this.fFileList[aFileName]["status"] = "<div class='progressbar' style='text-align:left;width:100%;height:20px;background-color:#FF0000;'>" + error + "</div>";
        this.fErrorFiles[aFileName] = true;

        this.refresh();
    },


    /**
     * Refresh content
     */
    refresh: function()
    {
        var html = "";
        for (var fileName in this.fFileList) {
             var fileAttr = this.fFileList[fileName];
             html += fileName + (fileAttr["total"]==0 ? "" : " (" + fileAttr["total"] + ")") + "<br/>";
             html += "<div style='width:100%;text-align:left'>" + fileAttr["status"] + "</div><br/>";
             html += "<br/>";
        }

        if (html)
            html = html.substr(0, html.length - 5);

        this.setContent(html);
    },


    /**
     * Done
     */
    done: function()
    {
        this.isDone = true;
        document.getElementById(this.btnID).value = page.getI18N("BTN_OK");
    },


    /**
     * Get uploaded files
     */
    getUploadedFiles: function()
    {
        var tmp = new Array();
        for (var fileName in this.fFileList) {
             if (!this.fErrorFiles[fileName]) {
                 tmp.push(fileName);
             }
        }

        return tmp;
    }

});