Index: /CKEditor/branches/prototype/_source/core/ckeditor.js
===================================================================
--- /CKEditor/branches/prototype/_source/core/ckeditor.js	(revision 2090)
+++ /CKEditor/branches/prototype/_source/core/ckeditor.js	(revision 2091)
@@ -22,100 +22,28 @@
 CKEDITOR.instances = {};
 
-(function()
+// Overwrite the basic _replaceElement implementation with the definitive one.
+CKEDITOR.replace._replaceElement = function( textarea, config )
 {
-	// These function loads custom configuration files and cache the
-	// CKEDITOR.editorConfig functions defined on them, so there is no need to
-	// download them more than once for several instances.
-	var loadConfigLoaded = {};
-	var loadConfig = function( editor )
-	{
-		var customConfig = editor.config.customConfig;
+	// Encapsulates the original DOM textarea in a CKEDITOR.dom.element
+	// instance.
+	textarea = new CKEDITOR.dom.element( textarea );
+	
+	// Create the editor instance.
+	CKEDITOR.add( new CKEDITOR.editor( textarea, config ) );
+};
 
-		// Check if there is a custom config to load.
-		if ( !customConfig )
-			return false;
+CKEDITOR.add = function( editor )
+{
+	var name = editor.name;
 
-		var loadedConfig = loadConfigLoaded[ customConfig ] || ( loadConfigLoaded[ customConfig ] = { editors : [] } );
+	// Abort it there is already an instance with that name.
+	if ( CKEDITOR.instances[ name ] )
+		return;
 
-		// If the custom config has already been downloaded, reuse it.
-		if ( loadedConfig.fn )
-		{
-			// Call the cached CKEDITOR.editorConfig defined in the custom
-			// config file for the editor instance depending on it.
-			loadedConfig.fn.call( editor, editor );
+	CKEDITOR.instances[ name ] = editor;
 
-			// If there is no other customConfig in the chain, fire the
-			// "configloaded" event.
-			if ( editor.config.customConfig == customConfig || !loadConfig( editor ) )
-				editor.fireOnce( 'configloaded' );
-		}
-		else
-		{
-			// Add the editor to the list of editors waiting for this config.
-			loadedConfig.editors.push( editor );
-
-			// Load the custom configuration file.
-			CKEDITOR.scriptLoader.load( customConfig, function()
-				{
-					// If the CKEDITOR.editorConfig function has been properly
-					// defined in the custom configuration file, cache it.
-					if ( CKEDITOR.editorConfig )
-						loadedConfig.fn = CKEDITOR.editorConfig;
-					else
-						loadedConfig.fn = function(){};
-
-					delete CKEDITOR.editorConfig;
-
-					CKEDITOR.tools.each( loadedConfig.editors, function( editor )
-						{
-							// Call the load config again. This time the custom
-							// config is already cached and so it will get loaded.
-							loadConfig( editor );
-						});
-
-					delete loadedConfig.editors;
-				} );
-		}
-
-		return true;
-	};
-
-	// Overwrite the basic _replaceElement implementation with the definitive one.
-	CKEDITOR.replace._replaceElement = function( textarea, config )
-	{
-		var textarea = new CKEDITOR.dom.element( textarea );
-
-		// Create the editor instance.
-		var editor = new CKEDITOR.editor( textarea );
-
-		// Abort it there is already an instance with that name.
-		if ( CKEDITOR.instances[ editor.name ] )
-			return;
-
-		textarea.hide();
-
-		CKEDITOR.instances[ editor.name ] = editor;
-
-		// Setup the lister for the "configloaded" event.
-		editor.on( 'configloaded', function()
-			{
-				// Overwrite the settings from the in-page config.
-				if ( config )
-					CKEDITOR.tools.extend( editor.config, config, true );
-
-				// Fire the "instancecreated" event.
-				CKEDITOR.fire( 'instancecreated', editor.name, editor );
-			});
-
-		// The instance config may override the customConfig setting to avoid
-		// loading the default ~/config.js file.
-		if ( config && config.customConfig != undefined )
-			editor.config.customConfig = config.customConfig;
-
-		// Load configs from the custom configuration files.
-		if ( !loadConfig( editor ) )
-			editor.fireOnce( 'configloaded' );
-	};
-})();
+	CKEDITOR.fire( 'instancecreated', name, editor );
+	editor.fireOnce( 'instancecreated' );
+};
 
 // Set the status to "loading", which means that the main CKEDITOR object has
