var swfu = undefined;
var image_host = "http://petzibit-images.s3.amazonaws.com";
var image_host_resize = "http://petzibit-resize.s3.amazonaws.com";
var swfuAlbumId;
var swfuFiles;
var swfuView;

function releaseTheSWF(albumId, view) {
	if(!swfu) {
		swfu = new SWFUpload({
			// Backend Settings
			upload_url : "/json/image/create",
			http_success : [ 200 ],
			
			post_params : {},
			
			// File Upload Settings
			file_size_limit : "5 MB",
			file_types : "*.*",
			file_types_description : "All Files",
			file_upload_limit : 0,
			file_queue_limit : 0,
			file_post_name : "file",
			
			// Handlers
			swfupload_preload_handler : preLoad,
			swfupload_load_failed_handler : loadFailed,
			file_queue_error_handler : fileQueueError,
			file_dialog_complete_handler : fileDialogComplete,
			upload_start_handler : uploadStart,
			upload_error_handler : uploadError,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete,
			
			// Button Settings
			button_image_url : "/images/SmallSpyGlassWithTransperancy_17x18.png",
			button_placeholder_id : 'imageSwfButton',
			button_width : 180,
			button_height : 18,
			button_text : '<span class="button">Add Photos <span class="buttonSmall">(5 MB Max)</span></span>',
			button_text_style : '.button {color: #ffffff; font-family: Helvetica, Arial, sans-serif; font-size: 14pt; font-style: bold; text-align: center; } .buttonSmall {color: #ffffff; font-size: 12pt; }',
			button_text_top_padding : 0,
			button_text_left_padding : 18,
			button_window_mode : SWFUpload.WINDOW_MODE.TRANSPARENT,
			button_cursor : SWFUpload.CURSOR.HAND,
			
			// Flash Settings
			flash_url : "/swf/swfupload.swf",
			flash9_url : "/swf/swfupload_fp9.swf",
			
			custom_settings : {
			},
			
			// Debug Settings
			debug : false
		});
	}
	swfuAlbumId = albumId;
	swfuView = view;
}
		
function preLoad() {
	if (!this.support.loading) {
		alert("You need the Flash Player to use SWFUpload.");
		return false;
	} else if (!this.support.imageResize) {
		alert("You need Flash Player 10 to upload resized images.");
		return false;
	}
}

function loadFailed() {
	alert("Something went wrong while loading SWFUpload. If this were a real application we'd clean up and then give you an alternative");
}

function fileQueueError(file, errorCode, message) {
	try {
		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:
			break;
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
		default:
			alert(message);
			break;
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	$('#uploaded-photos-table').show();
	for(var i = 0; i < numFilesSelected; i++) {	
		$('#uploaded-photos-table tbody').append('<tr><td id="swfuprogress_'+i+'" style="text-align: center; vertical-align:middle;"><img src="/images/ajax-loader.gif"/></td><td><textarea rows="6" class="span5"></textarea></td></tr>');
	}
	swfuFiles = {count: 0};
	try {
		if (numFilesQueued > 0) {
			this.startResizedUpload(this.getFile(0).ID, 720, 540, SWFUpload.RESIZE_ENCODING.JPEG, 80);
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadStart(file) {
	swfuFiles[file] = swfuFiles.count;
	swfuFiles.count++;
	this.setPostParams({albumId: swfuAlbumId});
}

function uploadSuccess(file, response) {
	$('#swfuprogress_' + swfuFiles[file]).empty();
	$('#swfuprogress_' + swfuFiles[file]).append('<img src="http://petzibit-resize.s3.amazonaws.com/'+$.parseJSON(response).id+'.jpg" />');
	swfuView.addImage(swfuAlbumId, $.parseJSON(response).id);
}

function uploadComplete(file) {
	try {
		if (this.getStats().files_queued > 0) {
			this.startResizedUpload(this.getFile(0).ID, 720, 540, SWFUpload.RESIZE_ENCODING.JPEG, 80);
		} 
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadError(file, errorCode, message) {
	try {
		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			break;
		default:
			break;
		}
	} catch (ex3) {
		this.debug(ex3);
	}
}

