progress = new Array()
progressing = false
id = 0
$(document).ready(
	function(){
		$('.progress').each(
			function(){
				progress.push(this.id)
			}
		)
		if(progress.length > 0){
			id = setInterval("get_progress()",2000)
		}
	}
)

function get_progress(){
	if(!progressing){
		progressing = true
		$.post(
			'ajax_progress.php',
			{id: progress.join()},
			got_progress,
			'json'
		)
	}
}

function got_progress(data,status){
	progressing = false
	
	for(each in data){
		percent = data[each]
		if(percent == 1 || percent == "e"){
			var parent = $('#' + each).parent()
			var name = $('p',parent).html()
			
			if(percent == 1){
				parent.html("<a href='index.php?request=download&id="+each+"'>"+name+"</a>")
				parent.parent().removeClass('inactive').css('display','none').fadeIn(1000)
			} else {
				parent.html(name)
				$('td:last',parent.parent()).html("An error has occured with this export. Our technical staff has been notified, and will restart the export when the problem has been resolved.")
				parent.parent().css('display','none').fadeIn(1000)
			}
			
			for(i in progress){
				if(progress[i] == each){
					progress.splice(i,1)
				}
			}
		} else {
			percent = Math.min(Math.max(data[each],0.02),0.98)
			$('#' + each + ' .inner').animate({width: (100*percent)+'%'},400)
		}
	}
	
	if(progress.length == 0){
		clearInterval(id)
	}
}