Index: /CKEditor/branches/prototype/_source/core/editor.js
===================================================================
--- /CKEditor/branches/prototype/_source/core/editor.js	(revision 2090)
+++ /CKEditor/branches/prototype/_source/core/editor.js	(revision 2091)
@@ -36,11 +36,91 @@
 		return CKEDITOR.instances[ name ] ? getNewName() : name;
 	};
-	
+
+	// These function loads custom configuration files and cache the
+	// CKEDITOR.editorConfig functions defined on them, so there is no need to
+	// download them more than once for several instances.
+	var loadConfigLoaded = {};
+	var loadConfig = function( editor )
+	{
+		var customConfig = editor.config.customConfig;
+
+		// Check if there is a custom config to load.
+		if ( !customConfig )
+			return false;
+
+		var loadedConfig = loadConfigLoaded[ customConfig ] || ( loadConfigLoaded[ customConfig ] = { editors : [] } );
+
+		// If the custom config has already been downloaded, reuse it.
+		if ( loadedConfig.fn )
+		{
+			// Call the cached CKEDITOR.editorConfig defined in the custom
+			// config file for the editor instance depending on it.
+			loadedConfig.fn.call( editor, editor );
+
+			// If there is no other customConfig in the chain, fire the
+			// "configloaded" event.
+			if ( editor.config.customConfig == customConfig || !loadConfig( editor ) )
+				editor.fireOnce( 'customconfigloaded' );
+		}
+		else
+		{
+			// Add the editor to the list of editors waiting for this config.
+			loadedConfig.editors.push( editor );
+
+			// Load the custom configuration file.
+			CKEDITOR.scriptLoader.load( customConfig, function()
+				{
+					// If the CKEDITOR.editorConfig function has been properly
+					// defined in the custom configuration file, cache it.
+					if ( CKEDITOR.editorConfig )
+						loadedConfig.fn = CKEDITOR.editorConfig;
+					else
+						loadedConfig.fn = function(){};
+
+					delete CKEDITOR.editorConfig;
+
+					CKEDITOR.tools.each( loadedConfig.editors, function( editor )
+						{
+							// Call the load config again. This time the custom
+							// config is already cached and so it will get loaded.
+							loadConfig( editor );
+						});
+
+					delete loadedConfig.editors;
+				} );
+		}
+
+		return true;
+	};
+
+	var initConfig = function( editor, instanceConfig )
+	{
+		// Setup the lister for the "customconfigloaded" event.
+		editor.on( 'customconfigloaded', function()
+			{
+				// Overwrite the settings from the in-page config.
+				if ( instanceConfig )
+					CKEDITOR.tools.extend( editor.config, instanceConfig, true );
+
+				// Fire the "configloaded" event.
+				editor.fire( 'configloaded' );
+			});
+
+		// The instance config may override the customConfig setting to avoid
+		// loading the default ~/config.js file.
+		if ( instanceConfig && instanceConfig.customConfig != undefined )
+			editor.config.customConfig = instanceConfig.customConfig;
+
+		// Load configs from the custom configuration files.
+		if ( !loadConfig( editor ) )
+			editor.fireOnce( 'customconfigloaded' );
+	};
+
 	// Basic config class to inherit the default settings from CKEDITOR.config.
 	var config = function()
 	{}
 	config.prototype = CKEDITOR.config;
-	
-	return function( element )
+
+	return function( element, instanceConfig )
 	{
 		// Call the base constructor.
@@ -49,7 +129,14 @@
 		this.element = element;
 		this.name = element.getId() || element.getNameAtt() || getNewName();
-		
+
 		// Get the default settings.
 		this.config = new config();
+
+		// Call initConfig using events, to be sure that instancecreated is
+		// fired first.
+		this.on( 'instancecreated', function()
+			{
+				initConfig( this, instanceConfig );
+			});
 	};
 }());
