			var a=1;
			var c=0;
			var uptimer;
			var prev=0;
			var ctime=0;

			var options = { 
			        beforeSubmit:  showRequest,  // pre-submit callback 
				success:       showResponse,  // post-submit callback 
				url: "upload.php"
			}; 


			settings = {
				interval: 1000,
		    		progress_bar_id: "progressbar",
				nginx_progress_url: "/progress"
			};

 
			function showResponse(responseText, statusText)  { 
				c++
				if (responseText.indexOf ("Request Entity Too Large")>-1)
				{
				$("#result").append("<p class=\"info\">Произошла ошибка при загрузке файла</p><p class=\"info\">(возможно файл слишком большой)</p><hr/>");
				}
				else
				{
				$("#result").append(responseText);
				}
				if (c<a) {
					$("#counter").html("Файл "+(c+1)+" из "+a);
					$("#bar").width("0%");
					$("#add"+c).ajaxSubmit(options);
				}
				else {
					$("#cancel").hide();
					$("#progressbar").width("100%");
					$("#uploading").slideUp();
				}
			} 

function nginxUploadProgress (e, nginx_progress_url, progress_bar_id, uuid) {
	$.ajax({
		type: "GET",
		url: nginx_progress_url,
		dataType: "json",
		beforeSend: function(xhr) {
			xhr.setRequestHeader("X-Progress-ID", uuid);
		},
		success: function(upload) {
			/* change the width if the inner progress-bar */
			if (upload.state == 'uploading') {
				ctime+=1000;
				bar = $('#'+progress_bar_id);
				w = Math.floor((upload.received / upload.size)*100);
				bar.width(w + '%');
				speed=Math.floor((upload.received*1000/ctime)/1024);
				$('#progrcount').html(Math.floor(upload.received/1024)+"Кб/"+Math.floor(upload.size/1024)+"Кб");
				$('#percount').html(Math.floor(upload.received*100/upload.size)+"%");
				time=(upload.size-upload.received)/(upload.received*1000/ctime);
				if (time < 60) {
				    $('#timeleft').html("Осталось: "+Math.floor(time)+" сек.");
				}
				else {
				    $('#timeleft').html("Осталось: "+Math.floor(time/60)+" мин.");
				}
				$('#speed').html("Скорость: "+speed+" Кб/с");
				prev=upload.received;
			}
			/* we are done, stop the interval */
			if (upload.state == 'done') {
				window.clearTimeout(e.timer);
			}
		}
	});
};

			
			function generateNGINX() {
			/* generate random progress-id */
				var uuid = "";
				for (i = 0; i < 32; i++) { uuid += Math.floor(Math.random() * 16).toString(16); }
				/* patch the form-action tag to include the progress-id */
				options.url="upload.php?X-Progress-ID=" + uuid;
				this.timer = window.setInterval(function() { nginxUploadProgress(this, settings['nginx_progress_url'], settings['progress_bar_id'], uuid) }, settings['interval']);
				return options;
			}	
			

			function showRequest(formData, jqForm, options) {
				prev=0;
				ctime=0;
				var queryString = $.param(formData); 	
				options=generateNGINX();
				return true;
			} 
			
			$(document).ready(function(){
					$("#add").click(function() {
						if (a<5)
						{
						$("#formpanel").append("<form id=\"add"+a+"\" method=\"post\" action=\"upload.php\"><input type=\"file\" name=\"upfile\" class=\"uploader\" size=\"45\"><input class=\"maxsize\" name=\"MAX_FILE_SIZE\" value=\"209715205\" type=\"hidden\"/><input name=\"exchangers\" class=\"exch\" value=\"\" type=\"hidden\"/></form>");
						a++;
						}
						else
						{
						alert("Достигнуто максимальное количество файлов!");
						}
						return false;
					});
					
					$("#uploadit").click(function() {
						var res=" ";
						var nominimal=1;
						var minimal=0;
						$(".serviceitem").each(
						function (intIndex) {
						    if (this.checked)
						    {
							    if (nominimal==1||Number(minimal)>Number(this.id))
							    {
								minimal=this.id;
								nominimal=0;
							    }
						    res+=this.value+" ";
						    }
						}
						);
						$(".maxsize").attr("value",minimal);						    
						if (res==" ")
						    alert("Выберите файлообменники для загрузки!");
						else
						{
						    $(".exch").attr("value",res);
						    $("#formwrap").slideUp();
						    $("#uploading").slideDown();
						    $("#counter").html("Файл 1 из "+a);
						    options=generateNGINX();
    						    $(".serviceitem").attr("disabled", "disabled")
						    $("#add0").ajaxSubmit(options);
						}
					});
				
					$(".service").click(function() {
					var mmin=0;
					var nommin=1;
					$(".serviceitem").each(
						function (intIndex) {
						    if (this.checked)
						    {
							    if (nommin==1||Number(mmin)>Number(this.id))
							    {
								mmin=this.id;
								nommin=0;
							    }
						    }
						}
		    			);
					$(".msize").html(Math.floor(mmin/1024/1024));
					
					
					});
					
					$("#cancel").click(function() {
					
						window.stop();
						document.execCommand("Stop");
						window.clearTimeout(this.timer);

					});
					var mmin=0;
					var nommin=1;
					$(".serviceitem").each(
						function (intIndex) {
						    if (this.checked)
						    {
							    if (nommin==1||Number(mmin)>Number(this.id))
							    {
								mmin=this.id;
								nommin=0;
							    }
						    }
						}
		    			);
					$(".msize").html(Math.floor(mmin/1024/1024));

			
			});
