var minWidth = 250;
var minHeight = 500;

$(document).ready(function(){

	// Resize to size of photo
	if ($("img.photo").length) {

		if (parseInt($("img.photo").attr("width")) > minWidth) minWidth = parseInt($("img.photo").attr("width")) + 167;
		if (parseInt($("img.photo").attr("height")) > 40) minHeight = parseInt($("img.photo").attr("height")) + 400;

		window.resizeTo(minWidth);

	}

	// When completed, sumbit photo data to parent window
	if ($(".complete").length) {

		// If the photo exists, change the data
		if (window.opener.$("#photo" + photoID).length) {
			// Change image
			window.opener.$("#photo" + photoID).attr("src", photoURL).attr("alt", photoCaption);
		}
		// Otherwise pop in front
		else {

			// Create list if none
			if (!window.opener.$(".photos ul").length) window.opener.$(".photos").append("<ul></ul>");

			// Create itemstring
			itemString = "<li id=\"photo" + photoID + "\"><a onclick=\"return openUploadWindow(this, 'photo');\" class=\"edit\" title=\"Edit this photo\" href=\"/events/photos/edit/" + photoID + "/\"><img src=\"" + photoURL + "\" width=\"120\" height=\"97\" alt=\"" + photoCaption + "\" /></a><a class=\"delete\" href=\"/events/photos/delete/" + photoID + "/" + itemSecret + "/\" title=\"Delete this photo\" onclick=\"return deleteItem(this);\">Delete Photo</a>";

			// Pop in front
			if (itemType == "event") {
				window.opener.$(".photos.event ul").prepend(itemString);
			}
			else {
				window.opener.$(".photos.report ul").prepend(itemString);
			}

		}

		// Close window
		self.close();

	}

	// On form submit, disable submit button
	$("form").submit(function() {

		$("#photo_submit").before("<input type=\"hidden\" name=\"photo_submit\" value=\"clicked\" />");
		$("#photo_submit").attr("disabled", "disabled");
		$("#photo_submit").attr("value", "Uploading... hold on.");

	});

});