@@ -64,5 +151,5 @@
 		return CKEDITOR.event.prototype.fire.call( this, eventName, data, this );
 	},
-	
+
 	fireOnce : function( eventName, data )
 	{
Index: /CKEditor/branches/prototype/_source/plugins/themes/default.js
===================================================================
--- /CKEditor/branches/prototype/_source/plugins/themes/default.js	(revision 2090)
+++ /CKEditor/branches/prototype/_source/plugins/themes/default.js	(revision 2091)
@@ -22,7 +22,7 @@
 CKEDITOR.theme.add( 'default',
 {
-	build : function { instance )
+	build : function { editor )
 	{
-		
+		editor.element.hide();
 	}
 });
Index: /CKEditor/branches/prototype/_source/tests/core/_editor/custom_config_1.js
===================================================================
--- /CKEditor/branches/prototype/_source/tests/core/_editor/custom_config_1.js	(revision 2091)
+++ /CKEditor/branches/prototype/_source/tests/core/_editor/custom_config_1.js	(revision 2091)
@@ -0,0 +1,27 @@
+/*
+ * CKEditor - The text editor for Internet - http://ckeditor.com
+ * Copyright (C) 2003-2008 Frederico Caldeira Knabben
+ *
+ * == BEGIN LICENSE ==
+ *
+ * Licensed under the terms of any of the following licenses at your
+ * choice:
+ *
+ *  - GNU General Public License Version 2 or later (the "GPL")
+ *    http://www.gnu.org/licenses/gpl.html
+ *
+ *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
+ *    http://www.gnu.org/licenses/lgpl.html
+ *
+ *  - Mozilla Public License Version 1.1 or later (the "MPL")
+ *    http://www.mozilla.org/MPL/MPL-1.1.html
+ *
+ * == END LICENSE ==
+ */
+
+CKEDITOR.editorConfig = function( editor )
+{
+	var config = editor.config;
+	config.customConfig = '_editor/custom_config_2.js';
+	config.test_custom1 = 'Ok';
+};
Index: /CKEditor/branches/prototype/_source/tests/core/_editor/custom_config_2.js
===================================================================
--- /CKEditor/branches/prototype/_source/tests/core/_editor/custom_config_2.js	(revision 2091)
+++ /CKEditor/branches/prototype/_source/tests/core/_editor/custom_config_2.js	(revision 2091)
@@ -0,0 +1,26 @@
+/*
+ * CKEditor - The text editor for Internet - http://ckeditor.com
+ * Copyright (C) 2003-2008 Frederico Caldeira Knabben
+ *
+ * == BEGIN LICENSE ==
+ *
+ * Licensed under the terms of any of the following licenses at your
+ * choice:
+ *
+ *  - GNU General Public License Version 2 or later (the "GPL")
+ *    http://www.gnu.org/licenses/gpl.html
+ *
+ *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
+ *    http://www.gnu.org/licenses/lgpl.html
+ *
+ *  - Mozilla Public License Version 1.1 or later (the "MPL")
+ *    http://www.mozilla.org/MPL/MPL-1.1.html
+ *
+ * == END LICENSE ==
+ */
+
+CKEDITOR.editorConfig = function( editor )
+{
+	var config = editor.config;
+	config.test_custom2 = 'Ok';
+};
Index: /CKEditor/branches/prototype/_source/tests/core/ckeditor.html
===================================================================
--- /CKEditor/branches/prototype/_source/tests/core/ckeditor.html	(revision 2090)
+++ /CKEditor/branches/prototype/_source/tests/core/ckeditor.html	(revision 2091)
@@ -106,7 +106,4 @@
 })() );
 
-
-
-
 	//]]>
 	</script>
