diff --git a/indra/newview/llinventorygallery.cpp b/indra/newview/llinventorygallery.cpp
index c584def3eb654779810f513af451b9ca03e3dc96..40c76a4b5f9959ea3396e97f0d6ba7b4e9c1113a 100644
--- a/indra/newview/llinventorygallery.cpp
+++ b/indra/newview/llinventorygallery.cpp
@@ -936,13 +936,13 @@ void LLInventoryGallery::updateItemThumbnail(LLUUID item_id)
 
 BOOL LLInventoryGallery::handleRightMouseDown(S32 x, S32 y, MASK mask)
 {
-    LLUUID old_selection = mSelectedItemID;
-    BOOL res = LLPanel::handleRightMouseDown(x, y, mask);
-    if (!res)
+    if(mItemMap[mSelectedItemID])
     {
-        clearSelection();
-        mItemMap[old_selection]->setFocus(false);
+        mItemMap[mSelectedItemID]->setFocus(false);
+        setFocus(true);
     }
+    clearSelection();
+    BOOL res = LLPanel::handleRightMouseDown(x, y, mask);
 
     if (mSelectedItemID.isNull())
     {