Index: trunk/expressoMail1_2/js/messages_proxy.js
===================================================================
--- trunk/expressoMail1_2/js/messages_proxy.js	(revision 1752)
+++ trunk/expressoMail1_2/js/messages_proxy.js	(revision 1938)
@@ -14,4 +14,5 @@
 			var msgs = expresso_local_messages.get_local_range_msgs(real_folder,msg_range_begin,preferences.max_email_per_page,sort_box_type,sort_box_reverse,search_box_type,preferences.preview_msg_subject,preferences.preview_msg_tip);
 			eval('call_back(msgs)');
+			draw_tree_local_folders();
 		}else {
 			cExecute ("$this.imap_functions.get_range_msgs2&folder="+folder+"&msg_range_begin="+msg_range_begin+"&msg_range_end="+emails_per_page+"&sort_box_type="+sort_box_type+ "&search_box_type="+ search_box_type +"&sort_box_reverse="+sort_box_reverse+"", call_back);
@@ -39,6 +40,8 @@
 		if (!this.is_local_folder(folder)) 
 			delete_msgs(folder, msgs_number, border_ID);
-		else 
-			expresso_local_messages.delete_msgs(msgs_number,border_ID);
+		else {
+			expresso_local_messages.delete_msgs(msgs_number, border_ID);
+			draw_tree_local_folders();
+		}
 	}
 	
@@ -133,5 +136,5 @@
 			if (!temp) 
 				alert(get_lang("cannot create folder. try other folder name"));
-			ttreeBox.update_folder();
+			draw_tree_local_folders();
 		}
 		else			
@@ -166,5 +169,5 @@
 				expresso_local_messages.remove_folder(ttree.FOLDER.substr(6));
 				alert(get_lang("The folder %1 was successfully removed", new_caption));
-				ttreeBox.update_folder();
+				draw_tree_local_folders();
 			}
 		}
@@ -189,4 +192,5 @@
 				expresso_local_messages.move_messages(new_folder.substr(6), msgs_number);
 				this.aux_interface_remove_mails(msgs_number, new_folder_name, border_ID);
+				draw_tree_local_folders();
 			}
 			else 
@@ -202,4 +206,5 @@
 				else
 					archive_msgs(folder,new_folder,msgs_number);
+				draw_tree_local_folders();
 			}
 			else {
Index: trunk/expressoMail1_2/js/local_messages.js
===================================================================
--- trunk/expressoMail1_2/js/local_messages.js	(revision 1806)
+++ trunk/expressoMail1_2/js/local_messages.js	(revision 1938)
@@ -161,4 +161,5 @@
 		' (id_folder text,folder_name text,uid_usuario int)');
 		this.dbGears.execute('create table if not exists msgs_to_remove (id_msg int,folder text,uid_usuario int)');
+		this.dbGears.execute('create index if not exists idx_user3 on mail (id_folder,uid_usuario,timestamp)');
 
 		//some people that used old version of local messages could not have the size column. If it's the first version
@@ -415,11 +416,6 @@
 			filter = "and header like '%\"Answered\";s:1:\"A%' ";
 		}
