// Called instead of the SWFUpload _showUI method

var my_is_uploading_stoped = false;
var my_is_bulk_uploading = false;
var FeaturesDemo = {
	start: function (swf_upload_instance) {
		FeaturesDemo.SU = swf_upload_instance;

		FeaturesDemo.cacheFields();

		/*FeaturesDemo.btnBrowse.onclick = function () {
			try {
				FeaturesDemo.selectFiles();
			} catch (ex) {
			}
			return false;
		};*/
		FeaturesDemo.btnStartAllFiles.onclick = function () {
			try {
				FeaturesDemo.startAllFiles();
			} catch (ex) {
			}
			return false;
		};
		FeaturesDemo.btnStartSelectedFile.onclick = function () {
			try {
				FeaturesDemo.startSelectedFile();
			} catch (ex) {
			}
			return false;
		};
		FeaturesDemo.btnStopUpload.onclick = function () {
			try {
				my_is_uploading_stoped = true;
				FeaturesDemo.stopUpload();
			} catch (ex) {
			}
			return false;
		};
		FeaturesDemo.btnCancelSelectedFile.onclick = function () {
			try {
				FeaturesDemo.cancelSelectedFile();
			} catch (ex) {
			}
			return false;
		};
		FeaturesDemo.btnCancelAllFiles.onclick = function () {
			try {
				FeaturesDemo.cancelAllFiles();
			} catch (ex) {
			}
			return false;
		};
		FeaturesDemo.btnClearQueue.onclick = function () {
			try {
				FeaturesDemo.clearQueue();
			} catch (ex) {
			}
			return false;
		};
		/*FeaturesDemo.btnClearSshots.onclick = function () {
			try {
				FeaturesDemo.clearSshots();
			} catch (ex) {
			}
			return false;
		};*/
	},
	cacheFields: function () {
		if (FeaturesDemo.is_cached) {
			return;
		}

		FeaturesDemo.btnBrowse = document.getElementById("btnBrowse");
		FeaturesDemo.selQueue = document.getElementById("selQueue");
		FeaturesDemo.selQueueTxt = document.getElementById("selQueueTxt");
		FeaturesDemo.thumbnails = document.getElementById("thumbnails");
		FeaturesDemo.btnStartSelectedFile = document.getElementById("btnStartSelectedFile");
		FeaturesDemo.btnStartAllFiles = document.getElementById("btnStartAllFiles");
		FeaturesDemo.btnStopUpload = document.getElementById("btnStopUpload");
		FeaturesDemo.btnCancelSelectedFile = document.getElementById("btnCancelSelectedFile");
		FeaturesDemo.btnCancelAllFiles = document.getElementById("btnCancelAllFiles");
		FeaturesDemo.btnClearQueue = document.getElementById("btnClearQueue");
		//FeaturesDemo.btnClearSshots = document.getElementById("btnClearSshots");

		FeaturesDemo.is_cached = true;
	},
	clearAll: function () {
		FeaturesDemo.selQueue.options.length = 0;

	},

	selectFiles: function () {
		FeaturesDemo.SU.selectFiles();
	},
	startAllFiles: function () {
		if (FeaturesDemo.selQueue.options.length === 0) {
			alert("You must queue a file first");
			return;
		}

		my_is_bulk_uploading = true;
		FeaturesDemo.SU.startUpload();
	},
	startSelectedFile: function () {
		if (FeaturesDemo.selQueue.options.length === 0) {
			alert("You must queue a file first");
			return;
		}
		if (FeaturesDemo.selQueue.selectedIndex === -1) {
			alert("Please select a file from the queue.");
			return;
		}

		my_is_bulk_uploading = false;
		var file_id = FeaturesDemo.selQueue.value;
		FeaturesDemo.SU.startUpload(file_id);
	},
	stopUpload: function () {
		FeaturesDemo.SU.stopUpload();
	},
	cancelSelectedFile: function () {
		if (FeaturesDemo.selQueue.options.length === 0) {
			alert("You must queue a file first");
			return;
		}
		if (FeaturesDemo.selQueue.selectedIndex === -1) {
			alert("Please select a file from the queue.");
			return;
		}

		var file_id = FeaturesDemo.selQueue.value;
		FeaturesDemo.SU.cancelUpload(file_id);
	},
	cancelAllFiles: function () {
		if (FeaturesDemo.selQueue.options.length === 0) {
			alert("You must queue a file first");
			return;
		}

		FeaturesDemo.SU.stopUpload();

		var stats = FeaturesDemo.SU.getStats();
		while (stats.files_queued > 0) {
			FeaturesDemo.SU.cancelUpload();
			stats = FeaturesDemo.SU.getStats();
		}
	},
	clearQueue: function () {
		FeaturesDemo.SU.stopUpload();

		var stats = FeaturesDemo.SU.getStats();

		if (stats.files_queued > 0) {
			if (!confirm('There are files in the queue haven\'t been uploaded yet. Do you really want to clear queue?'))
				return;
		}

		while (stats.files_queued > 0) {
			FeaturesDemo.SU.cancelUpload();
			stats = FeaturesDemo.SU.getStats();
		}

		FeaturesDemo.selQueue.length = 0;
	},
	clearSshots: function () {
		FeaturesDemo.thumbnails.innerHTML = ' ';
	}
};
