Index: /trunk/expressoMail1_2/inc/class.imap_functions.inc.php
===================================================================
--- /trunk/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 7552)
+++ /trunk/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 7603)
@@ -1119,7 +1119,25 @@
 		
 		$return = array();
-
+	
+		$msg_ids = array_values($this->getMessagesIds($params));	
+		
 		$msg_number = $params['msg_number'];
 		$msg_folder = isset($params['decoded']) ? $params['msg_folder'] : urldecode($params['msg_folder']);
+		
+		if($msg_ids[0] == $msg_number){
+			$return['next_message'] = $msg_ids[1];
+			$return['prev_message'] = null;
+		}else if($msg_ids[count($msg_ids)-1] == $msg_number){
+			$return['prev_message'] = $msg_ids[count($msg_ids)-2];
+			$return['next_message'] = null;
+		}else{
+			for($i = 0; $i < count($msg_ids); $i++){
+				if($msg_ids[$i] == $msg_number){
+					$return['prev_message'] = $msg_ids[$i-1];
+					$return['next_message'] = $msg_ids[$i+1];
+					break;
+				}
+			}
+		}
 		
 		if(preg_match('/(.+)(_[a-zA-Z0-9]+)/',$msg_number,$matches)) { //Verifies if it comes from a tab diferent of the main one. 
Index: /trunk/expressoMail1_2/js/main.js
===================================================================
--- /trunk/expressoMail1_2/js/main.js	(revision 7601)
+++ /trunk/expressoMail1_2/js/main.js	(revision 7603)
@@ -617,8 +617,8 @@
 				openTab.imapBox[border_id] = msg_info.msg_folder;
 				draw_message(msg_info,border_id);
-				var unseen_sort = document.getElementById('span_flag_UNSEEN').getAttribute('onclick');
-				unseen_sort = unseen_sort.toString();
-				if ( !(unseen_sort.indexOf("'UNSEEN' == 'UNSEEN'") < 0) )
-				{
+//				var unseen_sort = document.getElementById('span_flag_UNSEEN').getAttribute('onclick');
+//				unseen_sort = unseen_sort.toString();
+//				if ( !(unseen_sort.indexOf("'UNSEEN' == 'UNSEEN'") < 0) )
+//				{
 //					var sort_type = sort_box_type;
 //					sort_box_type = null;
@@ -626,5 +626,5 @@
 //					delete selectedPagingMsgs[msg_info.msg_number];
 //					totalFolderMsgs--;
-				}
+//				}
 			}
 			else
@@ -986,5 +986,5 @@
 }
 
