var spotUpload = {
	progress: null,
	count: 18,
	busy: false,
	refreshImages: function() {
		jQuery.get(document.location.href + '?rnd=' + Math.random(), {'type':1296, 'no_cache':1}, function(data) {
			jQuery('.tx-spot-upload-controller .images form').replaceWith(jQuery('.images form', data));
			jQuery.attachDeleteEvents();
			jQuery.checkMaxFiles();
			jQuery.updateProgress(true);
			jQuery.stopLoading();
		});
	},
	complete: function(file, serverData) {
		if (this.getStats().files_queued === 0) {
			spotUpload.busy = false;
			spotUpload.refreshImages();
			document.getElementById(this.customSettings.cancelButtonId).disabled = true;
			jQuery('.tx-spot-upload-controller .messages span').text('Bezig met aanmaken thumbnails');
		} else {
			jQuery('.tx-spot-upload-controller .messages span').text('Bezig met uploaden: ' + (this.getStats().files_queued + 1) + ' resterend');
		}
	},
	loaded: function() {
		jQuery('body').addClass('has-flash');
	},
	loadFailed: function() {
		// Probably wrong player version
		jQuery('.tx-spot-upload-controller .update-flash').show();
	},
	success: function(file, serverData) {
		
	},
	error: function(file, errorCode, message) {
		try {
			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:
					// If there aren't any files left (they were all cancelled) disable the cancel button
					if (this.getStats().files_queued === 0) {
						document.getElementById(this.customSettings.cancelButtonId).disabled = true;
					}
					break;
				case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
					break;
				default:
					this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
					break;
			}
		} catch (ex) {
			this.debug(ex);
		}
	},
	progress: function(file, bytesLoaded, bytesTotal) {
		try {
			var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
		} catch (ex) {
			this.debug(ex);
		}
	},
	start: function(file) {
		jQuery.startLoading();
		try {

		} catch (ex) {}
		return true;
	},
	queueComplete: function(numFilesUploaded) {
		
	},
	fileDialogComplete: function(numFilesSelected, numFilesQueued) {
		try {
			if (numFilesSelected > 0) {
				document.getElementById(this.customSettings.cancelButtonId).disabled = false;
			}
			/* I want auto start the upload and I can do that here */
			this.startUpload();
		} catch (ex)  {
			this.debug(ex);
		}
	},
	fileQueueError: function(file, errorCode, message) {
		try {
			if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
				alert("You have attempted to queue too many files.\n" + (message === 0 ? "You have reached the upload limit." : "You may select " + (message > 1 ? "up to " + message + " files." : "one file.")));
				return;
			}

			switch (errorCode) {
				case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
					this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
					break;
				case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
					this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
					break;
				case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
					this.debug("Error Code: Invalid File Type, 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);
		}
	},
	fileQueued: function (file) {
		try {
		} catch (ex) {
			this.debug(ex);
		}
	}
};

(function($) {
	$.loading = false;
	$.startLoading = function() {
		if (!$.loading) {
			$('.tx-spot-upload-controller .images').prepend('<div class="loading"></div><div class="messages"><span></span></div>');
			$.loading = true;
		}
	};
	$.stopLoading = function() {
		$('.tx-spot-upload-controller .messages').remove();
		$('.tx-spot-upload-controller .loading').fadeOut();
		$.loading = false;
	};
	$.deleteEvent = function(evt) {
		$('.tx-spot-upload-controller .images form').append('<input type="hidden" name="type" value="1296" />');
		$serializeForm = $('.tx-spot-upload-controller .images form').serialize();
		$('.tx-spot-upload-controller .images input[type=checkbox]').attr('disabled', 'disabled');
		$.startLoading();
		$.post(document.location.href + '?no_cache=1&rnd=' + Math.random(), $serializeForm, function(data) {
			$('.tx-spot-upload-controller .images form').replaceWith($('.images form', data));
			$.attachDeleteEvents();
			$.checkMaxFiles();
			$.updateProgress(true);
			$.stopLoading();
		});
	};
	$.attachDeleteEvents = function() {
		$('.tx-spot-upload-controller .images input[type=checkbox]').attr('disabled', '');
		$('.tx-spot-upload-controller .images form').bind('submit', function(evt) {
			evt.preventDefault();
		});
		$('.tx-spot-upload-controller .images input[type=checkbox]').bind('click', $.deleteEvent);
		if ($.browser.msie) {
			$('.tx-spot-upload-controller .images label').bind('click', function(evt) {
				$(this).unbind('click');
				$('input[type=checkbox]', $(this)).attr('checked', 'checked');
				$('input[type=checkbox]', $(this)).click();
			});
		}
	};
	$.checkMaxFiles = function() {
		var maxUploads = 18;
		var numImages = parseInt(jQuery('.tx-spot-upload-controller .images .image').length);
		if (numImages >= maxUploads) {
			jQuery('.tx-spot-upload-controller .button-upload').css('visibility', 'hidden');
		} else {
			jQuery('.tx-spot-upload-controller .button-upload').css('visibility', 'visible');
		}
	};
	$.updateProgress = function(refresh) {
		var numImages = jQuery('.tx-spot-upload-controller .images .image').length;
		var progressPercentage = parseInt(numImages / spotUpload.count * 100);
		var progressWidth = (numImages / spotUpload.count) * jQuery('.tx-spot-upload-controller .progress').width();
		jQuery('.tx-spot-upload-controller .progress .bar').animate({width: progressWidth});
		jQuery('.tx-spot-upload-controller .progress .percentage .number').text(progressPercentage + ' % (' + numImages + ' van ' + spotUpload.count + ')');
		
		if (refresh) {
			try {
				swfUpload.setFileQueueLimit(maxUploads - numImages);
			} catch(ex) {
				
			}
		}
	},
	$(function() {
		$('body').addClass('has-js');
		$.attachDeleteEvents();
		$.updateProgress(false);
		$.checkMaxFiles();
		$('#tx-spot-upload-controller-next').bind('click', function(evt) {
			if ($('.tx-spot-upload-controller .images .image').length < 18) {
				alert('Er moeten 18 afbeeldingen aanwezig zijn om naar de volgende stap te kunnen gaan')
				evt.preventDefault();
			}
		});
	});
})(jQuery);
