// uploadBar class
function uploadBar() {
	this.container = null;
	this.base_container = null;
	this.progress_container = null;
	this.comment_container = null;

	this.units    = 0;
	this.aktUnit  = 0;
	this.maxWidth = 0;

	this.initialize = function(iContainer, iUnits) {
		if (iContainer == null) {
			// reset
			this.units = iUnits;
			this.aktUnit = 0;
			this.progress_container.style.width = "0px";
			this.comment_container.innerHTML = "";
			
		} else {
			// inicializace
			this.container = iContainer;
			this.units = iUnits;

			this.base_container = document.createElement("DIV");
			this.base_container.style.width = "237px";
			this.base_container.style.padding = "4px 0px 0px 6px";
			this.base_container.style.textAlign = "left";
			this.base_container.style.marginLeft = "auto";
			this.base_container.style.marginRight = "auto";
			this.base_container.style.background = "url('/images/progress_bg.gif')";
			this.base_container.style.backgroundRepeat = "no-repeat";

			this.progress_container = document.createElement("DIV");
			this.progress_container.style.width = "0px";
			this.progress_container.style.height = "15px";
			this.progress_container.style.background = "url('/images/progress.gif')";

			this.comment_container = document.createElement("DIV");
			this.comment_container.style.width = "237px";
			this.comment_container.style.textAlign = "center";
			this.comment_container.style.fontSize = "11px";
			this.comment_container.style.paddingTop = "5px";

			this.container.appendChild(this.base_container);
			this.base_container.appendChild(this.progress_container);
			this.base_container.appendChild(this.comment_container);
	
			this.maxWidth = 224;//parseInt(this.base_container.style.width);
		}
	}
	
	this.destroy = function() {
		delete this.base_container;
		delete this.progress_container;
		delete this.comment_container;
	}

	this.processStart = 0;
	this.processDiff  = 0;
	this.processStep  = 0;
	this.beforeUnit   = 0
	this.unitDiff     = 0;
	this.uploaded	  = "";
	this.finished	  = false;

	this.setProgress = function(iNumUnits, iUploaded) {
		if (this.aktUnit < this.units) {
			if (iNumUnits > this.units) iNumUnits = this.units;
			this.processStart = Math.ceil((this.maxWidth / this.units) * this.aktUnit);

            this.beforeUnit = this.aktUnit;
            this.unitDiff = iNumUnits - this.aktUnit;
			this.aktUnit = iNumUnits;

			this.processDiff  = Math.ceil((this.maxWidth / this.units) * this.aktUnit) - this.processStart;
			this.processStep  = 0;

			this.progress_container.style.width = this.processStart + "px";

			this.setUploaded(iUploaded);
		}
	}

	this.setUploaded = function(iUploaded) {
		this.uploaded = iUploaded;
		this.comment_container.innerHTML = Math.round((this.beforeUnit / this.units) * 100) + " % (" + this.uploaded + ")";
	}
	
	this.finish = function() {
		this.setProgress(this.units, "upload dokončen");
	}

	this.doProcess = function(iMidTime, iStepTime) {
		var newWidth = 0;

		if (this.processDiff > 0) {
			var midSteps = Math.ceil(iStepTime / iMidTime);
			if (midSteps > 1) {
				if (this.processStep < midSteps) {
					this.processStep++;

					newWidth = this.processStart + Math.ceil(this.processDiff * (this.processStep / midSteps));
					this.progress_container.style.width = newWidth + "px";

            		this.comment_container.innerHTML = Math.round(((this.beforeUnit + this.unitDiff * (this.processStep / midSteps)) / this.units) * 100) + " % (" + this.uploaded + ")";
				}
			} else this.progress_container.style.width = this.processStart + this.processDiff;
		}

		return (parseInt(this.progress_container.style.width) < this.maxWidth);
	}
}
