/**
 * ProgressBar scripts
 *
 * @copyright (c) 2004-2007, SWsoft
 */
function SbProgressBar() {
	this._control = self.document.getElementById('sbProgressBar');
	this._statusControl = self.document.getElementById('sbProgressBarStatus');
	this._imageControl = self.document.getElementById('sbProgressBarImage');
	this._onShowEvents = new Array();
	this._onHideEvents = new Array();
	this._expectedTime = 5;
	this._timeStep = (document.all) ? 50 : 10;
	this._startTime = null;
	this._endTime = 0;
	this._shortPublicationExpectedTime = 10000;
	this._fullPublicationExpectedTime = 30000;
	this._expectedTime = this._fullPublicationExpectedTime;
	this._isEnding = false;
}

SbProgressBar.prototype.addOnShowEvent = function(event) {
	if ('function' == typeof(event)) {
		this._onShowEvents[this._onShowEvents.length] = event;
	}
}

SbProgressBar.prototype.addOnHideEvent = function(event) {
	if ('function' == typeof(event)) {
		this._onHideEvents[this._onHideEvents.length] = event;
	}
}

SbProgressBar.prototype.setShortPublicationExpectedTime = function() {
	this._expectedTime = this._shortPublicationExpectedTime;
}

SbProgressBar.prototype.setFullPublicationExpectedTime = function() {
	this._expectedTime = this._fullPublicationExpectedTime;
}

SbProgressBar.prototype._show = function() {
	sbPreloader.hide();
	sbPreloader.disable();
	sbPreloader.showBackground();
	this._control.style.display = 'block';
	centerObject(this._control);
	for(var i = 0; i < this._onShowEvents.length; i++) {
		var event = this._onShowEvents[i];
		event();
	}
}

SbProgressBar.prototype._hide = function() {
	for(var i = 0; i < this._onHideEvents.length; i++) {
		var event = this._onHideEvents[i];
		event();
	}
	sbPreloader.enable();
	sbPreloader.hideBackground();
	this._control.style.display = 'none';
}

/**
 * Ends progress bar process
 *
 * @param none
 */
SbProgressBar.prototype.endProcess = function() {
	this._isEnding = true;
	var now = new Date();
	this._endTime = now.getTime() - this._startTime;
}

/**
 * Starts progress bar process
 *
 * @param int number of seconds
 */
SbProgressBar.prototype.process = function() {
	this._isEnding = false;
	this._endTime = 0;
	var now = new Date();
	this._startTime = now.getTime();
	this._setStatus(0);
	this._show();
	this._process();
}

SbProgressBar.prototype._process = function() {
	var now = new Date();
	var currentTime = now.getTime() - this._startTime;
	var status = (this._isEnding) ? this._calculateExpressFormula(currentTime) : this._calculateMainFormula(currentTime);
	this._setStatus(status);

	var progressBarObject = this;
	if (status >= 1) {
		self.setTimeout(function () { progressBarObject._hide(); }, this._timeStep);
	} else {
		self.setTimeout(function () { progressBarObject._process(); }, this._timeStep);
	}
}

SbProgressBar.prototype._calculateExpressFormula = function(time) {
    var result = this._calculateMainFormula(time) + Math.pow((time - this._endTime) / 1000, 2);
    if (result > 1) {
        result = 1;
	}
	return result;
}

SbProgressBar.prototype._calculateMainFormula = function(time) {
	var result;
	var currentScope = this._expectedTime / 9;
	if (time <= currentScope) {
		result = this._calculateStartMainFormula(time);
	} else {
		result = this._calculateBasicMainFormula(time);
	}
	return result;
}

SbProgressBar.prototype._calculateStartMainFormula = function(time) {
	return (9 * time) / (10 * this._expectedTime);
}

SbProgressBar.prototype._calculateBasicMainFormula = function(time) {
	var tmp = 6 * Math.sqrt(this._expectedTime * time) / (65 * time - 5 * this._expectedTime);
	return 1 - tmp;
}

/**
 * Sets current progress bar status
 *
 * @param int number of percents completed
 */
SbProgressBar.prototype._setStatus = function(status) {
	var percent = 100 * status;
	if (percent > 100) {
		percent = 100;
	}
	this._statusControl.innerHTML = htmlSpecialChars(Math.ceil(percent));
	this._imageControl.style.width = percent + '%';
}