Index: /CKEditor/branches/prototype/_source/tests/core/editor.html
===================================================================
--- /CKEditor/branches/prototype/_source/tests/core/editor.html	(revision 2091)
+++ /CKEditor/branches/prototype/_source/tests/core/editor.html	(revision 2091)
@@ -0,0 +1,103 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+	<title>CKEDITOR.editor</title>
+	<link rel="stylesheet" type="text/css" href="../test.css" />
+	<script type="text/javascript" src="../../../ckeditor.js"></script>
+	<script type="text/javascript" src="../test.js"></script>
+	<script type="text/javascript">
+	//<![CDATA[
+
+CKEDITOR.test.addTestCase( (function()
+{
+	// Local reference to the "assert" object.
+	var assert = CKEDITOR.test.assert;
+
+	return {
+		test_name : function()
+		{
+			assert.areSame( 'editor1', CKEDITOR.instances.editor1.name );
+		},
+
+		test_element : function()
+		{
+			assert.areSame( document.getElementById( 'editor1' ), CKEDITOR.instances.editor1.element.$ );
+		},
+
+		test_config : function()
+		{
+			// The instance default config must match the CKEDITOR.config.
+
+			var config = CKEDITOR.instances.editor1.config;
+
+			for ( var prop in CKEDITOR.config )
+				assert.areSame( CKEDITOR.config[ prop ], config[ prop ], '"' + prop + '" doesn\'t match' );
+		},
+
+		test_config_inpage : function()
+		{
+			// Pass in-page settings to the instance.
+			CKEDITOR.replace( 'editor2', { test1 : 'ball', theme : 'test_theme' } );
+
+			var config = CKEDITOR.instances.editor2.config;
+
+			assert.areSame( 'ball', config.test1, '"test1" doesn\'t match' );
+			assert.areSame( 'test_theme', config.theme, '"theme" doesn\'t match' );
+
+			// All other settings must match CKEDITOR.config.
+			for ( var prop in CKEDITOR.config )
+			{
+				if ( prop != 'test1' && prop != 'theme' )
+					assert.areSame( CKEDITOR.config[ prop ], config[ prop ], '"' + prop + '" doesn\'t match' );
+			}
+		},
+
+		test_config_customConfig : function()
+		{
+			var testCase = this;
+
+			CKEDITOR.on( 'instancecreated', function( event )
+				{
+					if ( event.data == 'editor3' )
+					{
+						event.editor.on( 'configloaded', function()
+							{
+								testCase.resume( function()
+								{
+									var config = event.editor.config;
+									
+									assert.areSame( 'Ok', config.test_custom1, '"test_custom1" doesn\'t match' );
+									assert.areSame( 'Ok', config.test_custom2, '"test_custom1" doesn\'t match' );
+									assert.areSame( 'ball', config.test1, '"test1" doesn\'t match' );
+									assert.areSame( 'test_theme', config.theme, '"theme" doesn\'t match' );
+
+									// All other settings must match CKEDITOR.config.
+									for ( var prop in CKEDITOR.config )
+									{
+										if ( prop != 'customConfig' && prop != 'test_custom1' && prop != 'test_custom2' && prop != 'test1' && prop != 'theme' )
+											assert.areSame( CKEDITOR.config[ prop ], config[ prop ], '"' + prop + '" doesn\'t match' );
+									}
+								});
+							});
+					}
+				});
+
+			// Pass in-page settings to the instance.
+			CKEDITOR.replace( 'editor3', { customConfig : '_editor/custom_config_1.js', test1 : 'ball', theme : 'test_theme' } );
+
+			this.wait();
+		},
+
+		name : document.title
+	};
+})() );
+
+	//]]>
+	</script>
+</head>
+<body>
+	<textarea id="editor1" class="ckeditor" cols="80" rows="10"></textarea>
+	<textarea id="editor2" cols="80" rows="10"></textarea>
+	<textarea id="editor3" cols="80" rows="10"></textarea>
+</body>
+</html>
Index: /CKEditor/branches/prototype/_source/tests/testall.html
===================================================================
--- /CKEditor/branches/prototype/_source/tests/testall.html	(revision 2090)
+++ /CKEditor/branches/prototype/_source/tests/testall.html	(revision 2091)
@@ -11,4 +11,5 @@
 	'core/ajax',
 	'core/ckeditor',
+	'core/editor',
 	'core/dom/element',
 	'core/env',
