Index: /trunk/expressoMail1_2/inc/class.imap_functions.inc.php
===================================================================
--- /trunk/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 2739)
+++ /trunk/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 2747)
@@ -117,5 +117,7 @@
 		$return = array();
 		$i = 0;
-		$num_msgs = imap_num_msg($this->mbox);
+		$status = imap_status($this->mbox,
+			"{".$this->email_server['imapServer'].":".$this->email_server['imapPort'].$this->imap_options."}".$folder,
+			SA_MESSAGES + SA_UNSEEN);
 		if(is_array($sort_array_msg)){
 			foreach($sort_array_msg as $msg_number => $value)
@@ -132,5 +134,6 @@
 			}
 		}
-		$return['num_msgs'] = $num_msgs;
+		$return['num_msgs'] = $status->messages;
+		$return['num_unseen'] = $status->unseen;
 
 		return $return;
@@ -2321,5 +2324,5 @@
 	}
 
-	function messages_sort($sort_box_type,$sort_box_reverse, $search_box_type,$offsetBegin,$offsetEnd)
+	function messages_sort($sort_box_type, $sort_box_reverse, $search_box_type, $offsetBegin, $offsetEnd)
 	{
 		if ($sort_box_type != "SORTFROM" && $search_box_type!= "FLAGGED"){
Index: /trunk/expressoMail1_2/js/jscode/draw_api_folders.js
===================================================================
--- /trunk/expressoMail1_2/js/jscode/draw_api_folders.js	(revision 2745)
+++ /trunk/expressoMail1_2/js/jscode/draw_api_folders.js	(revision 2747)
@@ -427,14 +427,14 @@
 }
 
-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("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)
@@ -472,67 +472,60 @@
 caption:lang_folder(data[i].folder_name) + '<font style=color:red>&nbsp(</font><span id="dftree_'+data[i].folder_id+'_unseen" style=color:red>'+data[i].folder_unseen+'</span><font style=color:red>)</font>'
 });
