// *****************************************************************
// eBD Upload Progress.
// progressbar.js
// *****************************************************************


var progressBar = {
	txt_total_size: 'Total Size',
	txt_transferred: 'Transferred',
  	txt_bandwidth: 'Bandwidth',

	currentId: null,
    currentBar: null,

	last_received: 0,
	last_time: 0,

	showProgressBar: function(form,divid)
	{
		var id = '';
		var chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		for (var i=0;i<32;i++)
		{ id += chars.charAt(Math.floor(Math.random()*62)); }

		if (form.action.match(/\?/))
        { form.action += '&uploadprogress=' + id; }
	    else
	    { form.action += '?uploadprogress=' + id; }
		
		this.currentId = id;
		this.currentBar = document.getElementById(divid);
		
		this.last_received=0;
		var aux = new Date();
		this.last_time= aux.getTime();

		setTimeout( progressBar.request_progress, 500);
		return true;
	},

    stopProgressBar: function()
    { this.currentId = null; },
    
	request_progress: function()
	{
		if (progressBar.currentId != null)
		{
			var url = '/_ajax/UploadProgress.get_progress?id=' + progressBar.currentId;
			ajaxCaller.getXML(url, progressBar.receive_progress);
		}
	},

	receive_progress: function(xml)
	{
		if (xml != null)
		{
			var prog = xml.getElementsByTagName("progress")[0];
			if (prog != null)
			{
				var snode = prog.getElementsByTagName("size")[0];
				var rnode = prog.getElementsByTagName("received")[0];

				var size = parseInt(snode.firstChild.nodeValue);
				var received = parseInt(rnode.firstChild.nodeValue);
  				
  				if (progressBar.last_received != received)
  				{ progressBar.update_progress_bar_data(size,received); }
  				
				if ( (received < size) || (progressBar.last_received == 0) )
				{ setTimeout( progressBar.request_progress, 500 ); }
			}
			else
			{
				// lo reintento, pq todavia no habia empezado a recibir nada
				if (progressBar.last_received==0)
				{ setTimeout( progressBar.request_progress, 500 ); }
			}
		}
		else
		{
			// lo reintento, pq todavia no habia empezado a recibir nada
			if (progressBar.last_received==0)
			{ setTimeout( progressBar.request_progress, 500 ); }
		}
	},
	
	update_progress_bar_data: function(size,received)
	{
		if (this.currentBar != null)
		{
			var last = this.last_time;
			var aux = new Date();
			var now = aux.getTime();		
			
			var block = received - this.last_received;
			var elapsed = now - last;  //son milisegundos

			var percent = received/size * 100;
			percent = percent.toFixed(0);
			
			this.last_time = now;
			this.last_received = received;

			var sunits = 'Bytes';
			if (size > 1024)
			{ size = size/1024; sunits='KB'; }
			if (size > 1024)
			{ size = size/1024; sunits='MB'; }
			if (sunits != 'Bytes')
			{ if (size.toFixed) { size = size.toFixed(2); } }
			

			var runits = 'Bytes';
			if (received > 1024)
			{ received = received/1024; runits='KB'; }
			if (received > 1024)
			{ received = received/1024; runits='MB'; }
			if (runits != 'Bytes')
			{ if (received.toFixed) { received = received.toFixed(2); } }
			

			var bw = block / (elapsed/1000);

			if (this.last_bw == null)
			{ this.last_bw = bw; }
			this.last_bw=bw-(bw - this.last_bw)*(1/Math.exp(elapsed/1000/10));

			var speed = this.last_bw;
			var bunits = 'Bytes/s';
			if (speed > 1024)
			{ speed = speed/1024; bunits='KB/s'; }
			if (speed > 1024)
			{ speed = speed/1024; bunits='MB/s'; }
			if (speed.toFixed) { speed = speed.toFixed(2); }


			var content = '<div class="uploadprogress">'+
						  '<div class="uploadprogressbar">'+
						  '<div class="uploadprogressdone" style="width:' + percent + '%;"></div>'+
						  '<div class="uploadprogresspercent" style="width:100%;">' + percent + '%</div>' +
						  '</div>'+
						  '<div class="uploadprogressbw">' + speed + ' ' + bunits + '</div>'+
						  '<div class="uploadprogresstransfer">' + received + ' ' + runits + ' / ' + size + ' ' + sunits + '</div>'+
						  '</div>';

			this.currentBar.innerHTML=content;
		}
	}
};


