var uploadStatus = true;
function swfUploadLoaded() {
	//FeaturesDemo.start(this);  // This refers to the SWFObject because SWFUpload calls this with .apply(this).
	$(this.customSettings.close_button).hide();//disable close button
	$(this.customSettings.submit_button).hide();//disable close button
}
	
function fileQueueError(file, errorCode, message) {
	try {
		switch (errorCode) {
			case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
				faShowError(this.customSettings.message_target, 'выбрано слишком много файлов');
				break;
			case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
				faShowError(this.customSettings.message_target, 'размер файла: <i>'+file.name+'</i> превышает допустимый предел <i>'+file.size+'</i> байт');
				break;
			case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
				faShowError(this.customSettings.message_target, 'размер файла: <i>'+file.name+'</i> составляет 0 байт, файл не может быть передан');
				break;
			case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
				faShowError(this.customSettings.message_target, 'тип файла: <i>'+file.name+'</i> является недопустимым');
				break;
			default:
				faShowError(this.customSettings.message_target, message);
				break;
		};
	} catch (ex) {
		this.debug(ex);
	}
}

function fileDialogStart(){
	faHideError(this.customSettings.message_target);
	faHideHighlight(this.customSettings.message_target);
	faShowHighlight(this.customSettings.message_target, 'выберите файл который хотите загрузить на сервер');
};

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesQueued > 0) {
			this.startUpload();
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadStart(file) {
	try {
		$(this.customSettings.cancel_button).attr('disabled', true);//disable close button
		faHideHighlight(this.customSettings.message_target);
		faShowHighlight(this.customSettings.message_target, '<i>'+file.name+'</i>: начата передача на сервер');
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadProgress(file, bytesLoaded) {
	try {
		var percent = Math.ceil((bytesLoaded / file.size) * 100);
		var progress = new FileProgress(file,  this.customSettings.upload_progress);
		progress.setProgress(percent);
		//faShowError(this.customSettings.message_target, percent, false);
		if (percent === 100) {
			progress.toggleCancel(false, this);
			faShowHighlight(this.customSettings.message_target, '<i>'+file.name+'</i>: успешно получен');
			faShowHighlight(this.customSettings.message_target, '<i>'+file.name+'</i>: идет обработка');
			faShowHighlight(this.customSettings.message_target, '<b>пожалуйста подождите...</b>');
		} else {
			progress.toggleCancel(true, this);
			$('.sfw-upload .toggleCancel').show();
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadSuccess(file, serverData) {
	try {
		var progress = new FileProgress(file,  this.customSettings.upload_progress);
		if (serverData.substring(0, 8) === "success:") {
			progress.toggleCancel(false);
			faShowHighlight(this.customSettings.message_target, '<i>'+file.name+'</i>: успешно обработан');
		} else {
			progress.toggleCancel(false);
			faShowError(this.customSettings.message_target, file.name, false);
			faShowError(this.customSettings.message_target, serverData, false);
			uploadStatus = false;
		}
	} catch (ex) {
		this.debug(ex);
	}
}
	
function uploadComplete(file) {
	try {
		if (this.getStats().files_queued > 0) {// I want the next upload to continue automatically so I'll call startUpload here
			this.startUpload();
		} else {
			var progress = new FileProgress(file,  this.customSettings.upload_progress);
			progress.setComplete();
			progress.toggleCancel(false);
			//$(this.customSettings.close_button).show();//вкл close button
			$(this.customSettings.cancel_button).attr('disabled', false);//вкл close button
			if (uploadStatus) {//if no errors
				faShowNotify('передача файлов завершена', false);
				$(this.customSettings.message_target).dialog('close');//trigger close
			};
		}
	} catch (ex) {
		this.debug(ex);
	}
}

//to do
function uploadError(file, errorCode, message) {
	var progress;
	try {
		uploadStatus = false;
		switch (errorCode) {
			case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
				try {
					progress = new FileProgress(file,  this.customSettings.upload_progress);
					progress.setCancelled();
					progress.toggleCancel(false);
					faShowError(this.customSettings.message_target, 'cancelled');
				}
				catch (ex1) {
					this.debug(ex1);
				}
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
				try {
					progress = new FileProgress(file,  this.customSettings.upload_progress);
					progress.setCancelled();
					progress.toggleCancel(true);
					faShowError(this.customSettings.message_target, 'stopped');
				}
				catch (ex2) {
					this.debug(ex2);
				}
			case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
				try {
					progress = new FileProgress(file,  this.customSettings.upload_progress);
					progress.setCancelled();
					progress.toggleCancel(false);
					faShowError(this.customSettings.message_target, 'upload limit exceeded');
				}
				catch (ex1) {
					this.debug(ex1);
				}
				faShowError(this.customSettings.message_target, UPLOAD_LIMIT_EXCEEDED);
				break;
			default:
				try {
					progress = new FileProgress(file,  this.customSettings.upload_progress);
					progress.setCancelled();
					progress.toggleCancel(false);
					faShowError(this.customSettings.message_target, 'server error:' + message);
				}
				catch (ex1) {
					this.debug(ex1);
				}
				break;
		}
	} catch (ex3) {
		this.debug(ex3);
	}
}

/* ******************************************
 *	FileProgress Object
 *	Control object for displaying file info
 * ****************************************** */

function FileProgress(file, targetID) {
	this.fileProgressID = "divFileProgress";
	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.fileProgressID;
		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer";
		var progressCancel = document.createElement("a");
		progressCancel.className = "progressCancel";
		progressCancel.href = "#";
		progressCancel.style.visibility = "hidden";
		progressCancel.appendChild(document.createTextNode(" "));
		var progressText = document.createElement("div");
		progressText.className = "progressName";
		progressText.appendChild(document.createTextNode(file.name));
		var progressBar = document.createElement("div");
		progressBar.className = "progressBarInProgress";
		var progressStatus = document.createElement("div");
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "&nbsp;";
		this.fileProgressElement.appendChild(progressCancel);
		this.fileProgressElement.appendChild(progressText);
		this.fileProgressElement.appendChild(progressStatus);
		this.fileProgressElement.appendChild(progressBar);
		this.fileProgressWrapper.appendChild(this.fileProgressElement);
		document.getElementById(targetID).appendChild(this.fileProgressWrapper);
		fadeIn(this.fileProgressWrapper, 0);
	} else {
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
		this.fileProgressElement.childNodes[1].firstChild.nodeValue = file.name;
	}
	this.height = this.fileProgressWrapper.offsetHeight;
}
FileProgress.prototype.setProgress = function (percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[3].style.width = percentage + "%";
};
FileProgress.prototype.setComplete = function () {
	this.fileProgressElement.className = "progressContainer blue";
	this.fileProgressElement.childNodes[3].className = "progressBarComplete";
	this.fileProgressElement.childNodes[3].style.width = "";

};
FileProgress.prototype.setError = function () {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";

};
FileProgress.prototype.setCancelled = function () {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";
};
FileProgress.prototype.setStatus = function (status) {
	this.fileProgressElement.childNodes[2].innerHTML = status;
};
FileProgress.prototype.toggleCancel = function (show, swfuploadInstance) {
	this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
	if (swfuploadInstance) {
		var fileID = this.fileProgressID;
		this.fileProgressElement.childNodes[0].onclick = function () {
			swfuploadInstance.cancelUpload(fileID);
			return false;
		};
	}
};
