var Messages = {

	// Keeps track of the current folder.
	current_folder:'inbox',
	
	// Changes the current folder being displayed.
	change_folder:function(folder_id) {
		
		// Makes sure the folder_id is defined.
		if(folder_id==undefined) folder_id = 'inbox';
		
		// The Ajax.
		new Ajax.Request('/ajax.php', { method:'get',
			parameters:{ control:'messages', action:'change_folder', folder_id:folder_id, xsrf:xsrf, random_id:Math.floor(Math.random()*10000001) },
		onComplete:function(transport) {
			if(transport.responseText!='') {
				
				// Updates the container that displays the listing of messages by replacing it entirely.
				$('messages_'+Messages.current_folder).replace(transport.responseText);
				
				// If the folder has changed.
				if(Messages.current_folder!=folder_id) {
					
					// Selects the new folder.
					$('folder_'+folder_id).firstDescendant().addClassName('attn on');
					
					// Deselects the old folder.
					$('folder_'+Messages.current_folder).firstDescendant().removeClassName('attn on');
					
					// Updates the variable.
					Messages.current_folder = folder_id;
				}
				
				// Updates the unread counters.
				Messages.update_unread_counter();
			}
		}});
	},
	
	// Executes an action for the specified conversation.
	convo_action:function(action,convo_id) {
		
		// Makes sure the action is set.
		if(action!=undefined) {
		
			// If there's no convo_id specified, just use all the checked checkboxes.
			if(convo_id==undefined || !convo_id || convo_id==0) convo_id = Container.get_checked('messages_'+Messages.current_folder).join(',');
			
			// The Ajax.
			new Ajax.Request('/ajax.php', { method:'get',
				parameters:{ control:'messages', action:action, convo_id:convo_id, xsrf:xsrf, random_id:Math.floor(Math.random()*10000001) },
			onComplete:function(transport) {
				
				// Closes the pop-up, if it's open.
				Container.close_popup('popup_message_view',false,false);
				
				// Refreshes the folder.
				Messages.refresh_folder();
				
				// Updates the counters. We must update everything, since unread stuff can be moved around.
				Messages.update_unread_counter();
			}});
		}
	},
	
	// Gets the number of unread messages in a given folder (using JavaScript only).
	count_unread:function(folder_id) {
		return ($(folder_id+'_unread') ? parseInt(Shortcut.get_valid_chars($(folder_id+'_unread').innerHTML,'0123456789')) : 0);
	},
	
	// Refreshes a folder container.
	refresh_folder:function(folder_id) {
		
		// If no folder ID, use the current folder.
		if(folder_id==undefined) folder_id = Messages.current_folder;
		
		// Refreshes the folder by changing it.
		Messages.change_folder(folder_id);
	},
	
	// Counts the number of unread messages in a folder.
	update_unread_counter:function(folder_id,count) {
		
		// If the folder ID is not specified, update the inbox, trash, and spam folders.
		if(folder_id==undefined) {
			Messages.update_unread_counter('inbox');
			Messages.update_unread_counter('trash');
			Messages.update_unread_counter('spam');
		}
		
		// If a folder ID was specified, but the count is undefined. We need to get the count.
		else if(count==undefined) {
			
			// The Ajax.
			new Ajax.Request('/ajax.php', { method:'get',
				parameters:{ control:'messages', action:'count_unread', folder_id:folder_id,xsrf:xsrf,random_id:Math.floor(Math.random()*10000001) },
			onComplete:function(transport) {
				
				// Assumes 0 unread messages by default.
				var count = 0;
				
				// Makes sure we're dealing with an integer.
				if(transport.responseText) count = parseInt(transport.responseText);
				
				// Updates the counter.
				Messages.update_unread_counter(folder_id,count);
			}});
		}
		
		// If both a folder ID and count are specified.
		else {
			
			// Makes sure there's a <span> to update.
			if($(folder_id+'_unread')) {
				
				// Updates the <span>.
				$(folder_id+'_unread').update('('+count+')');
				
				// Will we be hiding or showing the counter?
				var hide_or_show = (count>0 ? 'show' : 'hide');
				
				// Toggles the visiblility of the <span>.
				eval('$(folder_id+"_unread").'+hide_or_show+'();');
			}
		}
	},
	
	// Views a conversation.
	view_message:function(convo_id) {
		
		// Makes sure the conversation ID is defined.
		if(convo_id!=undefined) {
			
			// The message is likely already being listed on the page. This is just in case it's not.
			if($('message_'+convo_id) && $('message_'+convo_id).hasClassName('attn')) {
				
				// Removes the "attn" class to indicate that the message has been read (or rather, is about to be read).
				$('message_'+convo_id).removeClassName('attn');
				
				// Updates the counter for the current folder.
				Messages.update_unread_counter(Messages.current_folder,Messages.count_unread(Messages.current_folder)-1);
			}
			
			// Views the message.
			Container.show_popup('message_view',convo_id,Messages.current_folder);
		}
	}
};