function fileQueueError(file, errorCode, message) {
	try {
		var imageName = "error.gif";
		var errorName = "";
		if (errorCode === SWFUpload.errorCode_QUEUE_LIMIT_EXCEEDED) {
			errorName = "You have attempted to queue too many files.";
		}

		if (errorName !== "") {
			alert(errorName);
			return;
		}

		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			imageName = "zerobyte.gif";
			break;
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			imageName = "toobig.gif";
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
		default:
			alert(message);
			break;
		}

		alert('fileQueueError');

	} catch (ex) {
		this.debug(ex);
	}
}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesQueued > 0) {
			
			// Переинициализация данных
			swfu.setPostParams({
				'rmt': ($('input[name="rmt"]').attr('checked')) ? 1 : 0,
				'tinyurl': ($('input[name="tinyurl"]').attr('checked')) ? 1 : 0,
				'width': $('input[name="width"]').attr('value')
			});
			
			this.startUpload();
			$('#statusContainer').fadeIn('slow');
		}
	} 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_target);
		progress.setProgress(percent);
		if (percent === 100) {
			progress.setStatus("Создание эскиза...");
			progress.toggleCancel(false, this);
		} else {
			progress.setStatus("Загрузка...");
			progress.toggleCancel(true, this);
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadSuccess(file, serverData) {
		
	try 
	{
		var progress = new FileProgress(file,  this.customSettings.upload_target);
		
		// Разбираем параметры
		var data = eval("(" + serverData + ")");
		
		if (data.error == 0){
			progress.setStatus("Эскиз создан");
			progress.toggleCancel(false);
			if (typeof(data.tinyurl) != 'undefined')
				addThumb(data.image, data.preview, data.thumb, data.tinyurl);
			else
				addThumb(data.image, data.preview, data.thumb);
		} else {
			progress.setStatus("Ошибка закачки изображения");
			progress.toggleCancel(false);
		}
		
		progress.setProgress(0);
		
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadComplete(file) {
	try {
		/*  I want the next upload to continue automatically so I'll call startUpload here */
		if (this.getStats().files_queued > 0) {
			this.startUpload();
		} else {
			var progress = new FileProgress(file,  this.customSettings.upload_target);
			progress.setComplete();
			progress.setStatus("Все изображение загружены");
			progress.toggleCancel(false);
			
			$('#statusContainer').fadeOut('slow');
			
			$('.thumb').hover(
				function(){
					$(this).css('background-color','#79B7E7');
				},
				function(){
					$(this).css('background-color','#fff');
				}
			);
			
			$('.image').live('click', function(){
				
				$(this).css('background-color','#fff');
				$(this).effect('shake', {}, 300);	
				$("#dialog").dialog('open');
				
			});
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadError(file, errorCode, message) {
	var imageName =  "error.gif";
	var progress;
	try {
		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			try {
				progress = new FileProgress(file,  this.customSettings.upload_target);
				progress.setCancelled();
				progress.setStatus("Отмена операции");
				progress.toggleCancel(false);
			}
			catch (ex1) {
				this.debug(ex1);
			}
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			try {
				progress = new FileProgress(file,  this.customSettings.upload_target);
				progress.setCancelled();
				progress.setStatus("Операция остановлена");
				progress.toggleCancel(true);
			}
			catch (ex2) {
				this.debug(ex2);
			}
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			imageName = "uploadlimit.gif";
			break;
		default:
			alert(message);
			break;
		}

	} catch (ex3) {
		this.debug(ex3);
	}
}

function addThumb(image, preview, thumb, tinyurl)
{
	var html = '';
	if (tinyurl != null)
		html = '<div class="image"><a thumb="'+thumb+'" image="'+image+'" preview="'+preview+'" tinyurl="'+tinyurl+'"><img src="'+thumb+'" /></a></div>';
	else
		html = '<div class="image"><a thumb="'+thumb+'" image="'+image+'" preview="'+preview+'"><img src="'+thumb+'" /></a></div>';
	$('.images').append(html);
}

function FileProgress(file, targetID)
{
	this.container = '#divFileProgress';
	this.status = '#divFileProgressStatus';
	
	FileProgress.prototype.setProgress = function (percentage) {	
		$(this.container).progressbar('option', 'value', percentage);
	};
	
	FileProgress.prototype.setComplete = function () {
		//alert('FileProgress complete');
	};
	
	FileProgress.prototype.setError = function () {
		//alert('FileProgress error');
	};
	
	FileProgress.prototype.setCancelled = function () {
		//alert('FileProgress cancel');
	};
	
	FileProgress.prototype.setStatus = function (status) {
		$(this.status).html(status);
	};
	
	FileProgress.prototype.toggleCancel = function (show, swfuploadInstance) {
		//alert('FileProgress toggle cancel');
	};
}