window.fd_progresses = [];
var tmrProgressMove = 0;
function fd_progresses_move() {
	if (tmrProgressMove > 0) window.clearTimeout(tmrProgressMove);
	tmrProgressMove = window.setTimeout('fd_progresses_doMove()', 100);
}
function fd_progresses_doMove() {
	if (tmrProgressMove > 0) {
		window.clearTimeout(tmrProgressMove);
	}
	for (var i = 0; i < fd_progresses.length; i++) {
		if (typeof fd_progresses[i] == 'object' && fd_progresses[i].initialized) {
			fd_progresses[i].move();
		}
	}
}

function FDProgress(cpTitleStr) {

	this.index = fd_progresses.length;
	fd_progresses.push(this);

	this.initialized = false;
	this.position = 0;
	this.loaded = false;
	this.width = 0;
	this.total = 0;
	this.div = null;
	this.window = null;
	this.title = null;
	this.bar = null;
	this.visible = false;
	this.progress = 0;
	this.dir = 0;
	this.funcInit = null;
	this.funcFinish = null;
	this.titlestr = cpTitleStr;
	this.subtitle = '';
	this.subtitled = false;
}

FDProgress.prototype.init = function(npProgressTotal, cpFuncInit, cpFuncFinish, cpTitle, cpSubTitle, lpStyle, npProgress) {

	this.DT = new Date();

	with (this) {
		initialized = true; progress = npProgress || 0;
		this.getRefs();
		if (arguments.length > 0) {
			total = npProgressTotal; funcInit = cpFuncInit; funcFinish = cpFuncFinish; titlestr = (typeof cpTitle == 'string' ? cpTitle : titlestr); subtitle = '';
			if (lpStyle) setStyle();
		}
		if (width == 0) width = Number.float($('tblProgress').width);
		bar.style.position = 'relative';
		visible = false; bar.width = '1%'; dir = 0;
		setDisplay(null, bar, false);
	}
	this.writeTitle(cpSubTitle, true);
	layerWrite(document, 'tdProgressWait', FD.Texts.Progress.get('WAIT'));
	setDisplay(document, 'tblProgressPos', true);
	if (this.position > 0) {
		var oEl = $('tblProgressPos');
		oEl.style.height = '0px'; oEl.style.width = '0px';
	}
	setDisplay(null, this.div, true);
	setDisplay(null, this.bar, false);
	setDisplay(document, 'tblProgress', true);
	this.move();
	addEventHandler(window, 'onscroll', fd_progresses_move);
	addEventHandler($('form'), 'onscroll', fd_progresses_move);
	addEventHandler(window, 'onresize', fd_progresses_move);
	if (this.funcInit) eval(this.funcInit);
}
FDProgress.prototype.getRefs = function() {
	this.div = $('divProgressWindow');
	this.window = $('tblProgressWindow');
	this.bar = $('tblProgressBar');
	this.title = $('tdProgressTitle');
}
FDProgress.prototype.set = function(cpSubTitle, npSet) {
	if (!this.initialized) return;
	this.progress += (npSet || 1);
	var nPerc = Math.max(Math.round((this.progress / this.total) * 100), 1);
	if (isNaN(nPerc) || !isFinite(nPerc)) nPerc = 1;
	this.bar.width = nPerc + '%';
	this.writeTitle(cpSubTitle);
	//this.move();
	if (nPerc >= 1 && !this.visible) {
		setDisplay(null, this.bar, true);
		this.visible = true;
	}
}
FDProgress.prototype.loop = function(npTM) {
	if (!this.initialized) return;
	npTM = npTM || 10;
	if (this.dir == 0) this.progress++;
	else this.progress--;
	var nPerc = Math.max(Math.round((this.progress / this.total) * 100), 1), nLeft;
	if (this.dir == 0) this.bar.width = nPerc + '%';
	if (this.dir == 1) {
		nLeft = (this.bar.parentNode.offsetWidth * ((100 - nPerc) / 100));
		this.bar.style.pixelLeft = nLeft;
		this.bar.width = Math.max(this.bar.parentNode.offsetWidth - nLeft - 1, 1);
	}
	if (nPerc >= 1 && !this.visible) {
		setDisplay(null, this.bar, true);
		this.visible = true;
	}
	if (this.dir == 0 && nPerc >= 100) this.dir = 1;
	if (this.dir == 1 && nPerc <= 1) { this.dir = 0; this.bar.style.pixelLeft = 0; }
	window.setTimeout("oProgress.loop(" + npTM + ")", npTM);
}
FDProgress.prototype.finish = function(npMS, lpDisp) {
	lLoading = false;
	//if(this.position > 0) return false;
	if (this.initialized) {

		if (tmrProgressMove > 0) window.clearTimeout(tmrProgressMove);
		removeEventHandler(window, 'onscroll', fd_progresses_move);
		removeEventHandler(getElement(document, 'form'), 'onscroll', fd_progresses_move);
		removeEventHandler(window, 'onresize', fd_progresses_move);

		//window.setTimeout( "setDisplay( document, 'divProgressWindow', false );" + ( lpDisp ? "setDisplay( document, 'grid1', true );oProgress.setStyle();" : "" ), ( arguments.length > 0 ? npMS : 0 ) );
		if (this.position == 0) setDisplay(document, 'tblProgressPos', false);

		//var d1 = ( new Date() - this.DT ) / 1000 ;
		//alert(d1) ;

		setDisplay(null, this.div, false);

		showControl(this.div);
		if (lpDisp) { setDisplay(document, 'tblForm', true); oProgress.setStyle(); }
	}
	this.loaded = true;
	if (this.initialized && this.funcFinish) window.setTimeout(this.funcFinish, 0);
	this.initialized = false; this.dir = 0; if (this.bar) this.bar.style.pixelLeft = 0;
}
FDProgress.prototype.move = function() {
	if (this.position == 0) return;
	var oDim = getWindowDim(window), form = getElement(document, 'form'), top, left;
	top = ((oDim.clientHeight / 2) - (this.window.offsetHeight / 2)) + oDim.scrollTop;
	left = ((oDim.clientWidth / 2) - (this.window.offsetWidth / 2)) + oDim.scrollLeft;
	layerMove(null, this.div, left, top);
	showControl(this.div);
	hideControl('SELECT', this.div);

	if (typeof WND != 'undefined' && WND.isLoading) WND.showLoadDIV(false, WND.top, true);
}
FDProgress.prototype.move2 = function() {
	if (this.position == 0) return;
	var oDim = getWindowDim(window);
	layerMove(null, this.div, ((oDim.clientWidth / 2) - (this.div.offsetWidth / 2)) + oDim.scrollLeft, ((oDim.clientHeight / 2) - (this.div.offsetHeight / 2)) + oDim.scrollTop);
}
FDProgress.prototype.move3 = function() {
	if (this.position == 0) return;
	var form = getElement(document, 'form'), top = form.offsetTop + (form.clientHeight / 2) + form.scrollTop - 50, left = form.offsetLeft + (form.clientWidth / 2) + form.scrollLeft - 160;
	layerMove(null, this.div, left, top);
}
FDProgress.prototype.setStyle = function() {
	if (!this.div) this.div = getElement(document, 'divProgressWindow');
	if (this.div) {
		if (FD.Browser.ie) {
			var div = document.body.appendChild(this.div.cloneNode(true));
			this.div.removeNode(true);
		} else {
			var div = this.div.parentNode.removeChild(this.div);
			document.body.appendChild(div);
		}
		this.getRefs();
		//var el = getElement(document, 'tblInit') ;
		//el.removeNode(false) ;
		//this.div.appendChild(el);
		this.div.style.position = 'absolute';
		this.div.style.width = '320px';
	}
	if (this.position == 0) setDisplay(document, 'tblProgressPos', false);
	this.position = 1;
	var oTbl = getElement(document, 'tblProgressWindow');
	oTbl.className = oTbl.getAttribute('prevClass');
	setBorder(oTbl, '#66667E 1px solid');
	setBorder(getElement(document, 'tblPWOutset'), '#E0DFE3 2px outset');
	var oEl = getElement(document, 'tdPWOutset');
	if (oEl.style) oEl.style.backgroundColor = '#F0F1F4';
	oEl = getElement(document, 'tblPWOutset');
	oEl.cellPadding = '1';
	//	getElement( document, 'tblProgressWindow' ).bgColor='#E0DFE3';
}
FDProgress.prototype.writeTitle = function(cpSubTitle, lpInit) {
	if (false /* no subtitles this moment */ && cpSubTitle) {
		if (cpSubTitle == this.subtitle) return;
		this.subtitle = cpSubTitle;
		if (!this.subtitled) this.setSubTitle(true);
		layerWrite(null, this.title, unescape('%3Cspan style="font-size:8pt; font-weight: normal"%3E- ' + cpSubTitle + ' -%3C/span%3E%3Cbr%3E' + this.titlestr));
	} else if (this.subtitled) {
		this.setSubTitle(false);
		layerWrite(null, this.title, this.titlestr);
	} else if (lpInit) layerWrite(null, this.title, this.titlestr);
}
FDProgress.prototype.setSubTitle = function(lpSet) {
	this.subtitled = lpSet;
	//if( this.div.style ) this.div.style.height = ( parseFloat( this.div.style.height ) + ( lpSet ? 60 : -10 ) ) + 'px';
}
