diff --git a/indra/llui/lltextbase.cpp b/indra/llui/lltextbase.cpp
index ebc9ee244e7131733c1e5372629bbfa9f63c97a7..270d5294f9f8076ae25b2c32ed1f918de5f4f09b 100644
--- a/indra/llui/lltextbase.cpp
+++ b/indra/llui/lltextbase.cpp
@@ -3201,7 +3201,23 @@ S32	LLNormalTextSegment::getNumChars(S32 num_pixels, S32 segment_offset, S32 lin
 	LLFontGL::EWordWrapStyle word_wrap_style = (line_offset == 0) 
 		? LLFontGL::WORD_BOUNDARY_IF_POSSIBLE 
 		: LLFontGL::ONLY_WORD_BOUNDARIES;
-	S32 num_chars = mStyle->getFont()->maxDrawableChars(text.c_str() + segment_offset + mStart, 
+	
+	
+	LLWString offsetString(text.c_str() + segment_offset + mStart);
+
+	if(getLength() < segment_offset + mStart)
+	{
+		llerrs << "getLength() < segment_offset + mStart\t getLength()\t" << getLength() << "\tsegment_offset:\t" 
+						<< segment_offset << "\tmStart:\t" << mStart << "\tsegments\t" << mEditor.mSegments.size() << "\tmax_chars\t" << max_chars << llendl;
+	}
+
+	if(offsetString.length() + 1 < max_chars)
+	{
+		llerrs << "offsetString.length() + 1 < max_chars\t max_chars:\t" << max_chars << "\toffsetString.length():\t" << offsetString.length()
+			<< getLength() << "\tsegment_offset:\t" << segment_offset << "\tmStart:\t" << mStart << "\tsegments\t" << mEditor.mSegments.size() << llendl;
+	}
+	
+	S32 num_chars = mStyle->getFont()->maxDrawableChars(offsetString.c_str(), 
 												(F32)num_pixels,
 												max_chars, 
 												word_wrap_style);
diff --git a/indra/newview/app_settings/settings_per_account.xml b/indra/newview/app_settings/settings_per_account.xml
index ada374f892d7e500a2c527e4bccf18885f8ab0b4..590f41283b68eb9d94c04af1ae5c00dae70c77a0 100644
--- a/indra/newview/app_settings/settings_per_account.xml
+++ b/indra/newview/app_settings/settings_per_account.xml
@@ -292,6 +292,17 @@
       <key>Value</key>
       <integer>1</integer>
     </map>
+    <key>NearbyChatIsNotCollapsed</key>
+    <map>
+      <key>Comment</key>
+      <string>Saving expanded/collapsed state of the nearby chat between sessions</string>
+      <key>Persist</key>
+      <integer>1</integer>
+      <key>Type</key>
+      <string>Boolean</string>
+      <key>Value</key>
+      <integer>0</integer>
+    </map>
     <key>ShowFavoritesOnLogin</key>
         <map>
         <key>Comment</key>
diff --git a/indra/newview/llchiclet.cpp b/indra/newview/llchiclet.cpp
index b221daf9365283c724366b9551b2b0c5419ff39c..43c6b558bc920e7d6cd7199a7fac6e5eb2fc3881 100644
--- a/indra/newview/llchiclet.cpp
+++ b/indra/newview/llchiclet.cpp
@@ -174,6 +174,7 @@ void LLNotificationChiclet::onMenuItemClicked(const LLSD& user_data)
 	if("close all" == action)
 	{
 		LLNotificationWellWindow::getInstance()->closeAll();
+		LLIMWellWindow::getInstance()->closeAll();
 	}
 }
 
diff --git a/indra/newview/llfloaterimcontainer.cpp b/indra/newview/llfloaterimcontainer.cpp
index 0cda1f79b348d548fa7e3c03ae69b270baaa1dc5..7296ec3cedadf17b12a498628b06895c1a7d2e59 100644
--- a/indra/newview/llfloaterimcontainer.cpp
+++ b/indra/newview/llfloaterimcontainer.cpp
@@ -1337,7 +1337,10 @@ void LLFloaterIMContainer::showConversation(const LLUUID& session_id)
     selectConversationPair(session_id, true);
 
     LLFloaterIMSessionTab* session_floater = LLFloaterIMSessionTab::findConversation(session_id);
-    session_floater->restoreFloater();
+    if (session_floater)
+    {
+        session_floater->restoreFloater();
+    }
 }
 
 void LLFloaterIMContainer::clearAllFlashStates()