-		sql = 'select rowid,header,size,timestamp,unseen,body from (';
-		sql += 'select mail.rowid as rowid,mail.header as header,mail.size as size,mail.timestamp as timestamp,mail.unseen as unseen,mail.body as body,ltrim(ltrim(substr(UPPER(ffrom),21,length(ffrom)),\':\'),\'"\') as order_from,mail.subject from mail inner join folder on mail.id_folder=folder.rowid where mail.uid_usuario=? and folder.folder=? and lower(mail.ffrom) not like lower(?) ';
-		sql += filter;
-		sql += 'union ';
-		sql += 'select mail.rowid as rowid,mail.header as header,mail.size as size,mail.timestamp as timestamp,mail.unseen as unseen,mail.body as body,ltrim(ltrim(substr(UPPER(fto),7,length(fto)),\':\'),\'"\') as order_from,mail.subject from mail inner join folder on mail.id_folder=folder.rowid where mail.uid_usuario=? and folder.folder=? and lower(mail.ffrom) like lower(?) ';
-		sql += filter;
-		sql += ') order by '
+		
+		sql = 'select mail.rowid as rowid,mail.header as header,mail.size as size,mail.timestamp as timestamp,mail.unseen as unseen,mail.body as body,case when lower(mail.ffrom) like ? then ltrim(ltrim(substr(UPPER(ffrom),21,length(ffrom)),\':\'),\'"\') else ltrim(ltrim(substr(UPPER(fto),7,length(fto)),\':\'),\'"\') end as order_from,mail.subject from mail inner join folder on mail.id_folder=folder.rowid where mail.uid_usuario=? and folder.folder=? order by '
 		
 		if(sort == 'SORTFROM') {
@@ -441,5 +437,5 @@
 
 
-		var rs = this.dbGears.execute(sql,[account_id,folder,'%'+Element("user_email").value+'%',account_id,folder,'%'+Element("user_email").value+'%',msg_range_begin,emails_per_page]);
+		var rs = this.dbGears.execute(sql,['%'+Element("user_email").value+'%',account_id,folder,msg_range_begin,emails_per_page]);
 		var cont = 0;
 		
@@ -662,12 +658,14 @@
 		this.init_local_messages();
 		var retorno = new Array();
-		rs = this.dbGears.execute("select folder.folder,sum(mail.unseen) from folder left join mail on "+
-				"folder.rowid=mail.id_folder where folder.uid_usuario=? group by folder.folder",[account_id]);
+		rs = this.dbGears.execute("select folder,rowid from folder where uid_usuario=?",[account_id]);
 		while(rs.isValidRow()) {
 			var temp = new Array();
 			temp[0] = rs.field(0);
-			temp[1] = rs.field(1);
-			var rs2 = this.dbGears.execute("select * from folder where folder like ?",["%"+temp[0]+"/%"]);
-			if(rs2.isValidRow())
+			
+			var rs2 = this.dbGears.execute("select count(*) from mail where id_folder=? and unseen=1",[rs.field(1)]);
+			temp[1] = rs2.field(0);			
+
+			var rs3 = this.dbGears.execute("select * from folder where folder like ? limit 1",[temp[0]+"/%"]);
+			if(rs3.isValidRow())
 				temp[2] = 1;
 			else
Index: trunk/expressoMail1_2/js/draw_api.js
===================================================================
--- trunk/expressoMail1_2/js/draw_api.js	(revision 1912)
+++ trunk/expressoMail1_2/js/draw_api.js	(revision 1938)
@@ -117,54 +117,5 @@
 			cExecute ("$this.imap_functions.get_folders_list&folder="+current_folder, update_tree_folders);
 		}
-
-		/**
-		 * Pastas locais
-		 */
-		if(preferences.use_local_messages==1 || expresso_offline) {
-			var local_folders = expresso_local_messages.list_local_folders();
-			var has_changes = false;
-			for (var i in local_folders) { //Coloca as pastas locais.
-				
-				var new_caption = local_folders[i][0];
-				if(local_folders[i][0].indexOf("/")!="-1") {
-					final_pos = local_folders[i][0].lastIndexOf("/");
-					new_caption = local_folders[i][0].substr(final_pos+1);
-				}
-				
-				var folder_unseen = Element('dftree_local_'+local_folders[i][0]+'_unseen');
-				
-				if ((folder_unseen) && (local_folders[i][1] > 0))
-				{
-					folder_unseen.innerHTML = local_folders[i][1];
-					has_changes = true;
-				}
-				else if (local_folders[i][1] > 0)
-				{
-					tree_folders.getNodeById("local_"+local_folders[i][0]).alter({caption:lang_folder(new_caption) + '<font style=color:red>&nbsp(</font><span id="dftree_local_'+local_folders[i][0]+'_unseen" style=color:red>'+local_folders[i][1]+'</span><font style=color:red>)</font>'});
-					tree_folders.getNodeById("local_"+local_folders[i][0])._refresh();
-					has_changes = true;
-				}
-				else if (local_folders[i][1] <= 0)
-				{
-					tree_folders.getNodeById("local_"+local_folders[i][0]).alter({caption:lang_folder(new_caption)});
-					tree_folders.getNodeById("local_"+local_folders[i][0])._refresh();
-					has_changes = true;
-				}
-	
-				if("local_"+local_folders[i][0] == get_current_folder()){					
-					var old_new_m = isNaN(parseInt(Element('new_m').innerHTML)) ? parseInt(Element('new_m').firstChild.innerHTML) : parseInt(Element('new_m').innerHTML);
-					if(!isNaN(old_new_m) && old_new_m < local_folders[i][1]){
-						Element('tot_m').innerHTML = parseInt(Element('tot_m').innerHTML) + (parseInt(local_folders[i][1])-old_new_m);
-					}
-					Element('new_m').innerHTML = local_folders[i][1] ? '<font color="RED">'+local_folders[i][1]+'</font>' : 0;
-					draw_paging(Element('tot_m').innerHTML);
-					has_changes = true;
-				}
-			}
-			if(has_changes)
-				tree_folders.getNodeById("local_root").open();
-
-		}
-		
+	
 		return;
 	}
