function fileDialogStart() {
	_cancelUpload();
	this.customSettings.cancelled = false;
}

function fileQueued(file) {
	try {
		clearError();
		this.setButtonDisabled(true);
		disableSubmit();
		this.customSettings.upload_successful = false;
		$('#' + this.customSettings.field_id).val(file.name);
		file.id = "singlefile";	// This makes it so FileProgress only makes a single UI element, instead of one for each file
		progress = new FileProgress(file, this.customSettings.progress_target);
		progress.toggleCancel(true);
		progress.appear();
		progress.setProgress(0);
		this.customSettings.progress = progress;
		//callback
		customFileQueued(file);
	} catch (e) {
		console.log(e);
	}
}
function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesSelected){
			swfu.startUpload();
		}
	} catch (e) {
		this.setButtonDisabled(false);
		console.log(e);
	}
}

function uploadProgress(file, bytesLoaded, bytesTotal) {
	try {
		var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);

		//file.id = "singlefile";	// This makes it so FileProgress only makes a single UI element, instead of one for each file
		var progress = this.customSettings.progress;// new FileProgress(file, this.customSettings.progress_target);
		progress.appear();
		progress.setProgress(percent);
		progress.setStatus(this.customSettings.messages['UPLOADING']);
	} catch (e) {
		console.log(e);
	}
}

function uploadSuccess(file, serverData) {
	try {
		this.setButtonDisabled(false);
		file.id = "singlefile";	// This makes it so FileProgress only makes a single UI element, instead of one for each file
		var progress = this.customSettings.progress;//new FileProgress(file, this.customSettings.progress_target);
		progress.setComplete();
		progress.setStatus(this.customSettings.messages['COMPLETE']);
		progress.toggleCancel(false);
		if(serverData = eval('(' + serverData + ')')){
			if(serverData['success']){
				progress.setStatus(this.customSettings.messages['SUCCESS']);
				if (this.customSettings.show_preview){
					$('#thumbnails img').attr('src', image('/img/global/preLoading.gif'));
					previewImage = '/images/version/' + this.customSettings.preview_version + '/'  + '?i=' + serverData['name'];
					setTimeout("$('#thumbnails img').attr('src', '" + previewImage + "')", 1000);
				}
				
				this.customSettings.upload_successful = true;
				inputs = $('#container_' + this.customSettings.field_id + ' .inputs').empty();
				for (data in serverData){
					if(data != 'success'){
						inputs.append('<input type="hidden" name="' + this.customSettings.field_name + '[' + data + ']" value="' + serverData[data] + '">');
					}
				}
			}
			else{
				this.customSettings.upload_successful = false;
				if(serverData['errors'] != undefined){
					this.customSettings.server_errors = serverData['errors'];
				}
			}
		}
		else {
			this.customSettings.upload_successful = false;
		}
		
		// Callback
  		customUploadSuccess(this.customSettings.upload_successful, serverData);
  		
	} catch (e) {
		console.log(e);
	}
}

function uploadComplete(file) {
	try {
		if (!this.customSettings.upload_successful) {
			file.id = "singlefile";	// This makes it so FileProgress only makes a single UI element, instead of one for each file
			var progress = this.customSettings.progress;//new FileProgress(file, this.customSettings.progress_target);
			
			progress.setStatus('');
			if(this.customSettings.server_errors != undefined){
				showUploaderError(this.customSettings.server_errors);
			}	
			else if(!this.customSettings.cancelled){
				progress.setStatus(this.customSettings.messages['UNKNOWN']);
			}
			
			progress.setError();
			progress.toggleCancel(false);
			
			$('#' + this.customSettings.field_id).val('');			
		}
		enableSubmit();
				
		// Callback
  		customUploadComplete(this.customSettings.upload_successful);
  		
	} catch (e) {
		console.log(e);
	}
}


function fileQueueError(file, errorCode, message)  {
	try{
		this.setButtonDisabled(false);
		showUploaderError(this.customSettings.messages[errorCode]);
	}
	catch (e){
		console.log(e);
	}
}

function uploadError(file, errorCode, message) {
	try{
		this.setButtonDisabled(false);
		showUploaderError(this.customSettings.messages[errorCode]);
		
		if (errorCode == SWFUpload.UPLOAD_ERROR.FILE_CANCELLED){
			this.customSettings.progress.setCancelled();
		}
	}
	catch (e){
		console.log(e);
	}
}

// Triggered in the onclick event of the cancel link
function _cancelUpload(){
	try{
		swfu.customSettings.cancelled = true;
		swfu.cancelUpload();
	}
	catch (e){
		console.log(e);
	}
	return false;
}

function showUploaderError(message){
	alert(message);
	//$('#uploader_errors').html(message);
}

function clearError(){
	$('#uploader_errors').html('');
}

function disableSubmit(){
	$('input[type=submit]').attr('disabled', 'true');
}

function enableSubmit(){
	$('input[type=submit]').removeAttr('disabled');
}