// handles context menus for image contributions
// toggles the context box and if not present creates it and loads in data
document.observe("dom:loaded", function() {
	$$(".photos").invoke("observe", "click", function(event) {
		var imgBox = this.parentNode;
		
		if(!$$("#" + imgBox.id + " .contextMenu")[0]) {
			var contextMenu = document.createElement("div");
			contextMenu.className = "contextMenu";
			$(imgBox).insert({top: contextMenu});
			new Ajax.Request("imageContextBox.php?snapshot_id=" + imgBox.id.split("image")[1], {

				method: "post",
				onSuccess: function(ajax) {
					contextMenu.innerHTML = ajax.responseText;
					
					// add on click handler for the close Button
					$$("#" + imgBox.id + " .closeButton").invoke("observe", "click", function(event) {
						this.parentNode.toggle();
					});
				},
				onFailure: function(ajax) {
					contextMenu.innerHTML = "There was a problem loading the data. Try again.";
				}
			});
		} else {
			$$("#" + imgBox.id + " .contextMenu")[0].toggle();
		}
	});
});
