function show_images_attached_upload()
{
	$("images-attached-upload").style.display = "block";
	$("images-attached-none-yet").style.display = "none";
	$("images-attached-many-yet").style.display = "none";

	window.setTimeout(init_images_attached_upload, 0);
	fix_height_reflow();
}

function hide_images_attached_upload()
{
	$("images-attached-upload").style.display = "none";

	var none_yet = false;
	if ($("images-attached-many-holder").innerHTML.replace(/\s/g, '').length == 0)
		none_yet = true;

	$("images-attached-none-yet").style.display = "block";
	$("images-attached-many-yet").style.display = none_yet ? "none" : "block";

	fix_height_reflow();
}

var upload_images_attached_filename = null;
var upload_images_attached_ready = false;
function init_images_attached_upload()
{
	var fo = new FlashObject(static_url + "/uploads.swf?v1", "images_attached_uploads_flash", "108", "27", "9.0.28", "#FFFFFF");
	fo.addParam("allowScriptAccess", "always");
	fo.addVariable("buttonType", "choose");

	if (typeof(images_attached_upload_place) == "undefined")
		images_attached_upload_place = "uploads-send-file-box";

	if (!$(images_attached_upload_place))
	{
		var div = document.createElement("div");
		div.id = images_attached_upload_place;
		document.body.appendChild(div);
	}

	if (!fo.write(images_attached_upload_place))
	{
		init_image_attached_upload_nonflash();
		return;
	}

	if ($("uploads-send-file-box"))
	{
		$("uploads-send-file-box").style.display = "none";
		$("uploads-send-file-box").style.height = "0";
	}

	var setup_tries = 0;
	function setup()
	{
		var u = $("images_attached_uploads_flash");
		try
		{
			u.setUploadURL(images_attached_upload_hook + "?" + uploads_pick_session_querystring);
			u.addFileType("Images and Photos", "*.png;*.jpg;*.jpeg;*.gif");
		}
		catch (e)
		{
			setup_tries++;
			if (setup_tries < 5)
				window.setTimeout(setup, 100);
		}
	}
	window.setTimeout(setup, 100);

	onUploadProgress(0, 1);
}

function init_image_attached_upload_nonflash()
{
	var iframe = document.createElement("iframe");
	iframe.frameBorder = "0";
	iframe.width = "360";
	iframe.height = "27";
	iframe.src = images_attached_upload_hook + "?" + uploads_pick_session_querystring + "&iframe=1";

	window.onUploadLoaded = function ()
	{
		var form = iframe.contentDocument.forms.upload_form;
		form.action = images_attached_upload_hook + "?" + uploads_pick_session_querystring + "&iframe=1";
	};

	$(images_attached_upload_place).innerHTML = "";
	$(images_attached_upload_place).appendChild(iframe);

	if ($("images-attached-progress"))
		$("images-attached-progress").style.display = "none";
}

function images_attached_go()
{
	var u = $("images_attached_uploads_flash");
	u.browse();
}
	
function onUploadComplete()
{
}

function onUploadError(msg)
{
	alert("An error occured while uploading:\n\n" + msg);
}

function onUploadStart()
{
}

var onUploadProgress_last = 0;
function onUploadProgress(bytes, total)
{
	var prog = $("images_attached_progress_flash");

	if (!prog && $("images-attached-progress") || 100 * (bytes / total) < onUploadProgress_last)
	{
		function finish()
		{
			onUploadProgress(bytes, total);
		}

		var fo = new FlashObject(static_url + "/progress-bar.swf?v1", "images_attached_progress_flash", "185", "16", "9.0.28", "#FFFFFF");
		fo.addParam("allowScriptAccess", "always");
		fo.write("images-attached-progress");

		onUploadProgress_last = 0;
		setTimeout(finish, 0);
	}
	else
	{
		onUploadProgress_last = 100 * (bytes / total);
		if (prog && prog.setProgress)
			prog.setProgress(onUploadProgress_last);
	}
}

function onUploadSelect(name, size)
{
	if (size > uploads_max_filesize)
		alert("The file you have selected is too large to upload.");
	else
	{
		upload_images_attached_filename = name + "";
		upload_images_attached_ready = false;

		if ($("images-attached-filename"))
			$("images-attached-filename").innerHTML = "uploading " + html_escape(upload_images_attached_filename) + "...";

		var u = $("images_attached_uploads_flash");
		if (u)
			u.upload();
	}
}

function onUploadResponse(data)
{
	if (data != "1")
		alert("There was a problem uploading your image.");
	else
	{
		if ($("images-attached-filename"))
			$("images-attached-filename").innerHTML = "selected " + html_escape(upload_images_attached_filename);

		if ($("images-attached-desc").value == "")
			$("images-attached-desc").value = upload_images_attached_filename.substr(0, upload_images_attached_filename.lastIndexOf(".")).replace("_", " ");

		onUploadProgress(100, 100);
		upload_images_attached_ready = true;
	}
}

function save_images_attached_upload()
{
	if (upload_images_attached_filename == null)
	{
		alert("Please upload an image first.");
		return;
	}

	if (upload_images_attached_ready == false)
	{
		alert("Please wait while your image uploads.");
		return;
	}

	http_post(images_attached_upload_hook, "description=" + escape($("images-attached-desc").value), images_attached_show_next_image);

	upload_images_attached_filename = null;
	if ($("images-attached-filename"))
		$("images-attached-filename").innerHTML = "no file selected";
	$("images-attached-desc").value = "";
}

function images_attached_show_next_image(request)
{
	$("images-attached-many-holder").innerHTML += request.responseText;

	hide_images_attached_upload();
	site_first_load();
	fix_height_reflow();
}

function images_attached_remove(id)
{
	function finish()
	{
		$("images-attached-many-holder").removeChild($("images-attached-specific-" + id));
		hide_images_attached_upload();
	}

	http_post(images_attached_upload_hook, "trash=" + escape(id), finish);
}

// !!! Not sure at all why this is needed, and not just images_attached_remove().
function journal_remove(id)
{
	function finish()
	{
		$("images-attached-many-holder").removeChild($("images-attached-specific-" + id));
		hide_images_attached_upload();
	}

	http_post(images_attached_upload_hook, "delete=" + escape(id), finish);
}