diff --git a/indra/newview/llfloaterimsessiontab.cpp b/indra/newview/llfloaterimsessiontab.cpp
index 0333bacd58931d6026ba6b2394dc3e0e30cb2608..64c1f500741ccb81c29d2d2a2fc533bfce640b78 100644
--- a/indra/newview/llfloaterimsessiontab.cpp
+++ b/indra/newview/llfloaterimsessiontab.cpp
@@ -132,6 +132,12 @@ void LLFloaterIMSessionTab::setVisible(BOOL visible)
 			LLFloaterReg::getTypedInstance<LLFloaterIMContainer>("im_container")->setVisible(true);
 		}
 		LLFloaterIMSessionTab::addToHost(mSessionID);
+		LLFloaterIMSessionTab* conversp = LLFloaterIMSessionTab::getConversation(mSessionID);
+
+		if (conversp && conversp->isNearbyChat() && gSavedPerAccountSettings.getBOOL("NearbyChatIsNotCollapsed"))
+		{
+			onCollapseToLine(this);
+		}
 		mInputButtonPanel->setVisible(isTornOff());
 	}
 
@@ -356,7 +362,7 @@ void LLFloaterIMSessionTab::draw()
 		// Restart the refresh timer
 		mRefreshTimer->setTimerExpirySec(REFRESH_INTERVAL);
 	}
-	
+
 	LLTransientDockableFloater::draw();
 }
 
@@ -866,7 +872,7 @@ void LLFloaterIMSessionTab::reshapeFloater(bool collapse)
 		enableResizeCtrls(true, true, true);
 
 	}
-
+	saveCollapsedState();
 	setShape(floater_rect, true);
 	mBodyStack->updateLayout();
 
@@ -874,8 +880,9 @@ void LLFloaterIMSessionTab::reshapeFloater(bool collapse)
 
 void LLFloaterIMSessionTab::restoreFloater()
 {
-	if(!isMessagePaneExpanded())
+	if(checkIfTornOff() && !isMessagePaneExpanded())
 	{
+
 		if(isMinimized())
 		{
 			setMinimized(false);
@@ -888,6 +895,7 @@ void LLFloaterIMSessionTab::restoreFloater()
 		mBodyStack->updateLayout();
 		mExpandCollapseLineBtn->setImageOverlay(getString("expandline_icon"));
 		setMessagePaneExpanded(true);
+		saveCollapsedState();
 		enableResizeCtrls(true, true, true);
 	}
 }
@@ -1063,6 +1071,14 @@ LLConversationItem* LLFloaterIMSessionTab::getCurSelectedViewModelItem()
 	return conversationItem;
 }
 