-function delete_msgs(folder, msgs_number, border_ID, show_success_msg,archive){	
+function delete_msgs(folder, msgs_number, border_ID, show_success_msg,archive, prev_message){	
             if( preferences.use_local_messages == 1 && expresso_local_messages.isArchiving( msgs_number, folder ) ){
 			  alert( "Impossivel deletar mensagens que ainda estão sendo arquivadas." );
@@ -1020,5 +1020,5 @@
  		         
  		        if (!archive && (parseInt(preferences.save_deleted_msg)) && (folder != userTrashFolder)){ 
- 		            move_msgs2(folder, ""+msgs_number, border_ID, userTrashFolder,special_folders['Trash'],show_success_msg ); 
+ 		            move_msgs2(folder, ""+msgs_number, border_ID, userTrashFolder,special_folders['Trash'],show_success_msg, undefined, prev_message ); 
 					return;
 				}
@@ -1073,24 +1073,26 @@
 			if (parseInt(preferences.delete_and_show_previous_message) && msg_to_delete) {
 				if (msg_to_delete.previousSibling){
- 					var previous_msg = msg_to_delete.previousSibling.id;
-					 $.ajax({
-						  url: 'controller.php?' + $.param( {action: '$this.imap_functions.get_info_msg',
-										      msg_number: previous_msg, 
-										      msg_folder: current_folder,
-										      decoded: true } ),
-						  success: function( data ){
-						      data = connector.unserialize( data );
-						      
-						      if( data )
-							  show_msg( data );
-						  },
-						  beforeSend: function( jqXHR, settings ){
-						  	connector.showProgressBar();
-						  },
-						  complete: function( jqXHR, settings ){
-						  	connector.hideProgressBar();
-						  }
-
-					});
+ 					var previous_msg = prev_message ? prev_message : msg_to_delete.previousSibling.id;
+					if(previous_msg){
+						 $.ajax({
+							  url: 'controller.php?' + $.param( {action: '$this.imap_functions.get_info_msg',
+												  msg_number: previous_msg, 
+												  msg_folder: current_folder,
+												  decoded: true } ),
+							  success: function( data ){
+								  data = connector.unserialize( data );
+								  
+								  if( data )
+								  show_msg( data );
+							  },
+							  beforeSend: function( jqXHR, settings ){
+								connector.showProgressBar();
+							  },
+							  complete: function( jqXHR, settings ){
+								connector.hideProgressBar();
+							  }
+
+						});
+					}
  				} 
 				else{
@@ -1299,5 +1301,5 @@
 }
 
-function move_msgs2(folder, msgs_number, border_ID, new_folder, new_folder_name,show_success_msg, not_opem_previus){
+function move_msgs2(folder, msgs_number, border_ID, new_folder, new_folder_name,show_success_msg, not_opem_previus, prev_message){
 	not_opem_previus = typeof(not_opem_previus) != 'undefined' ? not_opem_previus : false;
 	var folder_error = new_folder_name;
@@ -1343,25 +1345,28 @@
 				if (msg_to_delete.previousSibling)
 				{
-					var previous_msg = msg_to_delete.previousSibling.id;
+					var previous_msg = prev_message ? prev_message : msg_to_delete.previousSibling.id;
+					
 					//cExecute("$this.imap_functions.get_info_msg&msg_number="+previous_msg+"&msg_folder=" + current_folder, show_msg);
 					if(!not_opem_previus){
-						$.ajax({
-							  url: 'controller.php?' + $.param( {action: '$this.imap_functions.get_info_msg',
-											      msg_number: previous_msg, 
-											      msg_folder: folder,
-											      decoded: true } ),
-							  success: function( data ){
-							      data = connector.unserialize( data );
-							      
-							      if( data )
-								  show_msg( data );
-							  },
-				 			  beforeSend: function( jqXHR, settings ){
-							  	connector.showProgressBar();
-							  },
-							  complete: function( jqXHR, settings ){
-							  	connector.hideProgressBar();
-							  }
-						});
+						if(previous_msg){
+							$.ajax({
+								  url: 'controller.php?' + $.param( {action: '$this.imap_functions.get_info_msg',
+													  msg_number: previous_msg, 
+													  msg_folder: folder,
+													  decoded: true } ),
+								  success: function( data ){
+									  data = connector.unserialize( data );
+									  
+									  if( data )
+									  show_msg( data );
+								  },
+								  beforeSend: function( jqXHR, settings ){
+									connector.showProgressBar();
+								  },
+								  complete: function( jqXHR, settings ){
+									connector.hideProgressBar();
+								  }
+							});
+						}
 					}
 				}
@@ -1369,41 +1374,34 @@
 				else 
 				{
-					if (current_page > 1)
+					var border_id = $("#border_id_"+currentTab).prev().attr("id").split("_").slice(2, 3).join("")
+					var folderName = current_folder;
+					if(prev_message)
 					{
-						$('#msg_opt_previous_'+msg_to_delete.getAttribute('id') + '_r').trigger('click');
+						$.ajax({
+							url: 'controller.php?' + $.param({
+								action: '$this.imap_functions.get_info_msg',
+								msg_number: prev_message, 
+								msg_folder: folderName,
+								decoded: true 
+							}),
+							success: function( data ){
+								data = connector.unserialize( data );
+								delete_border(currentTab,'false');
+								if( data )
+								{
+									show_msg( data );
+								}
+							},
+							beforeSend: function( jqXHR, settings ){
+								connector.showProgressBar();
+							},
+							complete: function( jqXHR, settings ){
+								connector.hideProgressBar();
+							}
+						});
 					}
-					else
+					else 
 					{
-						var border_id = $("#border_id_"+currentTab).prev().attr("id").split("_").slice(2, 3).join("")
-						var folderName = current_folder;
-						if(border_id)
-						{
-							$.ajax({
-								url: 'controller.php?' + $.param({
-									action: '$this.imap_functions.get_info_msg',
-									msg_number: border_id, 
-									msg_folder: folderName,
-									decoded: true 
-								}),
-								success: function( data ){
-									data = connector.unserialize( data );
-									delete_border(currentTab,'false');
-									if( data )
-									{
-										show_msg( data );
-									}
-								},
-								beforeSend: function( jqXHR, settings ){
-									connector.showProgressBar();
-								},
-								complete: function( jqXHR, settings ){
-									connector.hideProgressBar();
-								}
-							});
-						}
-						else 
-						{
-							delete_border(currentTab,'false');
-						}
+						delete_border(currentTab,'false');
 					}
 				}
Index: /trunk/expressoMail1_2/js/messages_controller.js
===================================================================
--- /trunk/expressoMail1_2/js/messages_controller.js	(revision 7496)
+++ /trunk/expressoMail1_2/js/messages_controller.js	(revision 7603)
@@ -64,4 +64,6 @@
         }
     }
+	
+	
 
 	messages_proxy.prototype.get_msg = function(msg_number,msg_folder,set_flag,call_back) {
@@ -73,5 +75,9 @@
 			      url: 'controller.php?' + $.param( {action: '$this.imap_functions.get_info_msg',
 								  msg_number: msg_number, 
-								  msg_folder: msg_folder
+								  msg_folder: msg_folder,
+								  folder : folder,
+								  sort_box_type : sort_box_type,
+								  search_box_type : search_box_type,
+								  sort_box_reverse: sort_box_reverse
 								  } ),
 			      success: function( data ){
@@ -93,5 +99,5 @@
 	}
 	
-	messages_proxy.prototype.delete_msgs = function(folder, msgs_number, border_ID) {
+	messages_proxy.prototype.delete_msgs = function(folder, msgs_number, border_ID, id_delete_msg) {
 
 		if (folder == 'null')
@@ -114,5 +120,5 @@
 			
 		if (!this.is_local_folder(folder)){
-			delete_msgs(folder, msgs_number, border_ID);
+			delete_msgs(folder, msgs_number, border_ID, undefined, undefined, id_delete_msg);
 		}else {    
             //MAILARCHIVER-03
@@ -121,6 +127,6 @@
             if (isNaN(currentTab) && parseInt(preferences.delete_and_show_previous_message) && msg_to_delete) {
                     if (msg_to_delete.previousSibling){ 
-                            var previous_msg = msg_to_delete.previousSibling.id;
-                            
+                            var previous_msg = id_delete_msg ? id_delete_msg : msg_to_delete.previousSibling.id;
+
                             //user has preference to maintain default folder structure at local folders, so we have trash folder
                             if(preferences.auto_create_local == 1){
@@ -136,5 +142,5 @@
                                     expresso_mail_archive.deleteMessages(msgs_number);
                                     delete_border(currentTab,'false'); 
-                                    expresso_mail_archive.getMessage(previous_msg)
+                                    expresso_mail_archive.getMessage(previous_msg);
                                 }                                                                                
                             }
Index: /trunk/expressoMail1_2/js/draw_api.js
===================================================================
--- /trunk/expressoMail1_2/js/draw_api.js	(revision 7575)
+++ /trunk/expressoMail1_2/js/draw_api.js	(revision 7603)
@@ -2443,5 +2443,5 @@
 				currentTab = ID;
 				openTab.type[ID] = 2;
-				proxy_mensagens.get_msg(folder.nextSibling.id,nextMsgBox,true,show_msg);
+				proxy_mensagens.get_msg(info_msg.next_message,nextMsgBox,true,show_msg);
 				//select_msg('null', 'down', true);
 				if (!msg_selected){
@@ -2517,5 +2517,5 @@
 				currentTab = ID;
 				openTab.type[ID] = 2;
-				proxy_mensagens.get_msg(folder.previousSibling.id,previousMsgBox,true,show_msg);
+				proxy_mensagens.get_msg(info_msg.prev_message,previousMsgBox,true,show_msg);
 				//select_msg('null', 'up', false);
 				if (!msg_selected){
@@ -2902,5 +2902,5 @@
 		option_delete.id = 'msg_opt_delete_'+ID;
 		option_delete.className = 'message_options';
-		option_delete.onclick = function(){proxy_mensagens.delete_msgs('null','selected','null');};
+		option_delete.onclick = function(){proxy_mensagens.delete_msgs('null','selected','null', info_msg.prev_message);};
 		option_delete.innerHTML = get_lang('Delete');
 		option_delete.onmouseover=function () {this.className='message_options_active';};
