function regenerateCalendar(month, year) {
	
	new Ajax.Updater('calendar_container', 'ajaxShowEventCalendarShowCalendar.php', {
		
		parameters: {getMonth: month, getYear: year},
		onComplete: function () {
			
			var height = $('calendar_container').getHeight(); // - 12 subtract 2px for calendar border and 10 px for 5px padding on top and bottom of event_list
			height = height + "px";

			$('event_list').setStyle({

			  height: height

			});
			
		}
		
	});
	
}

function regenerateEventList(month, day, year, s, d) {
	
	new Ajax.Updater('event_list', 'ajaxShowEventCalendarEventList.php', {
		
		parameters: {month: month, day: day, year: year, s: s, d: d}
		
	});
	
}

function loadEventDetails(id) {
	
	new Ajax.Updater('event_details', 'ajaxShowEvent.php', {
		
		parameters: {id: id},
		evalScripts: true
		
	});
	
}

function toggleTopNavigation() {
	
	if (!$('event_top_navigation_container').visible()) {
		
		//unable to get dimesnions during effect transition, so we're setting visibility to hidden, using show() to render the element, getting its dimensions, setting the height, using hide() to hide it, setting visibility to visible, then running the effect transition
				
		$('event_list').setStyle({

		  visibility: 'hidden'

		});
		
		$('event_top_navigation_container').show();
		
		var height = $('calendar_container').getHeight();
		height = height + "px";

		$('event_list').setStyle({

		  height: height

		});
		
		$('event_top_navigation_container').hide();
		
		$('event_list').setStyle({

		  visibility: 'visible'

		});
		
		new Effect.Appear('event_top_navigation_container', {duration:.25, queue: 'end', limit:1});
		$('top_navigation_toggle').update('Hide Calendar');
		
		
		
	} else {
		
		$('event_top_navigation_container').hide();
		$('top_navigation_toggle').update('Show Calendar');
				
	}
	
}

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', 'ajaxShowComments.php', {
		
		parameters: {id: id, type: 'event', 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="10" style="width:918px">' + $(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: 'event'},
		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: 'event'},
		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();
				
			}
			
		});
		
	});
	
}

document.observe('dom:loaded', function() {
	
	if (!openId.empty()) {

		loadEventDetails(openId);

	}
	
	regenerateCalendar(month, year);
	regenerateEventList(month, day, year, '', '');
	
});

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