var PINT_SwfUI = {
	
	start: function (swf_upload_instance) {
		// this is the SWFObject
		PINT_SwfUI.SU = swf_upload_instance;

		// makes page elements available
		PINT_SwfUI.cacheFields();
		
		// assigns an onclick to the button browse for more than one file
		PINT_SwfUI.btnBrowse.onclick = function () {
			try {
				PINT_SwfUI.selectFiles();
			} catch (ex) {
			}
			return false;
		};
		
		// assigns an onclick to the button browse for more than one file
		PINT_SwfUI.cancelButton.onclick = function () {
			try {
				PINT_SwfUI.cancelSelectedFile();
			} catch (ex) {
			}
			return false;
		};
		
		PINT_SwfUI.form.onsubmit = this.doSubmit;
		
		//document.getElementById("spanLoadStatus").innerHTML = "loaded";
	},
	
	selectFiles: function () {
		PINT_SwfUI.SU.selectFiles();
	},
	
	cancelSelectedFile: function () {
		if (PINT_SwfUI.selQueue.options.length === 0) {
			alert("You must queue a file first");
			return;
		}
		if (PINT_SwfUI.selQueue.selectedIndex === -1) {
			alert("Please select a file from the queue.");
			return;
		}

		var file_id = PINT_SwfUI.selQueue.value;
		PINT_SwfUI.SU.cancelUpload(file_id);
		// remove it from selected queue
		for (var i = 0; i < PINT_SwfUI.selQueue.options.length; i++) {
			// remove from the queue if it's been canceled
			if (PINT_SwfUI.selQueue.options[i].value == file_id) {
				PINT_SwfUI.selQueue.options[i] = null;
				break;
			}
		}
	},
	
	
	cacheFields: function () {
		if (PINT_SwfUI.is_cached) {
			return;
		}
		
		PINT_SwfUI.btnBrowse = document.getElementById(PINT_SwfUI.SU.customSettings.select_files_id);
		PINT_SwfUI.cancelButton = document.getElementById(PINT_SwfUI.SU.customSettings.remove_files_id);
		PINT_SwfUI.selQueue = document.getElementById(PINT_SwfUI.SU.customSettings.select_id);
		PINT_SwfUI.form = document.getElementById(PINT_SwfUI.SU.customSettings.form_id);
		PINT_SwfUI.assetHiddenField = document.getElementById(PINT_SwfUI.SU.customSettings.asset_hidden_id);
		PINT_SwfUI.fileHiddenField = document.getElementById(PINT_SwfUI.SU.customSettings.file_hidden_id);

	
		PINT_SwfUI.is_cached = true;
	},
	
	// Called by the submit button to start the upload
	doSubmit: function(e) {
		e = e || window.event;
		if (e.stopPropagation) {
			e.stopPropagation();
		}
		e.cancelBubble = true;
		
		try {
			// check if there are any files queued at all
			var stats = PINT_SwfUI.SU.getStats();
			// if no queued files, just submit
			if (stats.files_queued == 0) {
				PINT_SwfUI.btnBrowse.disabled = "true";
				PINT_SwfUI.cancelButton.disabled = "true";
				PINT_Swfupload.uploadDone();
			} else {
				PINT_SwfUI.SU.startUpload();
			}
		} catch (ex) {

		}
		return false;
	}

};
