var timeout = 1000;
var data_index = "";
var uploaded = false;
var uploadingIds = [];
var timeoutId = "";

$(document).ready(function()
{
	if(uploadIds && uploadIds.length > 0)
	{
		uploadingIds = uploadIds.split(",");
	}
	updateUploadProgress();
});

function fileSizeAsString(size)
{
	var size_obj = Units.getFileSize(size);
	return size_obj.size + " " + locale.uploadProgress[size_obj.units];
}
	
function addToUpload(id)
{
	uploadingIds.push(id);
}

/*function removeFromUpload(id)
{
	index = uploadingIds.indexOf(id);
	if(uploadingIds.length == 1)
	{
		clearUploadingIds();
	}
	else
	{
		urloadingIds.splice(index,1);
	}
	clearTimeout(timeoutId);
	updateUploadProgress();
}*/

function clearUploadingIds()
{
	uploadingIds = [];
}

function updateUploadProgress()
{
	if(uploadingIds.length > 0)
	{
		$.get(uploadProgressUrl, {"ids" : uploadingIds.join(",")}, 
		function(data)
		{
			var json = eval(data);
			if (data == data_index)
			{
				if (timeout > 30000)
				{
					timeout = 60000;
				}
				else
				{
					timeout = timeout * 2;
				}
			}
			else
			{
				data_index = data;
				timeout = 1000;
			}
			var progress = $("#total-uploading-progress");
			total = json.size;
			if (total == 0)
			{
				progress.addClass("hidden");
				clearTimeout(timeoutId);
				updateUploadProgressTimeout();
				return false;
			}
			if (total > 0)
			{
				progress.removeClass("hidden");
			}
			percent = Math.round(json.received / json.size * 100);
			progress.find("span.total").html(fileSizeAsString(total));
			progress.find("span.percent").html(percent + "%");

			if (percent == 100)	// все загрузилось
			{
				clearUploadingIds();
				progress.addClass("hidden");
				if (typeof json.files != "undefined")
				{
					$.each(json.files, function()
					{
						total = this.size;
						file = $("#file-" + this.id);
						file.find(".upload-progress").remove();
						file.find("td.size").html("<span>" + fileSizeAsString(total) + "</span>");
					});
				}
			}
			if (typeof json.files != "undefined")
			{
				clearUploadingIds(); // сбрасываем список загружаемых файлов
				errors = 0;
				$.each(json.files, function()
				{
					total = this.size;
					percent = Math.round(this.received / this.size * 100);
					if (this.state == 'done')
					{
						file = $("#file-" + this.id);
						file.find(".upload-progress").remove();
						file.find("td.size").html("<span>" + fileSizeAsString(total) + "</span>");
						var deleteDate = new Date(new Date().getTime() + 1000*60*60*24*30);
						file.find("td.delete-date span").html(getDateAsString(deleteDate));
					}
					else
					{
						file = $("#file-" + this.id);
						file.find(".upload-progress span.total").html(fileSizeAsString(total));
						file.find(".upload-progress span.percent").html(percent + "%");
						if (this.error != null)
						{
							file.find("span.error").removeClass("hidden");
							file.find(".upload-progress").remove();
							errors++;
						}
						else
						{
							file.find("span.error").addClass("hidden");
							addToUpload(this.id.toString());
						}
					}
				});
				if(errors == json.files.length){
					clearTimeout(timeoutId);
					progress.addClass("hidden");
					clearUploadingIds();
					updateUploadProgressTimeout();
					return false;
				}
			}
			updateUploadProgressTimeout();
		});
	}
	else
	{
		updateUploadProgressTimeout();
	}
}

function getDateAsString(date)
{
	var result = "";
	var day = date.getDate();
	result += (day < 10 ? "0" : "") + day + ".";
	var month = date.getMonth() + 1;
	result += (month < 10 ? "0" : "") + month + "." + date.getFullYear();
	return result;
}

function updateUploadProgressTimeout()
{
	clearTimeout(timeoutId);
	timeoutId = setTimeout(function()
	{
		updateUploadProgress();
	}, timeout);
}
