Index: /CKEditor/trunk/CHANGES.html
===================================================================
--- /CKEditor/trunk/CHANGES.html	(revision 7367)
+++ /CKEditor/trunk/CHANGES.html	(revision 7368)
@@ -49,4 +49,5 @@
 		<li><a href="http://dev.ckeditor.com/ticket/8699">#8699</a> : Now focus is moved to clicked button in all browsers and back to colorpicker dialog opener (button) after dialog is closed.</li>
 		<li><a href="http://dev.ckeditor.com/ticket/8132">#8132</a> : [IE9] Fix link line get broken when containing BRs.</li>
+		<li><a href="http://dev.ckeditor.com/ticket/8629">#8629</a> : Optimized float panel layout when there's no enough spaces in viewport.</li>
 	</ul>
 	<h3>
Index: /CKEditor/trunk/_source/plugins/floatpanel/plugin.js
===================================================================
--- /CKEditor/trunk/_source/plugins/floatpanel/plugin.js	(revision 7367)
+++ /CKEditor/trunk/_source/plugins/floatpanel/plugin.js	(revision 7368)
@@ -199,7 +199,4 @@
 				CKEDITOR.tools.setTimeout( function()
 					{
-						if ( rtl )
-							left -= element.$.offsetWidth;
-
 						var panelLoad = CKEDITOR.tools.bind( function ()
 						{
@@ -248,22 +245,70 @@
 								target.removeStyle( 'height' );
 
+							// Flip panel layout horizontally in RTL with known width.
+							if ( rtl )
+								left -= element.$.offsetWidth;
+
+							// Pop the style now for measurement.
+							element.setStyle( 'left', left + 'px' );
+
+							/* panel layout smartly fit the viewport size. */
 							var panelElement = panel.element,
 								panelWindow = panelElement.getWindow(),
-								windowScroll = panelWindow.getScrollPosition(),
-								viewportSize = panelWindow.getViewPaneSize(),
-								panelSize =
+								rect = element.$.getBoundingClientRect(),
+								viewportSize = panelWindow.getViewPaneSize();
+
+							// Compensation for browsers that dont support "width" and "height".
+							var rectWidth = rect.width || rect.right - rect.left,
+							rectHeight = rect.height || rect.bottom - rect.top;
+
+							// Check if default horizontal layout is impossible.
+							var spaceAfter = rtl ? rect.right : viewportSize.width - rect.left,
+								spaceBefore = rtl ? viewportSize.width - rect.right : rect.left;
+
+							if ( rtl )
+							{
+								if ( spaceAfter < rectWidth )
 								{
-									'height' : panelElement.$.offsetHeight,
-									'width' : panelElement.$.offsetWidth
-								};
-
-							// If the menu is horizontal off, shift it toward
-							// the opposite language direction.
-							if ( rtl ? left < 0 : left + panelSize.width > viewportSize.width + windowScroll.x )
-								left += ( panelSize.width * ( rtl ? 1 : -1 ) );
-
-							// Vertical off screen is simpler.
-							if ( top + panelSize.height > viewportSize.height + windowScroll.y )
-								top -= panelSize.height;
+									// Flip to show on right.
+									if ( spaceBefore > rectWidth )
+										left += rectWidth;
+									// Align to window left.
+									else if ( viewportSize.width > rectWidth )
+										left = left - rect.left;
+									// Align to window right, never cutting the panel at right.
+									else
+										left = left - rect.right + viewportSize.width;
+								}
+							}
+							else if ( spaceAfter < rectWidth )
+							{
+									// Flip to show on left.
+									if ( spaceBefore > rectWidth )
+										left -= rectWidth;
+									// Align to window right.
+									else if ( viewportSize.width > rectWidth )
+										left = left - rect.right + viewportSize.width;
+									// Align to window left, never cutting the panel at left.
+									else
+										left = left - rect.left;
+								}
+
+
+							// Check if the default vertical layout is possible.
+							var spaceBelow = viewportSize.height - rect.top,
+								spaceAbove = rect.top;
+
+							if ( spaceBelow < rectHeight )
+							{
+								// Flip to show above.
+								if ( spaceAbove > rectHeight )
+									top -= rectHeight;
+								// Align to window bottom.
+								else if ( viewportSize.height > rectHeight )
+									top = top - rect.bottom + viewportSize.height;
+								// Align to top, never cutting the panel at top.
+								else
+									top = top - rect.top;
+							}
 
 							// If IE is in RTL, we have troubles with absolute
