var Photo123SWFHandlers = {

	swfUploadLoaded : function () {
		Photo123SWF.start(this);  // This refers to the SWFObject because SWFUpload calls this with .apply(this).
	},
	fileDialogStart : function () {
		try {
			//Photo123SWF.selEventsQueue.options[Photo123SWF.selEventsQueue.options.length] = new Option("File Dialog Start", "");
		} catch (ex) {
			this.debug(ex);
		}
	},

	fileQueued : function (file) {
		try {
			var queueString = "0%:" + file.name;
			Photo123SWF.selQueue.options[Photo123SWF.selQueue.options.length] = new Option(queueString, file.id);
			//Photo123SWF.selEventsQueue.options[Photo123SWF.selEventsQueue.options.length] = new Option("File Queued: " + 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;
			//Photo123SWF.selEventsQueue.options[Photo123SWF.selEventsQueue.options.length] = new Option("File Queue Error: " + errorString, "");

		} catch (ex) {
			this.debug(ex);
		}
	},
	
	fileDialogComplete : function (numFilesSelected, numFilesQueued) {
		try {
			//Photo123SWF.selEventsQueue.options[Photo123SWF.selEventsQueue.options.length] = new Option("File Dialog Complete: " + numFilesSelected + ", " + numFilesQueued, "");
		} catch (ex) {
			this.debug(ex);
		}
	},
	
	uploadStart : function (file) {
		try {
 			var amountEl = $("#upload_amount");
 			var productEl = $("#upload_product");
			this.addFileParam(file.id, $(amountEl).attr("name"), $(amountEl).attr("value"));
			this.addFileParam(file.id, $(productEl).attr("name"), $(productEl).attr("value"));
			this.addFileParam(file.id, "upload_session_id", generateRandomId(10));
			//Photo123SWF.selEventsFile.options[Photo123SWF.selEventsFile.options.length] = new Option("File Start: " + file.id, "");
		} catch (ex) {
			this.debug(ex);
		}

		return true;
	},

	uploadProgress : function (file, bytesLoaded, totalBytes) {

		try {
			var percent = Math.ceil((bytesLoaded / file.size) * 100);
			if (percent < 10) {
				percent = "  " + percent;
			} else if (percent < 100) {
				percent = " " + percent;
			}

			Photo123SWF.selQueue.value = file.id;
			var queueString = percent + "%:" + file.name;
			Photo123SWF.selQueue.options[Photo123SWF.selQueue.selectedIndex].text = queueString;
			
			//Photo123SWF.selEventsFile.options[Photo123SWF.selEventsFile.options.length] = new Option("Upload Progress: " + bytesLoaded, "");
		} catch (ex) {
			this.debug(ex);
		}
	},

	uploadSuccess : function (file, serverData, response) {
		try {

			var queueString = "100%:" + file.name;
			Photo123SWF.selQueue.value = file.id;
			Photo123SWF.selQueue.options[Photo123SWF.selQueue.selectedIndex].text = queueString;

			if(serverData) {
				
				var jsonObj = eval('(' + serverData + ')');
				
				var msgErrDiv = document.createElement('div');
				$(msgErrDiv).attr("className", "msg-error");
				$(msgErrDiv).attr("id", file.id + "_msg");
				$(msgErrDiv).css("margin-bottom", 0);
				$(msgErrDiv).hide();
				
				msgTxt = document.createTextNode(jsonObj.filename + ": " + jsonObj.message);
				$(msgErrDiv).append(msgTxt);

				$("#upload_messages").append(msgErrDiv);
				
				$(msgErrDiv).show("slow");
				
				window.setTimeout(function() {
					
					$("#" + file.id + "_msg").hide("slow");
					
					window.setTimeout(function() {
						$("#" + file.id + "_msg").remove();
					}, 2000);
				}, 3000);
			}
		} catch (ex) {
			this.debug(ex);
		}
	},

	uploadError : function (file, errorCode, message) {

		try {
			var errorName = "";
			switch (errorCode) {
			case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
				Photo123SWF.selQueue.options[Photo123SWF.selQueue.selectedIndex].text = "HTTP:" + file.name;
				errorName = "HTTP ERROR";
				break;
			case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
				errorName = "MISSING UPLOAD URL";
				break;
			case SWFUpload.UPLOAD_ERROR.IO_ERROR:
				Photo123SWF.selQueue.options[Photo123SWF.selQueue.selectedIndex].text = "IO  :" + file.name;
				errorName = "IO ERROR";
				break;
			case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
				Photo123SWF.selQueue.options[Photo123SWF.selQueue.selectedIndex].text = "SEC :" + file.name;
				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";

				if (file.post && file.post.upload_session_id) {
					// send a logging request to indicate that it was not a exceptional state
					$.post(
					    'log_cancel.do', 
					    { 
							upload_session_id:file.post.upload_session_id
					    }
					)
				}

				Photo123SWF.selQueue.value = file.id;
				Photo123SWF.selQueue.options[Photo123SWF.selQueue.selectedIndex].text = "----:" + file.name;

				//Photo123SWF.selEventsFile.options[Photo123SWF.selEventsFile.options.length] = new Option("File Cancelled " + file.id, "");
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
				errorName = "FILE STOPPED";
				
				Photo123SWF.selQueue.value = file.id;
				Photo123SWF.selQueue.options[Photo123SWF.selQueue.selectedIndex].text = "0%:" + file.name;

				//Photo123SWF.selEventsFile.options[Photo123SWF.selEventsFile.options.length] = new Option("File Stopped " + file.id, "");
				break;
			default:
				errorName = "UNKNOWN";
				break;
			}

			var errorString = errorName + ":File ID: " + (typeof(file) === "object" && file !== null ? file.id : "na") + ":" + message;
			this.debug(errorString);

			//Photo123SWF.selEventsFile.options[Photo123SWF.selEventsFile.options.length] = new Option(errorString, "");

		} catch (ex) {
			this.debug(ex);
		}
	},
	
	uploadComplete : function (file) {
		try {
			//Photo123SWF.selEventsFile.options[Photo123SWF.selEventsFile.options.length] = new Option("Upload Complete: " + file.id, "");
			
			var percentDone = 0;
			if (Photo123SWF.selQueue.options.length != 0) {
				percentDone = new Number(((Photo123SWF.loadCnt+1)/Photo123SWF.selQueue.options.length)*100).toFixed(1);
			}

	        if(percentDone>=1) {
	        	Photo123SWF.rowProgress.width = percentDone + "%";
				Photo123SWF.lable.innerHTML = percentDone + "%";
			} else {
	        	Photo123SWF.rowProgress.width = "1%";
			}
			
			Photo123SWF.loadCnt += 1;
			
			if (Photo123SWF.loadCnt >= Photo123SWF.selQueue.options.length) {
				
				var millis = 1500;
				var date = new Date();
				var curDate = null;

				do { curDate = new Date(); }
				while(curDate-date < millis);

				// redirectUrl is a global variable
				$("#photoForm").attr("action", redirectUrl);
				Photo123SWF.submitForm('photoForm');
			} else {
				var file_id = Photo123SWF.selQueue.options[Photo123SWF.loadCnt].value;
				Photo123SWF.SU.startUpload(file_id);
			}
		} catch (ex) {
			this.debug(ex);
		}
	},
	
	// 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) {
		}
	}
};

function generateRandomId(lengthVal) {

	var randomnumber = '';
	for(var i = 0; i < lengthVal; i++) {
		randomnumber += Math.floor(Math.random() * 11);
	}
	
	return randomnumber;
}