// JavaScript Document
var q_total = 0;
var q_i = 1;


function fileQueueError(file, errorCode, message) {
	try {
		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
			alert("Je hebt teveel files geselecteerd");
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			alert("Ongeldige foto");
			break;
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			alert("Dit bestand is te groot, kies een andere foto. (max 10mb)");
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
		default:
			alert(message);
			break;
		}

	} catch (ex) {
		this.debug(ex);
	}

}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesQueued > 0) {
			// q_total = files queued, q_i = current file
			q_total = numFilesQueued;
			q_i = 1;
			
			// Start uploading
			this.startUpload();
			
			// Init progressbar and hide button
			$('#progressbar').css('width','0%');
			$('#uploadbutton').hide();
			$('#uploadbar').show();
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadProgress(file, bytesLoaded) {

	try {
		var percent = Math.ceil((bytesLoaded / file.size) * 100);
		$('#progressbar').css('width',percent + '%');
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadSuccess(file, serverData) {
	try {
		if (serverData.substring(0, 7) === "FILEID:") {
			avaid = serverData.substring(7);
			$('#avaid').val(avaid);
			document.avatar.submit();
		} else {
			// Parse error
		}


	} catch (ex) {
		this.debug(ex);
	}
}

function uploadComplete(file) {
	try {
		//  if more files are queued, start next one
		if (this.getStats().files_queued > 0) {
			q_i++;
			this.startUpload();
		} else {
			// all uploads complete :)
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadError(file, errorCode, message) {
	var progress;
	try {
		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			try {
				// Cancelled
			}
			catch (ex1) {
				this.debug(ex1);
			}
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			try {
				//Stopped
			}
			catch (ex2) {
				this.debug(ex2);
			}
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
				// Uploadlimit
			break;
		default:
			alert(message);
			break;
		}

	} catch (ex3) {
		this.debug(ex3);
	}
}