+void LLFloaterIMSessionTab::saveCollapsedState()
+{
+	LLFloaterIMSessionTab* conversp = LLFloaterIMSessionTab::getConversation(mSessionID);
+	if(conversp->isNearbyChat())
+	{
+		gSavedPerAccountSettings.setBOOL("NearbyChatIsNotCollapsed", isMessagePaneExpanded());
+	}
+}
 BOOL LLFloaterIMSessionTab::handleKeyHere(KEY key, MASK mask )
 {
 	if(mask == MASK_ALT)
diff --git a/indra/newview/llfloaterimsessiontab.h b/indra/newview/llfloaterimsessiontab.h
index f0899a3c0934ae0d1fa6ded9c02c18880c69efc4..c7e73bd70d2838c6eb245b15518adc0f6f208606 100644
--- a/indra/newview/llfloaterimsessiontab.h
+++ b/indra/newview/llfloaterimsessiontab.h
@@ -101,6 +101,7 @@ class LLFloaterIMSessionTab
 	bool isMessagePaneExpanded(){return mMessagePaneExpanded;}
 	void setMessagePaneExpanded(bool expanded){mMessagePaneExpanded = expanded;}
 	void restoreFloater();
+	void saveCollapsedState();
 
 protected:
 
diff --git a/indra/newview/llimview.cpp b/indra/newview/llimview.cpp
index 8c862548bbc5ac2bdb3f986cd2c27200324722b3..ca04d27d1133032ce4ddfbb7a9b424628fd0b7cb 100644
--- a/indra/newview/llimview.cpp
+++ b/indra/newview/llimview.cpp
@@ -160,163 +160,152 @@ static void on_avatar_name_cache_toast(const LLUUID& agent_id,
 
 void on_new_message(const LLSD& msg)
 {
-    std::string action;
+    std::string user_preferences;
     LLUUID participant_id = msg["from_id"].asUUID();
     LLUUID session_id = msg["session_id"].asUUID();
     LLIMModel::LLIMSession* session = LLIMModel::instance().findIMSession(session_id);
 
-    //  determine action for this session
+    // do not show notification which goes from agent
+    if (gAgent.getID() == participant_id)
+    {
+        return;
+    }
+
+    // determine state of conversations floater
+    enum {CLOSED, NOT_ON_TOP, ON_TOP, ON_TOP_AND_ITEM_IS_SELECTED} conversations_floater_status;
+
+    LLFloaterIMContainer* im_box = LLFloaterReg::getTypedInstance<LLFloaterIMContainer>("im_container");
+	LLFloaterIMSessionTab* session_floater = LLFloaterIMSessionTab::getConversation(session_id);
+
+	if (!LLFloater::isVisible(im_box) || im_box->isMinimized())
+	{
+		conversations_floater_status = CLOSED;
+	}
+	else if (!im_box->isFrontmost() &&
+			(!session_floater || !LLFloater::isVisible(session_floater)
+	            || session_floater->isMinimized() || !session_floater->isFrontmost()))
+	{
+		conversations_floater_status = NOT_ON_TOP;
+	}
+	else if (im_box->getSelectedSession() != session_id)
+	{
+		conversations_floater_status = ON_TOP;
+    }
+	else
+	{
+		conversations_floater_status = ON_TOP_AND_ITEM_IS_SELECTED;
+	}
 
+    //  determine user prefs for this session
     if (session_id.isNull())
     {
-        action = gSavedSettings.getString("NotificationNearbyChatOptions");
+    	user_preferences = gSavedSettings.getString("NotificationNearbyChatOptions");
     }
     else if(session->isP2PSessionType())
     {
         if (LLAvatarTracker::instance().isBuddy(participant_id))
         {
-            action = gSavedSettings.getString("NotificationFriendIMOptions");
+        	user_preferences = gSavedSettings.getString("NotificationFriendIMOptions");
         }
         else
         {
-            action = gSavedSettings.getString("NotificationNonFriendIMOptions");
+        	user_preferences = gSavedSettings.getString("NotificationNonFriendIMOptions");
         }
     }
     else if(session->isAdHocSessionType())
     {
-        action = gSavedSettings.getString("NotificationConferenceIMOptions");
+    	user_preferences = gSavedSettings.getString("NotificationConferenceIMOptions");
     }
     else if(session->isGroupSessionType())
     {
-        action = gSavedSettings.getString("NotificationGroupChatOptions");
+    	user_preferences = gSavedSettings.getString("NotificationGroupChatOptions");
     }
 
-    // do not show notification which goes from agent
-    if (gAgent.getID() == participant_id)
-    {
-        return;
-    }
-
-    // execution of the action
-
-    LLFloaterIMContainer* im_box = LLFloaterReg::getTypedInstance<LLFloaterIMContainer>("im_container");
-	LLFloaterIMSessionTab* session_floater = LLFloaterIMSessionTab::getConversation(session_id);
-	
-	if (im_box->isFrontmost() && im_box->getSelectedSession() == session_id
-		&& !(session_floater->getHost() ? im_box->isMinimized() : session_floater->isMinimized()))
-	{
-		return;
-	}
-	
-    //session floater not focused (visible or not)
-    bool session_floater_not_focused = session_floater && !session_floater->hasFocus();
-
-    //conv. floater is closed
-    bool conversation_floater_is_closed =
-    		!(  im_box
-    		    && im_box->isInVisibleChain()
-                && !im_box->isMinimized());
-
-    //conversation floater not focused (visible or not)
-    bool conversation_floater_not_focused =
-    		conversation_floater_is_closed || !im_box->hasFocus();
-    // sess. floater is open
-    bool session_floater_is_open =
-            session_floater
-            && session_floater->isInVisibleChain()
-            && !session_floater->isMinimized()
-            && !(session_floater->getHost() && session_floater->getHost()->isMinimized());
-
-    bool conversation_floater_collapsed = !session_floater->isMessagePaneExpanded();
-    if (("toast" == action && !session_floater_is_open) || conversation_floater_collapsed)
-    {
-        //User is not focused on conversation containing the message
-        if(session_floater_not_focused || conversation_floater_collapsed)
-        {
-        	if(!LLMuteList::getInstance()->isMuted(participant_id))
-        	{
-        		im_box->flashConversationItemWidget(session_id, true);
-        	}
-            //The conversation floater isn't focused/open
-            if(conversation_floater_not_focused || conversation_floater_collapsed)
-            {
-            	if(!LLMuteList::getInstance()->isMuted(participant_id) 
-                    && !gAgent.isDoNotDisturb())
-            	{
-            		gToolBarView->flashCommand(LLCommandId("chat"), true);
-            	}
-
-                //Show IM toasts (upper right toasts)
-                // Skip toasting for system messages and for nearby chat
-                if(session_id.notNull() && participant_id.notNull())
-                {
-                    LLAvatarNameCache::get(participant_id, boost::bind(&on_avatar_name_cache_toast, _1, _2, msg));
-                }
-            }
-		}
-    }
+    // actions:
 
-    else if ("flash" == action)
+    // 0. nothing - exit
+    if (("none" == user_preferences ||
+    		ON_TOP_AND_ITEM_IS_SELECTED == conversations_floater_status)
+    	&& session_floater->isMessagePaneExpanded())
     {
-    	if (!gAgent.isDoNotDisturb())
-    	{
-			im_box->flashConversationItemWidget(session_id, true);
-			if(conversation_floater_not_focused)
-			{
-				//User is not focused on conversation containing the message
-				gToolBarView->flashCommand(LLCommandId("chat"), true);
-			}
-		}
-		else if(session_id.notNull() && participant_id.notNull())
-		{
-			//If a DND message, allow notification to be stored so upon DND exit 
-			//useMostItrusiveIMNotification will be called to notify user a message exists
-			LLAvatarNameCache::get(participant_id, boost::bind(&on_avatar_name_cache_toast, _1, _2, msg));
-		}
+    	return;
     }
 
-    else if("openconversations" == action)
+    // 1. open floater and [optional] surface it
+    if ("openconversations" == user_preferences &&
+    		(CLOSED == conversations_floater_status
+    				|| NOT_ON_TOP == conversations_floater_status))
     {
-        //User is not focused on conversation containing the message
-        if(session_floater_not_focused)
+    	if(!gAgent.isDoNotDisturb())
         {
-            //Flash line item
-            im_box->flashConversationItemWidget(session_id, true);
-
-            if(!gAgent.isDoNotDisturb())
-            {
-				//Surface conversations floater
-				LLFloaterReg::showInstance("im_container");
-				im_box->collapseMessagesPane(false);
-				if (session_floater)
+			// Open conversations floater
+			LLFloaterReg::showInstance("im_container");
+			im_box->collapseMessagesPane(false);
+			if (session_floater)
+			{
+				if (session_floater->getHost())
 				{
-					if (session_floater->getHost())
+					if (NULL != im_box && im_box->isMinimized())
 					{
-						if (NULL != im_box && im_box->isMinimized())
-						{
-							LLFloater::onClickMinimize(im_box);
-						}
+						LLFloater::onClickMinimize(im_box);
 					}
-					else
+				}
+				else
+				{
+					if (session_floater->isMinimized())
 					{
-						if (session_floater->isMinimized())
-						{
-							LLFloater::onClickMinimize(session_floater);
-						}
+						LLFloater::onClickMinimize(session_floater);
 					}
 				}
 			}
-
-            //If in DND mode, allow notification to be stored so upon DND exit 
+		}
+        else
+        {
+            //If in DND mode, allow notification to be stored so upon DND exit
             //useMostItrusiveIMNotification will be called to notify user a message exists
-            if(session_id.notNull() 
-                && participant_id.notNull()
-                && gAgent.isDoNotDisturb()
-				&& !session_floater_is_open)
+            if(session_id.notNull()
+               && participant_id.notNull()
+		       && !session_floater->isShown())
             {
                 LLAvatarNameCache::get(participant_id, boost::bind(&on_avatar_name_cache_toast, _1, _2, msg));
-			}
-		}
+	        }
+        }
+    }
+
+    // 2. Flash line item
+    if ("openconversations" == user_preferences
+    		|| NOT_ON_TOP == conversations_floater_status)
+    {
+    	if(!LLMuteList::getInstance()->isMuted(participant_id))
+    	{
+    		im_box->flashConversationItemWidget(session_id, true);
+    	}
+    }
+
+    // 3. Flash FUI button
+    if ("flash" == user_preferences &&
+    		(CLOSED == conversations_floater_status
+    		    || NOT_ON_TOP == conversations_floater_status))
+    {
+    	if(!LLMuteList::getInstance()->isMuted(participant_id)
+            && !gAgent.isDoNotDisturb())
+    	{
+    		gToolBarView->flashCommand(LLCommandId("chat"), true);
+    	}
+    }
+
+    // 4. Toast
+    if (("toast" == user_preferences &&
+    		(CLOSED == conversations_floater_status
+    		    || NOT_ON_TOP == conversations_floater_status))
+    		    || !session_floater->isMessagePaneExpanded())
+    {
+        //Show IM toasts (upper right toasts)
+        // Skip toasting for system messages and for nearby chat
+        if(session_id.notNull() && participant_id.notNull())
+        {
+            LLAvatarNameCache::get(participant_id, boost::bind(&on_avatar_name_cache_toast, _1, _2, msg));
+        }
     }
 }