
/* UPLOAD LISTENER */

var uploadListener = {
	init: function() {
		uploadListener.loadXML();
	},
	startThread: function() {
		setTimeout(function() {
			uploadListener.loadXML();
		}, uploadListener.properties.interval);
	},
	loadXML: function() {
		$.ajax({
			url: uploadListener.properties.url,
			data: uploadListener.properties.data,
			cache: uploadListener.properties.cache,
			async: uploadListener.properties.async,
			type: uploadListener.properties.type,
			dataType: uploadListener.properties.dataType,
			timeout: uploadListener.properties.timeout,
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				if(uploadListener.properties.debug) {
					alert("Error loading XML document: " + textStatus);
				}
				uploadListener.startThread();
			},
			success: function(xml, textStatus) {
				if(uploadListener.properties.debug) {
					alert("XML document loaded: " + textStatus);
				}
				uploadListener.readXML(xml);
				uploadListener.updateProgressBar();
				if(uploadListener.counters.percentageRead < 100) {
					uploadListener.startThread();
				}
			}
		});
	},
	readXML: function(xml) {
		if((uploadListener.counters.bytesRead > 0) && (parseInt($(xml).find("bytesRead").text()) == 0)) {
			uploadListener.counters.bytesRead = uploadListener.counters.contentLength;
			uploadListener.counters.readableBytesRead = uploadListener.counters.readableContentLength;
			uploadListener.counters.percentageRead = 100;
			uploadListener.counters.readablePercentageRead = "100%";
		} else {
			uploadListener.counters.bytesRead = parseInt($(xml).find("bytesRead").text());
			uploadListener.counters.readableBytesRead = $(xml).find("readableBytesRead").text();
			uploadListener.counters.contentLength = parseInt($(xml).find("contentLength").text());
			uploadListener.counters.readableContentLength = $(xml).find("readableContentLength").text();
			uploadListener.counters.percentageRead = parseInt($(xml).find("percentageRead").text());
			uploadListener.counters.readablePercentageRead = $(xml).find("readablePercentageRead").text();
		}
		if(uploadListener.properties.debug) {
			alert("bytesRead: " + uploadListener.counters.bytesRead + ", readableBytesRead: " + uploadListener.counters.readableBytesRead + ", contentLength: " + uploadListener.counters.contentLength + ", readableContentLength: " + uploadListener.counters.readableContentLength + ", percentageRead: " + uploadListener.counters.percentageRead + ", readablePercentageRead: " + uploadListener.counters.readablePercentageRead);
		}
	},
	updateProgressBar: function() {
		if(uploadListener.counters.percentageRead < 100) {
			document.getElementById(uploadListener.properties.uploadStatusId).innerHTML = uploadListener.properties.messageUploading;
		} else if(uploadListener.counters.percentageRead == 100) {
			document.getElementById(uploadListener.properties.uploadStatusId).innerHTML = uploadListener.properties.messageUploaded;
		}
		document.getElementById(uploadListener.properties.progressBarId).style.backgroundPosition = (-1 * (uploadListener.properties.progressBarWidth - (uploadListener.counters.percentageRead * (uploadListener.properties.progressBarWidth / 100)))).toString() + "px" + " " + "top";
		document.getElementById(uploadListener.properties.percentageReadId).innerHTML = uploadListener.counters.readablePercentageRead;
		document.getElementById(uploadListener.properties.bytesReadId).innerHTML = uploadListener.counters.readableBytesRead;
		document.getElementById(uploadListener.properties.contentLengthId).innerHTML = uploadListener.counters.readableContentLength;
	},
	counters: {
		bytesRead: 0,
		readableBytesRead: null,
		contentLength: 0,
		readableContentLength: null,
		percentageRead: 0,
		readablePercentageRead: null
	},
	properties: {
		debug: false,
		url: "/e-barty/servlets/FileUploadProgressListener",
		data: "uploadListener=uploadListener",
		cache: false,
		async: false,
		type: "GET",
		dataType: "xml",
		timeout: 0,
		interval: 500,
		progressBarWidth: 100,
		uploadStatusId: "uploadStatus",
		progressBarId: "progressBar",
		percentageReadId: "percentageRead",
		bytesReadId: "bytesRead",
		contentLengthId: "contentLength",
		messageUploading: "Caricamento del file in corso ...",
		messageUploaded: "File caricato"
	}
};