Index: CKEditor/trunk/_source/skins/kama/skin.js
===================================================================
--- CKEditor/trunk/_source/skins/kama/skin.js	(revision 4036)
+++ CKEditor/trunk/_source/skins/kama/skin.js	(revision 4180)
@@ -26,13 +26,7 @@
 				editor.config.width -= 12;
 
-			var menuHead;
-			function menuSetUiColor( color )
-			{
-				if ( !menuHead )
-					return null;
-
-				var uiStyle = menuHead.append('style');
-
-				var cssSrc = "/* UI Color Support */\
+			var uiColorMenus = [];
+			var uiColorRegex = /\$color/g;
+			var uiColorMenuCss = "/* UI Color Support */\
 .cke_skin_kama .cke_menuitem .cke_icon_wrapper\
 {\
@@ -88,37 +82,55 @@
 	background-color: $color !important;\
 }";
-
-				uiStyle.setAttribute( "type", "text/css" );
-				var regex = /\$color/g;
-
-				// We have to split CSS declarations for webkit.
-				if ( CKEDITOR.env.webkit )
-				{
-					cssSrc = cssSrc.split( '}' ).slice( 0, -1 );
-					for ( var i in cssSrc )
-							cssSrc[ i ] = cssSrc[ i ].split( '{' );
+			// We have to split CSS declarations for webkit.
+			if ( CKEDITOR.env.webkit )
+			{
+				uiColorMenuCss = uiColorMenuCss.split( '}' ).slice( 0, -1 );
+				for ( var i in uiColorMenuCss )
+						uiColorMenuCss[ i ] = uiColorMenuCss[ i ].split( '{' );
+			}
+
+			function addStylesheet( document )
+			{
+				var node = document.getHead().append( 'style' );
+				node.setAttribute( "id", "cke_ui_color" );
+				node.setAttribute( "type", "text/css" );
+				
+				return node;
+			}
+			
+			function updateStylesheets( styleNodes, styleContent, replace )
+			{
+				for ( var id in styleNodes )
+				{
+					if ( CKEDITOR.env.webkit )
+					{
+						// Truncate manually.
+						for ( var i = 0 ; i < styleNodes[ id ].$.sheet.rules.length ; i++ )
+							styleNodes[ id ].$.sheet.removeRule( i );
+
+						for ( var i in styleContent )
+						{
+							var content = styleContent[ i ][ 1 ];
+							for ( var r in replace )
+								content = content.replace( replace[ r ][ 0 ], replace[ r ][ 1 ] );
+
+							styleNodes[ id ].$.sheet.addRule( styleContent[ i ][ 0 ], content );
+						}
+					}
+					else
+					{
+						var content = styleContent;
+						for ( var r in replace )
+							content = content.replace( replace[ r ][ 0 ], replace[ r ][ 1 ] );
+
+						if ( CKEDITOR.env.ie )
+							styleNodes[ id ].$.styleSheet.cssText = content;
+						else
+							styleNodes[ id ].setHtml( content );
+					}
 				}
-
-				return ( menuSetUiColor =
-					function( color )
-					{
-						if ( CKEDITOR.env.webkit )
-						{
-							for ( var i in cssSrc )
-								uiStyle.$.sheet.addRule(
-									cssSrc[ i ][ 0 ], cssSrc[ i ][ 1 ].replace( regex, color )
-								);
-						}
-						else
-						{
-							var css = cssSrc.replace( regex, color );
-
-							if ( CKEDITOR.env.ie )
-								uiStyle.$.styleSheet.cssText = css;
-							else
-								uiStyle.setHtml( css );
-						}
-					})( color );
 			}
+
+			var uiColorRegexp = /\$color/g;
 
 			CKEDITOR.tools.extend( editor,
@@ -133,5 +145,5 @@
 				setUiColor : function( color )
 				{
-					var uiStyle = CKEDITOR.document.getHead().append('style'),
+					var uiStyle = addStylesheet( CKEDITOR.document ),
 						cssId = '#cke_' + editor.name.replace('.', '\\.');
 
@@ -145,74 +157,45 @@
 					var cssProperties = "background-color: $color !important;";
 
-					uiStyle.setAttribute("type", "text/css");
+					if ( CKEDITOR.env.webkit )
+						var cssContent = [ [ cssSelectors, cssProperties ] ];
+					else
+						var cssContent = cssSelectors + '{' + cssProperties + '}';
 
 					return ( this.setUiColor =
 						function( color )
 						{
-							var css = cssProperties.replace( '$color', color );
+							var replace = [ [ uiColorRegexp, color ] ];
 							editor.uiColor = color;
 
-							if ( CKEDITOR.env.ie )
-								uiStyle.$.styleSheet.cssText = cssSelectors + '{' + css + '}';
-							else if ( CKEDITOR.env.webkit )
-								uiStyle.$.sheet.addRule( cssSelectors, css );
-							else
-								uiStyle.setHtml( cssSelectors + '{' + css + '}' );
-
-							menuSetUiColor( color );
+							// Update general style.
+							updateStylesheets( [ uiStyle ], cssContent, replace );
+
+							// Update menu styles.
+							updateStylesheets( uiColorMenus, uiColorMenuCss, replace );
 						})( color );
 				}
 			});
 
-			// If the "menu" plugin is loaded, register the listeners.
-			if ( CKEDITOR.menu )
-			{
-				var old = CKEDITOR.menu.prototype.show;
-
-				CKEDITOR.menu.prototype.show = function()
-				{
-					old.apply( this, arguments );
-
-					if ( !menuHead && editor == this.editor )
-					{
-						// Save reference.
-						menuHead = this._.element.getDocument().getHead();
-						menuSetUiColor( editor.getUiColor() );
-					}
-				};
-			}
+			editor.on( 'menuShow', function( event )
+			{
+				var panel = event.data[ 0 ];
+				var iframe = panel.element.getElementsByTag( 'iframe' ).getItem( 0 ).getFrameDocument();
+
+				// Add stylesheet if missing.
+				if ( !iframe.getById( 'cke_ui_color' ) )
+				{
+					var node = addStylesheet( iframe );
+					uiColorMenus.push( node );
+
+					var color = editor.getUiColor();
+					// Set uiColor for new menu.
+					if ( color )
+						updateStylesheets( [ node ], uiColorMenuCss, [ [ uiColorRegexp, color ] ] );
+				}
+			});
 
 			// Apply UI color if specified in config.
 			if ( editor.config.uiColor )
 				editor.setUiColor( editor.config.uiColor );
-
-			// Fix editor's width. HPadding and 100% width iframe issue.
-//			if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )
-//			{
-//				editor.on( 'mode', function( event )
-//				{
-//					var container = editor.getResizable();
-//					editor.resize( container.$.offsetWidth-10, container.$.offsetHeight );
-//					event.removeListener();
-//				});
-//			}
-
-//			if ( CKEDITOR.env.ie && ( CKEDITOR.env.quirks || CKEDITOR.env.version < 7 ) )
-//			{
-//				editor.on( 'themeLoaded', function( event )
-//				{
-//					var toolbars = editor.container.getChild( [0, 0, 0, 0, 0, 0, 0] ).getChildren();
-//					for ( var i = 0 ; i < toolbars.count() ; i++ )
-//					{
-//						var toolbar = toolbars.getItem( i );
-
-//						var last = toolbar.getLast();
-//						if ( !last || !last.getPrevious().hasClass( 'cke_rcombo' ) )
-//							continue;
-//
-//						last.addClass( 'cke_toolbar_end_last' );
-//					}
-//				});
-//			}
 		}
 	};