@@ -372,4 +323,55 @@
 		}
 	}
+}
+
+function draw_tree_local_folders() {
+	/**
+	 * Pastas locais
+	 */
+	if(preferences.use_local_messages==1 || expresso_offline) {
+		var local_folders = expresso_local_messages.list_local_folders();
+		var has_changes = false;
+		for (var i in local_folders) { //Coloca as pastas locais.
+			
+			var new_caption = local_folders[i][0];
+			if(local_folders[i][0].indexOf("/")!="-1") {
+				final_pos = local_folders[i][0].lastIndexOf("/");
+				new_caption = local_folders[i][0].substr(final_pos+1);
+			}
+			
+			var folder_unseen = Element('dftree_local_'+local_folders[i][0]+'_unseen');
+			
+			if ((folder_unseen) && (local_folders[i][1] > 0))
+			{
+				folder_unseen.innerHTML = local_folders[i][1];
+				has_changes = true;
+			}
+			else if (local_folders[i][1] > 0)
+			{
+				tree_folders.getNodeById("local_"+local_folders[i][0]).alter({caption:lang_folder(new_caption) + '<font style=color:red>&nbsp(</font><span id="dftree_local_'+local_folders[i][0]+'_unseen" style=color:red>'+local_folders[i][1]+'</span><font style=color:red>)</font>'});
+				tree_folders.getNodeById("local_"+local_folders[i][0])._refresh();
+				has_changes = true;
+			}
+			else if (local_folders[i][1] <= 0)
+			{
+				tree_folders.getNodeById("local_"+local_folders[i][0]).alter({caption:lang_folder(new_caption)});
+				tree_folders.getNodeById("local_"+local_folders[i][0])._refresh();
+				has_changes = true;
+			}
+
+			if("local_"+local_folders[i][0] == get_current_folder()){					
+				var old_new_m = isNaN(parseInt(Element('new_m').innerHTML)) ? parseInt(Element('new_m').firstChild.innerHTML) : parseInt(Element('new_m').innerHTML);
+				if(!isNaN(old_new_m) && old_new_m < local_folders[i][1]){
+					Element('tot_m').innerHTML = parseInt(Element('tot_m').innerHTML) + (parseInt(local_folders[i][1])-old_new_m);
+				}
+				Element('new_m').innerHTML = local_folders[i][1] ? '<font color="RED">'+local_folders[i][1]+'</font>' : 0;
+				draw_paging(Element('tot_m').innerHTML);
+				has_changes = true;
+			}
+		}
+		if(has_changes)
+			tree_folders.getNodeById("local_root").open();
+
+	}	
 }
 
