(function ($){
	$(function () {
		var toggleDeleteButton = function() {
			var deleteButton = $("#userphoto-delete-button");
			if ($(".userphoto-selected").is(".userphoto-uploaded")) {
				deleteButton.attr("disabled", false);
			} else {
				deleteButton.attr("disabled", true);
			}
		}
		var toggleSelected = function (e) {
			$(".userphoto-selected").removeClass("userphoto-selected");
			$(e.target).addClass("userphoto-selected");

            /* Just in case. The fact the image is inside a label should do this for us. */
            $(e.target).parent().prev().click();

            toggleDeleteButton();
		}

		$(".userphoto").click(toggleSelected);

		toggleDeleteButton();
    });
})(jQuery);