function previousThumbSet() {
	
	if (page > 1) {
		
		new Effect.Move('thumbs', {

			x: 864,
			y: 0,
			mode: 'relative',
			duration: 0.4,  
			transition: Effect.Transitions.sinoidal,
			queue: 'end',
			limit: 1

		});

		page--;
		
		if (page == 1) {
			
			$('thumbnail_arrow_left').setStyle({visibility:'hidden'});
			
		}
		
		if (totalPages > 1 && page < totalPages) {
			
			$('thumbnail_arrow_right').setStyle({visibility:'visible'});
			
		}
		
	}
	
}

function nextThumbSet() {
	
	if (page < totalPages) {
		
		new Effect.Move('thumbs', {

			x: -864,
			y: 0,
			mode: 'relative',
			duration: 0.4,  
			transition: Effect.Transitions.sinoidal,
			queue: 'end',
			limit: 1

		});
		
		page++;
		
		if (page == totalPages) {
			
			$('thumbnail_arrow_right').setStyle({visibility:'hidden'});
			
		}
		
		if (totalPages > 1 && page > 1) {
			
			$('thumbnail_arrow_left').setStyle({visibility:'visible'});
			
		}
		
	}
	
}

function showImage(id, imageId) {
	
	//looks for any divs that have a class of 'image_container_selected', if there are, remove the class and set it to the regular container
	if ($$('div.thumb_image_container_selected').length > 0) {
		
		$('image_' + lastSelection).removeClassName('thumb_image_container_selected');
		$('image_' + lastSelection).addClassName('thumb_image_container');
		
	}
	
	$('image_' + imageId).removeClassName('thumb_image_container');
	$('image_' + imageId).addClassName('thumb_image_container_selected');
	lastSelection = imageId;
	
	new Effect.Opacity('main_content_container', {
		
		duration:1.0,
		from:1.0,
		to:0.0,
		
		afterFinish: function () {
			
			new Ajax.Updater('main_content_container', 'ajaxShowGalleryMainContent.php', {

				parameters: {id: id, imageId: imageId},
				evalScripts: true
				
			});
			
		}
		
	});
	
}

function fadeinMainImage () {
	
	//create the height based on the combined height of all (possible) elements (plus their padding or margins) in the body of the document
	var height = $('main_image').getHeight();
	
	if($('document_title') != null) {
		
		height += $('document_title').getHeight();
		height += 10;
		
	}
	
	if($('main_document_title') != null) {
		
		height += $('main_document_title').getHeight();
		height += 10;
		
	}
	
	if($('document_info_bar') != null) {
		
		height += $('document_info_bar').getHeight();
		height += 10;
		
	}
	
	if($('document_image_caption') != null) {
		
		height += $('document_image_caption').getHeight();
		
	}
	
	if($('document_body') != null) {
		
		height += $('document_body').getHeight();
		height += 10;
		
	}
	
	height += 10; //margin thumbs_container
	
	$('main_content_container').setStyle({'height':height + 'px'});
	
	new Effect.Opacity('main_content_container', {
		
		duration:1.0,
		from:0.0,
		to:1.0
		
	});
	
}

function showCommentsList() {
	
	if (!$('comments_main_container').visible()) {
		
		new Effect.Appear('comments_main_container', {duration:.25, queue: 'end', limit:1});
		$('comment_toggle_button').update('Hide Comments');
		
	} else {
		
		$('comments_main_container').hide();
		$('comment_toggle_button').update('Show Comments');
				
	}
	
}

function regenerateCommentsList(s, d) {
	
	new Ajax.Updater('comments_container', 'ajaxShowCommentsImages.php', {
		
		parameters: {parentId: id, id: imageId, type: 'documentImage', s: s, d: d},
		evalScripts: true
		
	});
	
}

function initEditComment(commentId) {
	
	commentContainer = 'comment_container_' + commentId;
	commentBody = 'comment_' + commentId;
	
	//handle existing editor (if necessary)
	if ($('edit_in_place')) {
		
		//remove all observers
		$('editor_ok').stopObserving();
		$('editor_cancel').stopObserving();

		//remove the edit-in-place dom object
		$('edit_in_place').remove();
		
		if ($(lastComment)) {
			
			$(lastComment).show();
			
		}
		
	}
	
	//hide the current static field
	$(commentContainer).hide();
	
	//create the editable text and ok & cancel buttons
	var showEditor ='<div id="edit_in_place" class="comment_container"><textarea id="comment_edit" rows="8" style="width:776px">' + $(commentBody).innerHTML.replace(/"/g,'&quot;').replace(/\'/g,'&#039;').replace(/<br>/g,'\n') + '</textarea><br><input type="button" id="editor_ok" value="ok"> <input type="button" id="editor_cancel" value="cancel"></div>';
	$(commentContainer).insert({after: showEditor});
	
	//assign the category being edited to a global variable
	lastComment = commentContainer;
	
	//create observers for ok and cancel buttons
	$('editor_ok').observe('click', function() {editComment(commentId);});
	$('editor_cancel').observe('click', function() {cancelEditComment();});	
	
}

function editComment(commentId) {
	
	new Ajax.Request('ajaxUpdateComment.php', {
		
		parameters: {id: commentId, value: $F('comment_edit'), type: 'documentImage'},
		onComplete: function() {
			
			if ($('edit_in_place')) {

				//remove all observers
				$('editor_ok').stopObserving();
				$('editor_cancel').stopObserving();

				//remove the edit-in-place dom object
				$('edit_in_place').remove();
				
				//regenerate the comment list
				$(lastComment).show();

			}
			
		}
		
	});
	
}

function cancelEditComment() {
	
	//remove all observers
	$('editor_ok').stopObserving();
	$('editor_cancel').stopObserving();
	
	//remove the edit-in-place dom object
	$('edit_in_place').remove();
	
	//show the original static category name
	$(lastComment).show();
	
}

function deleteComment(commentId) {
	
	new Ajax.Request('ajaxDeleteComment.php', {
		
		parameters: {id: commentId, type: 'documentImage'},
		onComplete: function() {
			
			regenerateCommentsList(last_s, '');
			
		}
		
	});
	
}

function showAddComment() {
	
	if (!$('add_comment_container').visible()) {
		
		new Effect.Appear('add_comment_container', {duration:.25, queue: 'end', limit:1});
		
		
	} else {
		
		$('add_comment_container').hide();
				
	}
	
}

function addComment() {
	
	//wathes the submit button
	
	$('add_comment').observe('submit', function(e) {
		
		//e.stop prevents ("stops") the submit button from executing 
		e.stop();
		new Ajax.Request('ajaxAddComment.php', {
			
			parameters: $('add_comment').serialize(),
			onComplete: function() {
				
				regenerateCommentsList();
				$('add_comment').reset();
				
			}
			
		});
		
	});
	
}

//show the spinner whenever anything ajax is happening 
Ajax.Responders.register({
	onCreate: function() { $('spinner').show(); },
	onComplete: function() {
			
			if (0 == Ajax.activeRequestCount) {
				
				$('spinner').hide();
								
			}
			
		}
});

//wait until the dom has loaded and then watch the add category form and activate the sortables
document.observe('dom:loaded', function() {
	
	showImage(id, imageId);
	
	if ($('comments_container') != null) {
		
		regenerateCommentsList();
		
	}
	
	if ($('add_comment') != null) {
		
		addComment();
		
	}
	
});
