Index: sandbox/expressoServiceImap/prototype/modules/attach_message/attach_message.js
===================================================================
--- sandbox/expressoServiceImap/prototype/modules/attach_message/attach_message.js	(revision 6545)
+++ sandbox/expressoServiceImap/prototype/modules/attach_message/attach_message.js	(revision 6558)
@@ -13,225 +13,229 @@
 jQuery('#buttons-container .button').button();
 
-var firstFolder = jQuery("#foldertree-container")
-.removeClass('empty-container')
-.html(DataLayer.render(BASE_PATH + 'api/templates/foldertree.ejs', 'folder:tree', false, false, true))
-.find("#foldertree").treeview()
-.click(function(event){
-	//request new selected folder messages
-    var target = $(event.target);
-
-    if( target.is('.collapsable-hitarea, .expandable-hitarea, .lastCollapsable, .lastExpandable, .treeview') )
-	return;
-
-    if( !target.attr('id') )
-	target = target.parent();
-
-    if (target.attr('id') == "foldertree") return;
-
-    var targetId = target.attr('id');
-    var child = target.find('.folder');
-
-    $('.filetree span.folder.selected').removeClass('selected');
-    if(!target.is('#foldertree > .expandable, #foldertree > .collapsable'))
-	    $(target).children('.folder').addClass('selected');
-
+DataLayer.render(BASE_PATH + 'api/templates/foldertree.ejs', 'folder:tree', function(contentFolders){
+
+    var firstFolder = jQuery("#foldertree-container")
+    .removeClass('empty-container')
+    .html(contentFolders)
+    .find("#foldertree").treeview()
+    .click(function(event){
+	    //request new selected folder messages
+	var target = $(event.target);
+
+	if( target.is('.collapsable-hitarea, .expandable-hitarea, .lastCollapsable, .lastExpandable, .treeview') )
+	    return;
+
+	if( !target.attr('id') )
+	    target = target.parent();
+
+	if (target.attr('id') == "foldertree") return;
+
+	var targetId = target.attr('id');
+	var child = target.find('.folder');
+
+	$('.filetree span.folder.selected').removeClass('selected');
+	if(!target.is('#foldertree > .expandable, #foldertree > .collapsable'))
+		$(target).children('.folder').addClass('selected');
+
+	selectedFolder = {
+	    id: targetId, 
+	    name: child.attr('title'),
+	    'class': child.attr('class')
+	};
+	
+	current_folder = targetId.replace(dashes, '.');
+	current_count = DataLayer.get( 'message', { criteria: { count: true, properties: {context:{folder:current_folder}} } }, true );
+
+	$("#message_attacher_grid").trigger("reloadGrid")
+	    .jqGrid('setCaption', '<span class="'+child.attr('class')+'">'+child.attr('title')+'</span>');
+    })
+    .find('span:first-child');
+    $('span.folder.inbox').addClass('selected');
     selectedFolder = {
-	id: targetId, 
-	name: child.attr('title'),
-	'class': child.attr('class')
+	id: firstFolder.parent().attr('id'), 
+	name: firstFolder.attr('title'),
+	'class': firstFolder.attr('class')
     };
-    
-    current_folder = targetId.replace(dashes, '.');
-    current_count = DataLayer.get( 'message', { criteria: { count: true, properties: {context:{folder:current_folder}} } }, true );
-
-     $("#message_attacher_grid").trigger("reloadGrid")
-	.jqGrid('setCaption', '<span class="'+child.attr('class')+'">'+child.attr('title')+'</span>');
-})
-.find('span:first-child');
-$('span.folder.inbox').addClass('selected');
-selectedFolder = {
-    id: firstFolder.parent().attr('id'), 
-    name: firstFolder.attr('title'),
-    'class': firstFolder.attr('class')
-};
-
-//jqgrid
-jQuery("#mailgrid-container")
-.removeClass('empty-container')
-.html(DataLayer.render(BASE_PATH + 'api/templates/messagegrid.ejs', {}))
-.find("#message_attacher_grid")
-.jqGrid({
-    url: 'message:jqGrid',
-    datatype: "json",
-    mtype: 'GET',
-    colNames:['#',' ', 'De', 'Assunto', 'Data', 'Tamanho'],
-    colModel:[
-	    {name:'msg_number',index:'msg_number', width:45,  sortable:false, hidden:true},
-	    {name:'flags',     index:'msg_number', width:100, sortable:false, formatter:flags2Class, edittype: 'image', title :false},
-	    {name:'from.name', index:'msg_number', width:70,  sortable:false, formatter:NormaliseFrom},
-	    {name:'subject',   index:'subject',    width:245, sortable:false, formatter:NormaliseSubject},
-	    {name:'timestamp', index:'timestamp',  width:65,  sortable:false, formatter:date2Time,   align:"center"},
-	    {name:'size',      index:'size',       width:55,  sortable:false, formatter:bytes2Size,  align:"right"}
-    ],
-    jsonReader : {
-	      root:"rows",
-	      page: "page",
-	      total: "total",
-	      records: "records",
-	      repeatitems: false,
-	      id: "0"
-    },
-    rowNum:10,
-    rowList:[10,25,50],
-    pager: '#message_attacher_grid_pager',
-    sortname: 'id',
-    viewrecords: true,
-    sortorder: "desc",
-    multiselect: true,
-    autowidth: true,
-    loadComplete: function(data) {
-	    lastLoadedMessages = data.rows;
-	    
-	    // aplica o contador
-	    jQuery('.timable').each(function (i) {
-		    jQuery(this).countdown({
-			    since: new Date(parseInt(this.title)), 
-			    significant: 1,
-			    layout: 'h&aacute; {d<}{dn} {dl} {d>}{h<}{hn} {hl} {h>}{m<}{mn} {ml} {m>}{s<}{sn} {sl}{s>}', 
-			    description: ' atr&aacute;s'
-		    });					
-	    });
-	    
-	    // reconstrói a seleção das mensagens mesmo depois da mudança de pasta
-	    if (selectedMessages[selectedFolder.id]) {
-		    for (var message in selectedMessages[selectedFolder.id]){
-			    for (var j=0; j<data.rows.length; j++){	
-				    if (selectedMessages[selectedFolder.id][message] && message == data.rows[j].msg_number) {
-					    jQuery("#message_attacher_grid").setSelection(jQuery("#message_attacher_grid").getDataIDs()[j], false);
-				    }
-			    }
-		    }
-	    }
-	    $('#cb_message_attacher_grid').css('display', 'none');
-	    
-    },
-    onSelectRow: function (id, selected) {
-	    var message = false;
-	    for (var i=0; i<lastLoadedMessages.length; i++){
-		    if (lastLoadedMessages[i].msg_number == id) {
-			    message = lastLoadedMessages[i];
-			    break;
-		    }
-	    }
-	    
-	    var tabPanelTemplateId = 'mailpreview_tab_' + selectedFolder.id.replace(/[.\/]/g, '_') + '_' + message.msg_number;
-	    var tabPanelTemplateId = tabPanelTemplateId.replace(/[\s\/]/g, '-'); 
-	    
-	    if (selected) {	
-		    if (onceOpenedMessages[selectedFolder.id] && onceOpenedMessages[selectedFolder.id][message.msg_number]) {
-			    if (!selectedMessages[selectedFolder.id])
-				    selectedMessages[selectedFolder.id] = {};
-				    
-			    selectedMessages[selectedFolder.id][message.msg_number] = true;
-			    $('#mailpreview-container').unblock();						
-
-			    var tabPanelTemplateLabel = html_entities(onceOpenedMessages[selectedFolder.id][message.msg_number].subject);
-			    if (tabPanelTemplateLabel.length > $mailpreview_tabs_label_length + 3)
-				    tabPanelTemplateLabel = tabPanelTemplateLabel.substring(0, $mailpreview_tabs_label_length) + '...';
-			    
-			    if (!$('#' + tabPanelTemplateId).length) {								
-				    $mailpreview_tabs.tabs("add", '#' + tabPanelTemplateId, tabPanelTemplateLabel)
-				    .find('.message.empty-container').hide().end()
-				    .find('#' + tabPanelTemplateId).html(onceOpenedMessages[selectedFolder.id][message.msg_number].body);
-			    } else {
-				    $mailpreview_tabs.tabs('select', '#' + tabPanelTemplateId)
-				    .find('#' + tabPanelTemplateId + ', [href="#' + tabPanelTemplateId + '"]').removeClass('preview-message-unselected');
-			    }
-			    
-		    } else {
-			    jQuery('#mailpreview_container').block({  
-				    message: '<div id="loading-content"><div class="image"></div></div>',  
-				    css: {  
-					    backgroundImage: 'url('+BASE_PATH+'modules/attach_message/images/loading.gif)',  
-					    backgroundRepeat: 'no-repeat', 
-					    backgroundPosition: 'center', 
-					    backgroundColor: 'transparent', 
-					    width: '32px', 
-					    height: '32px', 
-					    border:'none'  
-				    }, 
-				    overlayCSS: {  
-					    backgroundColor: '#CCC',  
-					    opacity:         0.5 
-				    }  
-			    }); 
-		      
-			    if (!selectedMessages[selectedFolder.id])
-				    selectedMessages[selectedFolder.id] = {};
-			    selectedMessages[selectedFolder.id][message.msg_number] = true;
-			    
-			    var folder = selectedFolder.id.replace(dashes, '.'), mail_preview = null;
-			    
-			    if( !folder.indexOf( 'local_messages/' ) )
-			    {
-			      mail_preview = expresso_local_messages.get_local_mail( id );
-			      mail_preview.eml = expresso_local_messages.get_src( mail_preview.url_export_file );
-			    }
-			    else
-			      mail_preview = DataLayer.get( 'message', { filter: id, criteria: { properties: {context:{folder:folder}} } } );
-
-			      if (!onceOpenedMessages[selectedFolder.id])
-				      onceOpenedMessages[selectedFolder.id] = {};
-			      onceOpenedMessages[selectedFolder.id][message.msg_number] = jQuery.extend(true, message, mail_preview);
-
-			      $('#mailpreview_container').unblock();
-
-			      var tabPanelTemplateLabel = html_entities(onceOpenedMessages[selectedFolder.id][message.msg_number].subject);
-			      if (tabPanelTemplateLabel.length > $mailpreview_tabs_label_length + 3)
-				      tabPanelTemplateLabel = tabPanelTemplateLabel.substring(0, $mailpreview_tabs_label_length) + '...';
-			      selectedMessagesCount();
-			      
-			      if (!$('#' + tabPanelTemplateId).length) {								
-				      $mailpreview_tabs.tabs("add", '#' + tabPanelTemplateId, tabPanelTemplateLabel)
-				      .find('.message.empty-container').hide().end()
-				      .find('#' + tabPanelTemplateId).html(onceOpenedMessages[selectedFolder.id][message.msg_number].body)
-				      .prepend(
-					      '<div class="mailpreview-message-info">' + 
-						      get_lang('Subject') + ': ' +
-						      html_entities(onceOpenedMessages[selectedFolder.id][message.msg_number].subject) + 
-					      '</div>'
-				      )
-				      .find('[class^="ExpressoCssWrapper"]').addClass("mailpreview-message-body");
-			      } else {
-				      $mailpreview_tabs.tabs('select', '#' + tabPanelTemplateId)
-				      .find('#' + tabPanelTemplateId + ', [href="#' + tabPanelTemplateId + '"]').removeClass('preview-message-unselected');
-			      }
-		    }
-	    } else {
-		    /**
-		      * if you wants to remove tab on unselect message,
-		      * but still needs to uselect message on remove tab.
-		      * 
-		      */
-		    /*
-		    if ($('#' + tabPanelTemplateId).length) {
-			    $mailpreview_tabs.tabs('remove', '#' + tabPanelTemplateId);
-		    }
-		      */
-		    selectedMessages[selectedFolder.id][message.msg_number] = false;
-		    $mailpreview_tabs.find('#' + tabPanelTemplateId + ', [href="#' + tabPanelTemplateId + '"]').addClass('preview-message-unselected');
-	    }
-	    
-	    if (onceOpenedMessages[selectedFolder.id] && onceOpenedMessages[selectedFolder.id][message.msg_number])
-		    selectedMessagesCount();
-    },
-    caption: '<span class="'+selectedFolder['class']+'">'+selectedFolder.name+'</span>'
-});
-//.jqGrid('navGrid','#message_attacher_grid_pager',{edit:false,add:false,del:false});
-var title = [get_lang("First page"), get_lang("Prev page"), get_lang("Next page"), get_lang("Last page")];
-$("#first_message_attacher_grid_pager").attr("title",title[0]);
-$("#prev_message_attacher_grid_pager").attr("title",title[1]);
-$("#next_message_attacher_grid_pager").attr("title",title[2]);
-$("#last_message_attacher_grid_pager").attr("title",title[3]);
+
+    //jqgrid
+    jQuery("#mailgrid-container")
+    .removeClass('empty-container')
+    .html(DataLayer.render(BASE_PATH + 'api/templates/messagegrid.ejs', {}))
+    .find("#message_attacher_grid")
+    .jqGrid({
+	url: 'message:jqGrid',
+	datatype: "json",
+	mtype: 'GET',
+	colNames:['#',' ', 'De', 'Assunto', 'Data', 'Tamanho'],
+	colModel:[
+		{name:'msg_number',index:'msg_number', width:45,  sortable:false, hidden:true},
+		{name:'flags',     index:'msg_number', width:100, sortable:false, formatter:flags2Class, edittype: 'image', title :false},
+		{name:'from.name', index:'msg_number', width:70,  sortable:false, formatter:NormaliseFrom},
+		{name:'subject',   index:'subject',    width:245, sortable:false, formatter:NormaliseSubject},
+		{name:'timestamp', index:'timestamp',  width:65,  sortable:false, formatter:date2Time,   align:"center"},
+		{name:'size',      index:'size',       width:55,  sortable:false, formatter:bytes2Size,  align:"right"}
+	],
+	jsonReader : {
+		  root:"rows",
+		  page: "page",
+		  total: "total",
+		  records: "records",
+		  repeatitems: false,
+		  id: "0"
+	},
+	rowNum:10,
+	rowList:[10,25,50],
+	pager: '#message_attacher_grid_pager',
+	sortname: 'id',
+	viewrecords: true,
+	sortorder: "desc",
+	multiselect: true,
+	autowidth: true,
+	loadComplete: function(data) {
+		lastLoadedMessages = data.rows;
+		
+		// aplica o contador
+		jQuery('.timable').each(function (i) {
+			jQuery(this).countdown({
+				since: new Date(parseInt(this.title)), 
+				significant: 1,
+				layout: 'h&aacute; {d<}{dn} {dl} {d>}{h<}{hn} {hl} {h>}{m<}{mn} {ml} {m>}{s<}{sn} {sl}{s>}', 
+				description: ' atr&aacute;s'
+			});					
+		});
+		
+		// reconstrói a seleção das mensagens mesmo depois da mudança de pasta
+		if (selectedMessages[selectedFolder.id]) {
+			for (var message in selectedMessages[selectedFolder.id]){
+				for (var j=0; j<data.rows.length; j++){	
+					if (selectedMessages[selectedFolder.id][message] && message == data.rows[j].msg_number) {
+						jQuery("#message_attacher_grid").setSelection(jQuery("#message_attacher_grid").getDataIDs()[j], false);
+					}
+				}
+			}
+		}
+		$('#cb_message_attacher_grid').css('display', 'none');
+		
+	},
+	onSelectRow: function (id, selected) {
+		var message = false;
+		for (var i=0; i<lastLoadedMessages.length; i++){
+			if (lastLoadedMessages[i].msg_number == id) {
+				message = lastLoadedMessages[i];
+				break;
+			}
+		}
+		
+		var tabPanelTemplateId = 'mailpreview_tab_' + selectedFolder.id.replace(/[.\/]/g, '_') + '_' + message.msg_number;
+		var tabPanelTemplateId = tabPanelTemplateId.replace(/[\s\/]/g, '-'); 
+		
+		if (selected) {	
+			if (onceOpenedMessages[selectedFolder.id] && onceOpenedMessages[selectedFolder.id][message.msg_number]) {
+				if (!selectedMessages[selectedFolder.id])
+					selectedMessages[selectedFolder.id] = {};
+					
+				selectedMessages[selectedFolder.id][message.msg_number] = true;
+				$('#mailpreview-container').unblock();						
+
+				var tabPanelTemplateLabel = html_entities(onceOpenedMessages[selectedFolder.id][message.msg_number].subject);
+				if (tabPanelTemplateLabel.length > $mailpreview_tabs_label_length + 3)
+					tabPanelTemplateLabel = tabPanelTemplateLabel.substring(0, $mailpreview_tabs_label_length) + '...';
+				
+				if (!$('#' + tabPanelTemplateId).length) {								
+					$mailpreview_tabs.tabs("add", '#' + tabPanelTemplateId, tabPanelTemplateLabel)
+					.find('.message.empty-container').hide().end()
+					.find('#' + tabPanelTemplateId).html(onceOpenedMessages[selectedFolder.id][message.msg_number].body);
+				} else {
+					$mailpreview_tabs.tabs('select', '#' + tabPanelTemplateId)
+					.find('#' + tabPanelTemplateId + ', [href="#' + tabPanelTemplateId + '"]').removeClass('preview-message-unselected');
+				}
+				
+			} else {
+				jQuery('#mailpreview_container').block({  
+					message: '<div id="loading-content"><div class="image"></div></div>',  
+					css: {  
+						backgroundImage: 'url('+BASE_PATH+'modules/attach_message/images/loading.gif)',  
+						backgroundRepeat: 'no-repeat', 
+						backgroundPosition: 'center', 
+						backgroundColor: 'transparent', 
+						width: '32px', 
+						height: '32px', 
+						border:'none'  
+					}, 
+					overlayCSS: {  
+						backgroundColor: '#CCC',  
+						opacity:         0.5 
+					}  
+				}); 
+			  
+				if (!selectedMessages[selectedFolder.id])
+					selectedMessages[selectedFolder.id] = {};
+				selectedMessages[selectedFolder.id][message.msg_number] = true;
+				
+				var folder = selectedFolder.id.replace(dashes, '.'), mail_preview = null;
+				
+				if( !folder.indexOf( 'local_messages/' ) )
+				{
+				  mail_preview = expresso_local_messages.get_local_mail( id );
+				  mail_preview.eml = expresso_local_messages.get_src( mail_preview.url_export_file );
+				}
+				else
+				  mail_preview = DataLayer.get( 'message', { filter: id, criteria: { properties: {context:{folder:folder}} } } );
+
+				  if (!onceOpenedMessages[selectedFolder.id])
+					  onceOpenedMessages[selectedFolder.id] = {};
+				  onceOpenedMessages[selectedFolder.id][message.msg_number] = jQuery.extend(true, message, mail_preview);
+
+				  $('#mailpreview_container').unblock();
+
+				  var tabPanelTemplateLabel = html_entities(onceOpenedMessages[selectedFolder.id][message.msg_number].subject);
+				  if (tabPanelTemplateLabel.length > $mailpreview_tabs_label_length + 3)
+					  tabPanelTemplateLabel = tabPanelTemplateLabel.substring(0, $mailpreview_tabs_label_length) + '...';
+				  selectedMessagesCount();
+				  
+				  if (!$('#' + tabPanelTemplateId).length) {								
+					  $mailpreview_tabs.tabs("add", '#' + tabPanelTemplateId, tabPanelTemplateLabel)
+					  .find('.message.empty-container').hide().end()
+					  .find('#' + tabPanelTemplateId).html(onceOpenedMessages[selectedFolder.id][message.msg_number].body)
+					  .prepend(
+						  '<div class="mailpreview-message-info">' + 
+							  get_lang('Subject') + ': ' +
+							  html_entities(onceOpenedMessages[selectedFolder.id][message.msg_number].subject) + 
+						  '</div>'
+					  )
+					  .find('[class^="ExpressoCssWrapper"]').addClass("mailpreview-message-body");
+				  } else {
+					  $mailpreview_tabs.tabs('select', '#' + tabPanelTemplateId)
+					  .find('#' + tabPanelTemplateId + ', [href="#' + tabPanelTemplateId + '"]').removeClass('preview-message-unselected');
+				  }
+			}
+		} else {
+			/**
+			  * if you wants to remove tab on unselect message,
+			  * but still needs to uselect message on remove tab.
+			  * 
+			  */
+			/*
+			if ($('#' + tabPanelTemplateId).length) {
+				$mailpreview_tabs.tabs('remove', '#' + tabPanelTemplateId);
+			}
+			  */
+			selectedMessages[selectedFolder.id][message.msg_number] = false;
+			$mailpreview_tabs.find('#' + tabPanelTemplateId + ', [href="#' + tabPanelTemplateId + '"]').addClass('preview-message-unselected');
+		}
+		
+		if (onceOpenedMessages[selectedFolder.id] && onceOpenedMessages[selectedFolder.id][message.msg_number])
+			selectedMessagesCount();
+	},
+	caption: '<span class="'+selectedFolder['class']+'">'+selectedFolder.name+'</span>'
+    });
+    //.jqGrid('navGrid','#message_attacher_grid_pager',{edit:false,add:false,del:false});
+    var title = [get_lang("First page"), get_lang("Prev page"), get_lang("Next page"), get_lang("Last page")];
+    $("#first_message_attacher_grid_pager").attr("title",title[0]);
+    $("#prev_message_attacher_grid_pager").attr("title",title[1]);
+    $("#next_message_attacher_grid_pager").attr("title",title[2]);
+    $("#last_message_attacher_grid_pager").attr("title",title[3]);
+
+}, true);
 
 var $mailpreview_tabs = $( "#mailpreview_container").tabs({
