var isFaceboxLoaded = false;

function enableUploadButton(customSettings, state){
		if(state){
				$(customSettings.uploadButton).removeClass("disable")
																			.bind("click", function(){
						upload();
				}).parent().css("width", "205px");
		} else {
				$(customSettings.uploadButton).addClass("disable")
																			.unbind("click")
																			.parent()
																			.css("width", "100px");
		}
}

function calculateNumItemsAndTotalSize(customSettings){
		var itemsTarget = $(customSettings.itemsTarget);
		var numItems = itemsTarget.find("div.queueitem").not(".error").not(".removed").length;
		
		if(numItems > 0){
				enableUploadButton(customSettings, true);
			
				if(numItems == 1){
						etiqueta = customSettings.etiqueta1017;
				} else {
						etiqueta = customSettings.etiqueta1016;
				}
		} else {
				enableUploadButton(customSettings, false);

				etiqueta = customSettings.etiqueta1016;
		}

		etiqueta = etiqueta.replace("[#num]", numItems);
	
		$(customSettings.numFilesTarget).html(etiqueta);
		
		var fileSize = 0;
		var tempSize = 0;

		itemsTarget.find("div.queueitem").not(".error").not(".removed").find("div.size").each(function(){
				tempSize = parseFloat($(this).html()
																			.replace("<", "")
																			.replace(">", "")
																			.replace(" ", "")
																			.replace("KB", ""));
																			
				if(!isNaN(tempSize)){
						fileSize += tempSize;
				}
		});

		$(customSettings.fileSizeTarget).html(fileSize + " KB");
		$(customSettings.totalBytesToUpload).attr("value", fileSize * 1000);
}

function swfUploadLoadFailed() {
		$(this.customSettings.numFilesTarget).html(this.customSettings.etiqueta1016.replace("[#num]", 0));
}

function swfUploadLoaded() {
		$(this.customSettings.numFilesTarget).html(this.customSettings.etiqueta1016.replace("[#num]", 0));
}

function fileQueued(file) {
		try {
				$(this.customSettings.globalError).remove();

				var queueItem = $(this.customSettings.itemsTarget).find("div.queueitem").
																													not(".error").
																													not(".removed").
																													find("span.filename:contains('" + file.name + "')");
				
				if(queueItem.length > 0){
						this.cancelUpload(file.id);
				} else {
						var progress = new FileProgress(file);
						progress.addItem(this.customSettings.itemsTarget);
						progress.registerCancel(this);
						
						calculateNumItemsAndTotalSize(this.customSettings);
				}
		} catch (ex) {
				this.debug(ex);
		}
}

function fileQueueError(file, errorCode, message) {
		try {
				$(this.customSettings.globalError).remove();
			
				if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
						var progress = new FileProgress();
						progress.addGlobalError(this.customSettings);
						
						return;
				}
			
				var progress = new FileProgress(file);
				progress.addItem(this.customSettings.itemsTarget);
				
				switch (errorCode) {
						case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:				
								progress.setError("FileSize>");
								
								break;
						case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
								progress.setError("FileSize<");
								
								break;
						case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
								progress.setError();

								break;
						default:
								if (file !== null) {
										progress.setError();
								}

								break;
				}
		} catch (ex) {
        this.debug(ex);
    }
}

function uploadStart(file) {
		try {
				var progress = new FileProgress(file);
				progress.setStart();
		}
		catch (ex) {}
		
		return true;
}

function uploadProgress(file, bytesLoaded, bytesTotal) {
		try {
				var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);

				var progress = new FileProgress(file);
				progress.setProgress(percent);

				var totalBytesLoaded = parseInt($(this.customSettings.totalBytesLoaded).val());
				var totalBytesToUpload = parseInt($(this.customSettings.totalBytesToUpload).val());
				
				var totalPercent = Math.ceil(((totalBytesLoaded + bytesLoaded) / totalBytesToUpload) * 100);
				
				$(this.customSettings.totalProgressBar).show().css("width", "" + totalPercent + "%");
		} catch (ex) {
				this.debug(ex);
		}
}

function uploadSuccess(file, serverData) {
		try {
				var progress = new FileProgress(file);
				progress.setComplete();
				
				var totalBytesLoaded = $(this.customSettings.totalBytesLoaded);
				totalBytesLoaded.attr("value", parseInt(totalBytesLoaded.val()) + file.size);
		} catch (ex) {
				this.debug(ex);
		}
}

function uploadError(file, errorCode, message) {
		try {
				var progress = new FileProgress(file);
				progress.setError();

				switch (errorCode) {
				case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
						this.debug("Error Code: HTTP Error, File name: " + file.name + ", Message: " + message);
						break;
				case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
						this.debug("Error Code: Upload Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
						break;
				case SWFUpload.UPLOAD_ERROR.IO_ERROR:
						this.debug("Error Code: IO Error, File name: " + file.name + ", Message: " + message);
						break;
				case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
						this.debug("Error Code: Security Error, File name: " + file.name + ", Message: " + message);
						break;
				case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
						this.debug("Error Code: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
						break;
				case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
						this.debug("Error Code: File Validation Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
						break;
				case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
						this.debug("Error Code: File Cancelled, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
						break;
				case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
						this.debug("Error Code: Upload Stopped, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
						break;
				default:
						this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
						break;
				}
		} catch (ex) {
        this.debug(ex);
    }
}

function uploadComplete(file) {
		if (this.getStats().files_queued === 0) {
				$(this.customSettings.totalProgressBar).show().css("width", "100%");
				
				var url = this.customSettings.urlcomplete;
				
				jQuery.facebox({ ajax: url});
		}
}