var PINT_Swfupload = {
	swfUploadLoaded : function () {
		// Hide the loading and alternate UIs, show flashUI
		document.getElementById('loadingUI').style.display = "none";
		document.getElementById('altUI').style.display = "none";

		PINT_SwfUI.start(this);  // This refers to the SWFObject because SWFUpload calls this with .apply(this).
	},
	
	// Called when correct flash version is found, hide alt content
	swfuploadPreLoad : function () {
		// Show the loading UI, hide the alt UI
		document.getElementById('loadingUI').style.display = "block";
		document.getElementById('altUI').style.display = "none";
		document.getElementById('flashUI').style.display = "block";
			
	},
	

	uploadError : function (file, errorCode, message) {
		try {
			var errorName = "";
			switch (errorCode) {
			case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
				errorName = "HTTP ERROR";
				break;
			case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
				errorName = "MISSING UPLOAD URL";
				break;
			case SWFUpload.UPLOAD_ERROR.IO_ERROR:
				errorName = "IO ERROR";
				break;
			case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
				errorName = "SECURITY ERROR";
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
				errorName = "UPLOAD LIMIT EXCEEDED";
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
				errorName = "UPLOAD FAILED";
				break;
			case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
				errorName = "SPECIFIED FILE ID NOT FOUND";
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
				errorName = "FILE VALIDATION FAILED";
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
				errorName = "FILE CANCELLED";
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
				errorName = "FILE STOPPED";
				break;
			default:
				errorName = "UNKNOWN";
				break;
			}
			
			// default: do not requeue on error. remove from the queue
			for (var i = 0; i < PINT_SwfUI.selQueue.options.length; i++) {
				if (PINT_SwfUI.selQueue.options[i].value == file.id) {
					PINT_SwfUI.selQueue.options[i] = null;
				}
			}
			
			errorMessage = file.name + " : " + message;
			
			if (errorCode != SWFUpload.UPLOAD_ERROR.FILE_CANCELLED) {
				alert(errorMessage);
			}

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

	// Add to select box
	fileQueued : function (file) {
		try {
			var queueString = file.name;
			
			PINT_SwfUI.selQueue.options[PINT_SwfUI.selQueue.options.length] = new Option(queueString, file.id);
		
		} catch (ex) {
			this.debug(ex);
		}
	},
	
	fileQueueError : function (file, errorCode, message) {
		try {
			var errorName = "";
			switch (errorCode) {
			case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
				errorName = "QUEUE LIMIT EXCEEDED";
				break;
			case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
				errorName = "FILE EXCEEDS SIZE LIMIT";
				break;
			case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
				errorName = "ZERO BYTE FILE";
				break;
			case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
				errorName = "INVALID FILE TYPE";
				break;
			default:
				errorName = "UNKNOWN";
				break;
			}

			var errorString = errorName + ":File ID: " + (typeof(file) === "object" && file !== null ? file.id : "na") + ":" + message;
			//FeaturesDemo.selEventsQueue.options[FeaturesDemo.selEventsQueue.options.length] = new Option("File Queue Error: " + errorString, "");

		} catch (ex) {
			this.debug(ex);
		}
	},
	
	// Creates a new progress bar for each file
	uploadProgress: function (file, bytesLoaded, bytesTotal) {

		try {
			var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
			var progress = new FileProgress(file, this.customSettings.progress_target);
			progress.setProgress(percent);
			progress.setStatus("Uploading...");
		} catch (e) {
			this.debug(e);
		}
	},	

	uploadSuccess: function(file, serverData) {
		try {
			var progress = new FileProgress(file, this.customSettings.progress_target);
			progress.setComplete();
			progress.setStatus("Complete.");
			progress.toggleCancel(false);
			
			if (serverData === " ") {
				this.customSettings.upload_successful = false;
			} else {
				this.customSettings.upload_successful = true;
				// append the successfully uploaded asset id to the list
				var currentAssets = PINT_SwfUI.assetHiddenField.value;
				var currentAssetsArray = new Array();
				if (currentAssets != "") {
					currentAssetsArray = currentAssets.split(",");
				} 
				currentAssetsArray.push(serverData);	
				PINT_SwfUI.assetHiddenField.value = currentAssetsArray.join(",");
				
				// does the same for file names and the fileHiddenField
				var currentFiles = PINT_SwfUI.fileHiddenField.value;
				
				var currentFilesArray = new Array();
				if (currentFiles != "") {
					currentFilesArray = currentFiles.split(",");
				} 
				currentFilesArray.push(file.name);	
				
				PINT_SwfUI.fileHiddenField.value = currentFilesArray.join(",");
			}
			
		} catch (e) {
			this.debug(e);
		}
	},
	uploadDone: function() {
		try {
		PINT_SwfUI.form.submit();
				//;
		} catch (ex) {
			this.debug(ex);
			alert("Error submitting form");
		}
	},
	
	uploadStart : function (file) {
		try {
		
		} catch (ex) {
			this.debug(ex);
		}

		return true;
	},
	
	uploadComplete: function(file) {
		try {
			// if successful upload, check if have additional files
			if (this.customSettings.upload_successful) {
				var stats = PINT_SwfUI.SU.getStats();
				if (stats.files_queued == 0) {
					PINT_SwfUI.btnBrowse.disabled = "true";
					PINT_SwfUI.cancelButton.disabled = "true";
					PINT_Swfupload.uploadDone();
				}
			} else {
				var progress = new FileProgress(file, this.customSettings.progress_target);
				progress.setError();
				progress.setStatus("File rejected");
				progress.toggleCancel(false);
				
				//var txtFileName = document.getElementById("txtFileName");
				// TODO: let's remove the option from the select queue
				var sQueue = PINT_SwfUI.selQueue;
				var numOptions = PINT_SwfUI.selQueue.length;
				for (var i = 0; i < numOptions; i++) {
					if (sQueue.options[i].value == file.id) {
						sQueue.options[i] = null;
						break;
					}
				}
			
				alert("There was a problem with the upload.\nThe server did not accept it.");
			}
		} catch (e) {
		}
	},
	
	// This custom debug method sends all debug messages to the Firebug console.  If debug is enabled it then sends the debug messages
	// to the built in debug console.  Only JavaScript message are sent to the Firebug console when debug is disabled (SWFUpload won't send the messages
	// when debug is disabled). 
	debug : function (message) {
		try {
			if (window.console && typeof(window.console.error) === "function" && typeof(window.console.log) === "function") {
				if (typeof(message) === "object" && typeof(message.name) === "string" && typeof(message.message) === "string") {
					window.console.error(message);
				} else {
					window.console.log(message);
				}
			}
		} catch (ex) {
		}
		try {
			if (this.settings.debug) {
				this.debugMessage(message);
			}
		} catch (ex1) {
		}
	} 

};