-tree_folders.getNodeById(data[i].folder_id)._refresh();
-}
-else if (data[i].folder_unseen <= 0)
-{
-	tree_folders.getNodeById(data[i].folder_id).alter({
-caption:lang_folder(data[i].folder_name)
-});
-tree_folders.getNodeById(data[i].folder_id)._refresh();
-}
-
-if(data[i].folder_id == current_folder){
-	var old_new_m = isNaN(parseInt(Element('new_m').innerHTML)) ? parseInt(Element('new_m').firstChild.innerHTML) : parseInt(Element('new_m').innerHTML);
+						tree_folders.getNodeById(data[i].folder_id)._refresh();
+					}
+					else if (data[i].folder_unseen <= 0)
+					{
+					tree_folders.getNodeById(data[i].folder_id).alter({
+					caption:lang_folder(data[i].folder_name)
+					});
+					tree_folders.getNodeById(data[i].folder_id)._refresh();
+					}
+					if(data[i].folder_id == 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 < data[i].folder_unseen){
 	//						Element('tot_m').innerHTML = parseInt(Element('tot_m').innerHTML) + (parseInt(data[i].folder_unseen)-old_new_m);
 	//					}
-	Element('new_m').innerHTML = data[i].folder_unseen ? '<font color="RED">'+data[i].folder_unseen+'</font>' : 0;
-	draw_paging(Element('tot_m').innerHTML);
-}
-}
-
-var display_unseen_in_mailbox = tree_folders.getNodeById( 'root' );
-//if ( unseen_in_mailbox )
-//	display_unseen_in_mailbox.alter({caption:'<font style=color:red>[</font><span id="dftree_root_unseen" style="color:red">' + unseen_in_mailbox +'</span><font style=color:red>]</font> ' + get_lang("My Folders")});
-//else
-display_unseen_in_mailbox.alter({
-caption:get_lang("My Folders")
-});
-display_unseen_in_mailbox._refresh();
-
-var display_unseen_in_shared_folders = tree_folders.getNodeById( 'user' );
-if ( display_unseen_in_shared_folders )
-{
-	if ( unseen_in_shared_folders )
-		display_unseen_in_shared_folders.alter({
-caption:'<font style=color:red>[</font><span id="dftree_user_unseen" style="color:red">' + unseen_in_shared_folders +'</span><font style=color:red>]</font> ' + get_lang("Shared folders")
-});
-else
-display_unseen_in_shared_folders.alter({
-caption:get_lang("Shared folders")
-});
-display_unseen_in_shared_folders._refresh();
-}
-};
-expresso.connector.cacheNextRequest(1);
-cExecute ("expressoMail1_2.imap_functions.get_folders_list&folder=INBOX", update_tree_folders);
-}
-
-return;
-}
-else{
-	if (!expresso_offline) {
-		tree_folders = new dFTree({
-name: 'tree_folders'
-});
-
-var n_root = new dNode({
-id:'root',
-caption: get_lang("My Folders")
-});
-tree_folders.add(n_root,'anything'); //Places the root; second argument can be anything.
-
-var unseen_in_mailbox = 0;
-var unseen_in_shared_folders = 0;
+					Element('new_m').innerHTML = data[i].folder_unseen ? '<font color="RED">'+data[i].folder_unseen+'</font>' : 0;
+					draw_paging(Element('tot_m').innerHTML);
+					}
+			}
+
+			var display_unseen_in_mailbox = tree_folders.getNodeById( 'root' );
+			//if ( unseen_in_mailbox )
+			//	display_unseen_in_mailbox.alter({caption:'<font style=color:red>[</font><span id="dftree_root_unseen" style="color:red">' + unseen_in_mailbox +'</span><font style=color:red>]</font> ' + get_lang("My Folders")});
+			//else
+			display_unseen_in_mailbox.alter({
+				caption:get_lang("My Folders")
+			});
+			display_unseen_in_mailbox._refresh();
+
+			var display_unseen_in_shared_folders = tree_folders.getNodeById( 'user' );
+			if ( display_unseen_in_shared_folders )
+			{
+				if ( unseen_in_shared_folders )
+				display_unseen_in_shared_folders.alter({
+				caption:'<font style=color:red>[</font><span id="dftree_user_unseen" style="color:red">' + unseen_in_shared_folders +'</span><font style=color:red>]</font> ' + get_lang("Shared folders")
+			});
+			else
+			display_unseen_in_shared_folders.alter({
+			caption:get_lang("Shared folders")
+			});
+			display_unseen_in_shared_folders._refresh(); } };
+		}
+		return;
+	}
+	else{
+		if (!expresso_offline) {
+			tree_folders = new dFTree({
+			name: 'tree_folders'
+			});
+				var n_root = new dNode({
+				id:'root',
+				caption: get_lang("My Folders")
+			});
+			tree_folders.add(n_root,'anything'); //Places the root; second argument can be anything.
+
+	var unseen_in_mailbox = 0;
+	var unseen_in_shared_folders = 0;
 	for (var i=0; i<folders.length; i++){
 		if (folders[i].folder_unseen > 0)
@@ -543,41 +536,41 @@
 
 			var nn = new dNode({
-id:folders[i].folder_id,
-caption:lang_folder(folders[i].folder_name) + '<font style=color:red>&nbsp(</font><span id="dftree_'+folders[i].folder_id+'_unseen" style=color:red>'+folders[i].folder_unseen+'</span><font style=color:red>)</font>',
-onClick:"change_folder('"+folders[i].folder_id+"','"+folders[i].folder_name+"')",
-plusSign:folders[i].folder_hasChildren
-});
-
-if( folders[i].folder_name.toLowerCase() == 'inbox' )
-Element('new_m').innerHTML = '<font style="color:red">' + folders[i].folder_unseen + '</font>';
-}
-else
-var nn = new dNode({
-id:folders[i].folder_id,
-caption:lang_folder(folders[i].folder_name),
-onClick:"change_folder('"+folders[i].folder_id+"','"+folders[i].folder_name+"')",
-plusSign:folders[i].folder_hasChildren
-});
-
-if (folders[i].folder_parent == '')
-folders[i].folder_parent = 'root';
-else if (folders[i].folder_parent == 'user'){
-
-	if (!tree_folders.getNodeById('user')){
-		tmpFolderId = folders[i].folder_id.split(cyrus_delimiter).pop();
-		if (tmpFolderId != folders[i].folder_name)
-		{
-			translatedFolders.add(tmpFolderId, folders[i].folder_name);
-		}
-		var n_root_shared_folders = new dNode({
-id:'user',
-caption:get_lang("Shared folders"),
-plusSign:true
-});
-tree_folders.add(n_root_shared_folders,'root');
-}
-}
-tree_folders.add(nn,folders[i].folder_parent);
-}
+				id:folders[i].folder_id,
+				caption:lang_folder(folders[i].folder_name) + '<font style=color:red>&nbsp(</font><span id="dftree_'+folders[i].folder_id+'_unseen" style=color:red>'+folders[i].folder_unseen+'</span><font style=color:red>)</font>',
+				onClick:"change_folder('"+folders[i].folder_id+"','"+folders[i].folder_name+"')",
+				plusSign:folders[i].folder_hasChildren
+			});
+
+			if( folders[i].folder_name.toLowerCase() == 'inbox' )
+				Element('new_m').innerHTML = '<font style="color:red">' + folders[i].folder_unseen + '</font>';
+			}
+			else
+				var nn = new dNode({
+					id:folders[i].folder_id,
+					caption:lang_folder(folders[i].folder_name),
+					onClick:"change_folder('"+folders[i].folder_id+"','"+folders[i].folder_name+"')",
+					plusSign:folders[i].folder_hasChildren
+				});
+
+			if (folders[i].folder_parent == '')
+				folders[i].folder_parent = 'root';
+			else if (folders[i].folder_parent == 'user'){
+
+				if (!tree_folders.getNodeById('user')){
+					tmpFolderId = folders[i].folder_id.split(cyrus_delimiter).pop();
+					if (tmpFolderId != folders[i].folder_name)
+					{
+						translatedFolders.add(tmpFolderId, folders[i].folder_name);
+					}
+					var n_root_shared_folders = new dNode({
+						id:'user',
+						caption:get_lang("Shared folders"),
+						plusSign:true
+					});
+					tree_folders.add(n_root_shared_folders,'root');
+				}
+		}
+		tree_folders.add(nn,folders[i].folder_parent);
+	}
 }
 //tree_local_folders = new dFTree({name: 'tree_local_folders'});
