// File upload object
//
var swfu;

// Associate arrays indicating files uploaded or being uploaded
//
var nationFiles = new Array();
var nationFileIDs = new Array();


// Document ready hook
//
$(document).ready(function() {
	// Initialize the file upload object
	//
	swfu = new SWFUpload({
		upload_url : "/vb/nation_upload.php", // relative to the swf file
		flash_url : "/vb/flash/swfupload_f9.swf",
		post_params: {
			"sessionhash" : $.cookie("sessionhash"),
			"bbuserid" : $.cookie("bbuserid"),
			"bbpassword" : $.cookie("bbpassword")
		},
		file_size_limit : "10 MB",
		file_types : "*.jpg;*.gif;*.bmp;*.png",
		file_queued_handler : nationFileQueued,
		file_dialog_complete_handler : nationFileDialogComplete,
		upload_progress_handler : nationUploadProgress,
		upload_success_handler : nationUploadSuccess,
		upload_error_handler : nationUploadError,
		debug : false
	});
	
	// Hook the click event for upload objects
	//
	$(".nation_browse").click(function() {
		swfu.selectFiles();
	});
	
	// Hook the mouse over event for the upload items
	//
	$(".nation_upload_item").mouseover(function() {
		nationDisplayUploadPreview(this);
	});
});


function nationUploadError(file, errorCode, message) {
	try {
		
	} catch (ex) {
		this.debug(ex);
	}
}


function nationDisplayUploadPreview(item) {
	
}


// Start the upload immediately after the dialog closes
//
function nationFileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		this.addPostParam("cid", $("#nation_category").val());
		this.startUpload();
	} catch (ex) {
		this.debug(ex);
	}
}

// Show the pending upload
//
function nationFileQueued(file) {
	try {
		
	} catch (ex) {
		this.debug(ex);
	}
}

function nationUploadProgress(file, bytesLoaded, bytesTotal) {
	try {
		var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
		var progress = new NationUploadProgress(file, this.customSettings.progressTarget);
		progress.setProgress(percent);
		progress.setStatus("Uploading...");
	} catch (ex) {
		this.debug(ex);
	}
}

function nationUploadSuccess(file, xml) {
	try {
		// Determine if any errors occurred
		//
		if ($(xml).find("error").length > 0) {
			alert($(xml).find("error").text());
			$("#" + file.id).addClass("nation_upload_error");
			$("#" + file.id + " .nation_upload_item_thumbnail").empty();
			this.cancelQueue();
		}else{
			var image_id = $(xml).find("image_id").text();
			var thumbnail = $(xml).find("thumbnail").text();
			$("#" + file.id + " .nation_upload_item_thumbnail img").attr("src", thumbnail);
			$("#" + file.id + " .nation_upload_item_insert").show();
		
			// Hook the add to post click event
			//
			$("#" + file.id + " .nation_upload_item_insert a").click(function() {
				$("#vBulletin_editor textarea").insertAtCaret("[nation=" + image_id + "]");
				return false;
			});
		}
	} catch (ex) {
		this.debug(ex);
	}
}

/*
var settings = {
15 flash_url : "../swfupload/swfupload_f9.swf",
16 upload_url: "../simpledemo/upload.php", // Relative to the SWF file
17 post_params: {"PHPSESSID" : ""},
18 file_size_limit : "100 MB",
19 file_types : "*.*",
20 file_types_description : "All Files",
21 file_upload_limit : 100,
22 file_queue_limit : 0,
23 custom_settings : {
24 progressTarget : "fsUploadProgress",
25 cancelButtonId : "btnCancel"
26 },
27 debug: false,
28
29 // The event handler functions are defined in handlers.js
30 file_queued_handler : fileQueued,
31 file_queue_error_handler : fileQueueError,
32 file_dialog_complete_handler : fileDialogComplete,
33 upload_start_handler : uploadStart,
34 upload_progress_handler : uploadProgress,
35 upload_error_handler : uploadError,
36 upload_success_handler : uploadSuccess,
37 upload_complete_handler : uploadComplete,
38 queue_complete_handler : queueComplete // Queue plugin event
39 };
40
41 swfu = new SWFUpload(settings);
*/