
window.addEvent('domready', function() {
	
	// gallery list functionality
	if($('quick_jump_select')) {
		$('quick_jump_select').addEvent('change', function() { $('quick_jump').submit(); });
	}
	
	// gallery item - comments deletion - bind to all comment delete links
	$(document.body).getElements('.moderate .delete').addEvent('click', (function(obj_event) {
		
		var elm_target = $(obj_event.target);
		
		// check for already-sending state
		if(elm_target.hasClass('active')) {
			return;
		}
		
		// retrieve parent comment element to extract the id
		var elm_comment = elm_target.getParent('.comment');
		
		if(elm_comment) {
			
			var int_comment_id = elm_comment.get('id').replace(/comment-/, '');
			
			// fade out element to indicate we're about to delete it
			elm_comment.setStyle('opacity', 0.4);
			
			if(confirm('Are you sure you wish to remove this comment?')) {
				
				// flag as active to prevent duplicate requests
				elm_target.addClass('active');
				
				var obj_json = new Request.JSON({
					url: '/gallery/item-comment-delete',
					onSuccess: function(response) {
						
						if(response.error == false) {
							
							// remove comment element
							elm_comment.dissolve();
							
							// update total comments count
							var elm_num_comments = $(document.body).getElement('h2.num_comments');
							var elm_num = elm_num_comments.getElement('span');
							var int_num = elm_num.get('html') - 1;
							
							// check all comments haven't been removed - wipe the heading if so
							if(int_num < 1) {
								elm_num_comments.dissolve();
							}
							else {
								elm_num.set('html', int_num);
							}
						}
						else {
							
							// something went wrong - revert the comment back to normal
							elm_comment.setStyle('opacity', 1);
							elm_target.removeClass('active');
							elm_target.set({
								src: '/images/icons/delete.png',
								width: 16,
								height: 16
							});
							
							// show the user the error
							alert(response.message);
						}
					}
				});
				
				elm_target.set({
					src: '/images/common/loading.gif',
					width: 16,
					height: 11
				});
	
				obj_json.post({
					'comment_id': int_comment_id
				})
			}
			else {
				
				// cancelled delete - show comment element again
				elm_comment.setStyle('opacity', 1);
			}
		}
		
	}).bindWithEvent());
});