@@ -847,10 +840,16 @@
 	var handler_draw_box = function(data)
 	{
-	    draw_box.headers_msgs = data;
-  	    draw_box.draw_box_messages(data, folder, true);
-	    draw_paging(data.num_msgs);
-	    Element("tot_m").innerHTML = data.num_msgs;
-	    update_menu();
-	    return true;
+		draw_box.headers_msgs = data;
+		draw_box.draw_box_messages(data, folder, true);
+		draw_paging(data.num_msgs);
+		Element("tot_m").innerHTML = data.num_msgs;
+		Element("new_m").innerHTML = '<font style="color:'+(data.num_unseen > 0 ? 'red': '')+'">' + data.num_unseen + '</font>';
+		folder_unseen = Element('dftree_'+current_folder+'_unseen');
+		var num_unseen = data.num_unseen;
+		if (folder_unseen)
+			num_unseen -= folder_unseen.innerHTML;
+		increment_folder_unseen(num_unseen);
+		update_menu();
+		return true;
 	};
 	proxy_mensagens.messages_list(current_folder,1,preferences.max_email_per_page,sort_box_type,search_box_type,sort_box_reverse,preferences.preview_msg_subject,preferences.preview_msg_tip,handler_draw_box);
Index: /trunk/expressoMail1_2/js/jscode/common_functions.js
===================================================================
--- /trunk/expressoMail1_2/js/jscode/common_functions.js	(revision 2692)
+++ /trunk/expressoMail1_2/js/jscode/common_functions.js	(revision 2747)
@@ -76,5 +76,5 @@
 	tr_message = Element(msg_number);
 	if (exist_className(tr_message, 'tr_msg_unread'))
-		decrement_folder_unseen();
+		decrement_folder_unseen(1);
 	remove_className(tr_message, 'tr_msg_unread');
 	remove_className(tr_message, 'selected_msg');
@@ -92,5 +92,5 @@
 	tr_message = Element(msg_number);
 	if ((exist_className(tr_message, 'tr_msg_read') || exist_className(tr_message, 'tr_msg_read2')) && (!exist_className(tr_message, 'tr_msg_unread')))
-		increment_folder_unseen();	
+		increment_folder_unseen(1);
 	remove_className(tr_message, 'selected_msg');
 	add_className(tr_message, 'tr_msg_unread');
@@ -525,23 +525,27 @@
 };
 
-function increment_folder_unseen(){
+function increment_folder_unseen(num){
+	if (num < 0)
+		return decrement_folder_unseen(num*-1);
+	else if(num == 0)
+		return;
 	var folder_id = get_current_folder();
 
 	var folder_unseen = Element('dftree_'+folder_id+'_unseen');
 	var abas_unseen = Element('new_m').innerHTML;
-        abas_unseen = abas_unseen.match( /[0-9]+/);
+        abas_unseen = abas_unseen.match(/[0-9]+/);
 
 	if (folder_unseen)
 	{
-		folder_unseen.innerHTML = (parseInt(folder_unseen.innerHTML) + 1);
+		folder_unseen.innerHTML = (parseInt(folder_unseen.innerHTML) + num);
 	}
 	else
 	{
-		tree_folders.getNodeById(folder_id).alter({caption: tree_folders.getNodeById(current_folder).caption + '<font style=color:red>&nbsp(</font><span id="dftree_'+current_folder+'_unseen" style=color:red>1</span><font style=color:red>)</font>'});
+		tree_folders.getNodeById(folder_id).alter({caption: tree_folders.getNodeById(current_folder).caption + '<font style=color:red>&nbsp(</font><span id="dftree_'+current_folder+'_unseen" style=color:red>'+num+'</span><font style=color:red>)</font>'});
 		tree_folders.getNodeById(folder_id)._refresh();
 	}
 	if (abas_unseen)
 	{
-		abas_unseen = parseInt(abas_unseen) + 1;
+		abas_unseen = parseInt(abas_unseen) + num;
 		Element('new_m').innerHTML = '<font style="color:red">' + abas_unseen + '</font>';
 	}
@@ -549,5 +553,5 @@
 		abas_unseen = Element('new_m').innerHTML;
 		abas_unseen = abas_unseen.match( /[0-9]+/);
-		abas_unseen = parseInt(abas_unseen) + 1;
+		abas_unseen = parseInt(abas_unseen) + num;
 		Element('new_m').innerHTML = '<font style="color:red">' + abas_unseen + '</font>';
 	}
@@ -556,5 +560,5 @@
 		var display_unseen_in_shared_folders = Element('dftree_user_unseen');
 		if ( display_unseen_in_shared_folders )
-			tree_folders.getNodeById( 'user' ).alter({caption:'<font style=color:red>[</font><span id="dftree_user_unseen" style="color:red">' + ( parseInt( display_unseen_in_shared_folders.innerHTML) + 1 ) + '</span><font style=color:red>]</font>' + get_lang("Shared folders")});
+			tree_folders.getNodeById( 'user' ).alter({caption:'<font style=color:red>[</font><span id="dftree_user_unseen" style="color:red">' + ( parseInt( display_unseen_in_shared_folders.innerHTML) + num ) + '</span><font style=color:red>]</font>' + get_lang("Shared folders")});
 		else
 			tree_folders.getNodeById( 'user' ).alter({caption:'<font style=color:red>[</font><span id="dftree_user_unseen" style="color:red">1</span><font style=color:red>]</font>' + get_lang("Shared folders")});
@@ -568,5 +572,5 @@
 	tree_folders.getNodeById( node_to_refresh )._refresh();
 }
-function decrement_folder_unseen(){
+function decrement_folder_unseen(num){
 	var folder_id = get_current_folder();
 
@@ -578,9 +582,9 @@
 		return;
 
-	if ((folder_unseen) && (parseInt(folder_unseen.innerHTML) > 1))
-	{
-		folder_unseen.innerHTML = (parseInt(folder_unseen.innerHTML) - 1);
-	}
-	else if (parseInt(folder_unseen.innerHTML) <= 1)
+	if ((folder_unseen) && (parseInt(folder_unseen.innerHTML) > num))
+	{
+		folder_unseen.innerHTML = (parseInt(folder_unseen.innerHTML) - num);
+	}
+	else if (parseInt(folder_unseen.innerHTML) <= num)
 	{
 		var tmp_folder_name = tree_folders.getNodeById(folder_id).caption.split('<');
@@ -589,7 +593,7 @@
 		tree_folders.getNodeById(folder_id)._refresh();
 	}
-	if ((abas_unseen) && (parseInt(abas_unseen) > 1))
+	if ((abas_unseen) && (parseInt(abas_unseen) > num))
         {
-	        abas_unseen = (parseInt(abas_unseen) - 1);
+	        abas_unseen = (parseInt(abas_unseen) - num);
 	        Element('new_m').innerHTML = '<font style="color:red">' + abas_unseen + '</font>';
 	}
Index: /trunk/expressoMail1_2/index.php
===================================================================
--- /trunk/expressoMail1_2/index.php	(revision 2648)
+++ /trunk/expressoMail1_2/index.php	(revision 2747)
@@ -154,4 +154,5 @@
 $GLOBALS[ 'phpgw' ] -> js -> validate_file( 'jscode', 'drag_area', 'expressoMail1_2' );
 $GLOBALS[ 'phpgw' ] -> js -> validate_file( 'jscode', 'draw_api_messages', 'expressoMail1_2' );
+$GLOBALS[ 'phpgw' ] -> js -> validate_file( 'jscode', 'messages', 'expressoMail1_2' );
 $GLOBALS[ 'phpgw' ] -> js -> validate_file( 'jscode', 'draw_api_folders', 'expressoMail1_2' );
 $GLOBALS[ 'phpgw' ] -> js -> validate_file( 'jscode', 'DropDownContacts', 'expressoMail1_2' );
