diff -upN wysiwyg_template/README.txt wysiwyg_template_new/README.txt
--- wysiwyg_template/README.txt	2009-06-10 21:53:53.000000000 +0200
+++ wysiwyg_template_new/README.txt	2010-10-07 21:24:51.000000000 +0200
@@ -19,27 +19,22 @@ Bug reports, feature suggestions and lat
 
 * Install as usual, see http://drupal.org/node/70151 for further information.
 
-* Go to Administer > Site configuration > Wysiwyg Template, and verify the values
-  for your path and file name of your template registry file  (this file will 
-  hold the names and locations of your templates)
-
-* Create the template registry file in the specified location, its contents should
-  look something like this: 
-  
-  // These templates will be displayed as a dropdown
-  var tinyMCETemplateList = [
-  // Name, URL, Description
-  ["Template One", "sites/all/files/templates/template1.html", "Template One"],
-  ["Template Two", "sites/all/files/templates/template2.html", "Template Two"]
-  ];
-  
-*  Create your template files at the locations specified in your registry file
-
--- CONFIGURATION --
-
 * Go to Administer > Site configuration > Wysiwyg and
 
   - click "edit" to set up your editor profile
 
   - under "Buttons and Plugins" you'll see "Insert Templates" as a new option
 
+* Administer > Site configuration > Wysiwyg > Wysiwyg Templates to manage templates.
+
+
+-- EDITOR COMPATIBILITY --
+
+Supported editors:
+
+ - FCK Editor 2.66
+ 
+ - CK Editor 3.4.0
+ 
+ - Tiny MCE 3.3.8
+
Common subdirectories: wysiwyg_template/js and wysiwyg_template_new/js
diff -upN wysiwyg_template/wysiwyg_template.admin.inc wysiwyg_template_new/wysiwyg_template.admin.inc
--- wysiwyg_template/wysiwyg_template.admin.inc	1970-01-01 02:00:00.000000000 +0200
+++ wysiwyg_template_new/wysiwyg_template.admin.inc	2010-10-07 21:24:54.000000000 +0200
@@ -0,0 +1,205 @@
+<?php
+
+/**
+ * @file
+ * Administrative page callbacks for the Wysiwyg Template module.
+ */
+
+/**
+ * Wysiwyg template overview page - view templates and edit them
+ */
+function wysiwyg_template_overview(){
+	$form = array();
+	// load the templates and display them on the admin form
+	$templateResults = db_query("SELECT * FROM {wysiwyg_templates}");
+	while ($template = db_fetch_object($templateResults)) {
+		$form[$template->tid]['#template'] = (array)$template;
+		$form[$template->tid]['name'] = array('#value' => check_plain($template->title));
+		$form[$template->tid]['description'] = array('#value' => check_plain($template->description));
+		$form[$template->tid]['edit'] = array('#value' => l(t('edit'), "admin/settings/wysiwyg/templates/edit/" . $template->tid));
+		$form[$template->tid]['delete'] = array('#value' => l(t('delete'), "admin/settings/wysiwyg/templates/delete/" . $template->tid));
+	}
+	return $form;
+}
+/**
+ * Theme the Wysiwyg template overview form
+ */
+function theme_wysiwyg_template_overview($form) {
+  // render a nice admin list page (like other modules)
+  $rows = array();
+  foreach (element_children($form) as $key) {
+    if (isset($form[$key]['name'])) {
+      $template = &$form[$key];
+
+      $row = array();
+      $row[] = drupal_render($template['name']);
+      $row[] = drupal_render($template['description']);
+	  $row[] = drupal_render($template['edit']);
+	  $row[] = drupal_render($template['delete']);
+      $rows[] = array('data' => $row);
+    }
+  }
+  if (empty($rows)) {
+    $rows[] = array(array(
+		'data' => t('No templates available.'), 
+		'colspan' => 4
+	));
+  }
+  $rows[] = array(array(
+      'data' => l(t('Create a new template'), 'admin/settings/wysiwyg/templates/add'),
+      'colspan' => 4,
+  ));
+
+  /*$form['wysiwyg_template_selected_content_classes'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Selected content classes'),
+    '#default_value' => variable_get('wysiwyg_template_selected_content_classes', ''),
+    '#description' => t('A list of class names separated by spaces. Any template element with one of the classes will have its content replaced by the selected editor content when first inserted.'),
+  );
+  $form['info'] = array(
+    '#value' => '<p>'.t('Templates can be created and modified from the <a href="' . url('admin/content/wysiwyg-template') . '">Template Administration</a> page.').'</p>',
+  );*/
+  
+  $header = array(t('Name'), t('Description'), array('data' => t('Operations'), 'colspan' => 2));
+  return theme('table', $header, $rows, array('id' => 'wysiwyg_template')) . drupal_render($form);
+}
+
+/**
+ * Wysiwyg template create/modify form
+ */
+function wysiwyg_template_template_form($form_state, $template = NULL) {
+	if($form_state['confirm_delete']){
+		// Rebuild the form to confirm term deletion.
+		$form['tid'] = array('#type' => 'value', '#value' => $form_state['values']['tid']);
+		$form['delete'] = array('#type' => 'value', '#value' => true);
+		return confirm_form($form, 
+			t('Are you sure you want to delete the template %title?', array('%title' => $form_state['values']['title'])),
+			'admin/content/wysiwyg-template',
+			t('This action cannot be undone.'),
+			t('Delete'),
+			t('Cancel')
+		);
+	}
+
+	if($template){
+		// add the current values as defaults to the form, if editing an existing item
+		$form_state['values'] = $template;
+	}
+	
+	$form = array();
+	
+	$form['title'] = array(
+		'#type' => 'textfield',
+		'#title' => t('Template Name'),
+		'#default_value' => $form_state['values']['title'],
+		'#description' => t('Select a name for this template.'),
+		'#maxlength' => 80,
+		'#required' => true
+	);
+	$form['description'] = array(
+		'#type' => 'textfield',
+		'#title' => t('Template Description'),
+		'#default_value' => $form_state['values']['description'],
+		'#description' => t('A description to be shown with the template.')
+	);
+	$form['body'] = array(
+		'#type' => 'textarea',
+		'#title'  => t('HTML Template'),
+		'#description' => t('Create the HTML Template above. Note that the editor may clean your markup. If the precise markup is important, you may wish to check your Wysiwyg settings.'),
+		'#rows'  =>  8,
+		'#default_value' => $form_state['values']['body'],
+		'#required' => true
+	);
+	$form['format'] = filter_form(NULL, NULL, array()); // needed for wysiwyg editor
+	$form['submit'] = array(
+		'#type' => 'submit', 
+		'#value' => t('Save')
+	);
+	
+	if($form_state['values']['tid']){
+		// if it's an existing template, offer a delete button
+		$form['delete'] = array(
+			'#type' => 'submit',
+			'#value' => t('Delete')
+		);
+		// and keep the template id going through a hidden field
+		$form['tid'] = array(
+			'#type' => 'hidden',
+			'#value' => $form_state['values']['tid'],
+		);
+	}
+	return $form;
+}
+
+/**
+ * Wysiwyg template form submit - delete and save handlers
+ */
+function wysiwyg_template_template_form_submit($form, &$form_state) {
+	//if we're deleting the template
+	if ($form_state['clicked_button']['#id'] == 'edit-delete') {
+		//show the confirmation
+		$form_state['rebuild'] = true;
+		$form_state['confirm_delete'] = true;
+		return;
+	}
+	// delete confirmation provided
+	if ($form_state['values']['delete'] === TRUE) {
+		wysiwyg_template_delete_template($form_state['values']['tid']);                             
+		drupal_set_message(t('The template has been deleted.'));		
+		$form_state['redirect'] = 'admin/settings/wysiwyg/templates';
+		return;
+	}
+	
+	// save the template
+	if(wysiwyg_template_save_template($form_state['values'])){
+		drupal_set_message(t('The template has been saved.'));
+	}
+	else{
+		drupal_set_message(t('There was an error saving the template to the database.'));
+	}
+	// redirect back to the overview page
+	$form_state['redirect'] = 'admin/settings/wysiwyg/templates';
+}
+
+/**
+ * Menu callback -- ask for confirmation of rule deletion.
+ */
+function wysiwyg_template_delete_confirm(&$form_state, $tid) {
+  $form['tid'] = array(
+    '#type' => 'value',
+    '#value' => $tid,
+  );
+
+  $template = wysiwyg_template_load_template($tid);
+  return confirm_form($form,
+    t('Are you sure you want to delete the template %title?', array('%title' => $template['title'])),
+    isset($_GET['destination']) ? $_GET['destination'] : 'admin/settings/wysiwyg/templates',
+    t('This action cannot be undone.'),
+    t('Delete'),
+    t('Cancel')
+  );
+}
+
+/**
+ * Execute node deletion.
+ */
+function wysiwyg_template_delete_confirm_submit($form, &$form_state) {
+  if ($form_state['values']['confirm']) {
+    wysiwyg_template_delete_template($form_state['values']['tid']);
+  }
+
+  $form_state['redirect'] = 'admin/settings/wysiwyg/templates';
+  return;
+}
+
+
+/**
+ * Settings form help
+ */
+function wysiwyg_template_help($path, $arg) {
+  switch ($path) {
+    case 'admin/settings/wysiwyg/templates':
+      $output = t('<p>The Wysiwyg Template module allows you to specify templates to be used with the Wysiwyg editor. The template button for the editor can be enabled from the Wysiwyg profile settings. The currently supported editors are FCK Editor, CK Editor and TinyMCE.</p>');
+      return $output;
+  }
+}
diff -upN wysiwyg_template/wysiwyg_template.install wysiwyg_template_new/wysiwyg_template.install
--- wysiwyg_template/wysiwyg_template.install	1970-01-01 02:00:00.000000000 +0200
+++ wysiwyg_template_new/wysiwyg_template.install	2010-10-07 21:24:56.000000000 +0200
@@ -0,0 +1,46 @@
+<?php
+
+/**
+ * Implementation of hook_install().
+ */
+function wysiwyg_template_install() {
+  drupal_install_schema('wysiwyg_template');
+}
+
+/**
+ * Implementation of hook_schema().
+ */
+function wysiwyg_template_schema() {
+  $schema['wysiwyg_templates'] = array(
+    'fields' => array(
+      'tid' => array(
+        'description' => 'The primary identifier for the Wysiwyg template',
+        'type' => 'serial',
+        'unsigned' => TRUE,
+        'not null' => TRUE),
+      'title' => array(
+        'description' => 'The title of the Wysiwyg template',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE),
+      'description' => array(
+        'description' => 'The description of the Wysiwyg template',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE),
+      'body' => array(
+        'description' => 'The Wysiwyg template HTML',
+        'type' => 'text'),
+    ),
+    'primary key' => array('tid'),
+  );
+  return $schema;
+}
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function wysiwyg_template_uninstall() {
+  cache_clear_all('wysiwyg_template:*', 'cache', TRUE);
+  db_query("DROP TABLE {wysiwyg_templates}");
+}
\ No newline at end of file
diff -upN wysiwyg_template/wysiwyg_template.module wysiwyg_template_new/wysiwyg_template.module
--- wysiwyg_template/wysiwyg_template.module	2009-06-10 23:50:19.000000000 +0200
+++ wysiwyg_template_new/wysiwyg_template.module	2010-10-07 21:24:58.000000000 +0200
@@ -1,5 +1,4 @@
 <?php
-// $Id: wysiwyg_template.module,v 1.4 2009/06/10 21:50:19 jenlampton Exp $
 
 /**
  * @file
@@ -7,79 +6,274 @@
  * Wysiwyg API.
  */
 
-/**
- * Implementation of hook_wysiwyg_plugin().
- */
-function wysiwyg_template_wysiwyg_plugin($editor, $version) {
-  static $integrated = array();
-
-  switch ($editor) {
-    case 'tinymce':
-      // get values defined on settings page
-      $jspath = variable_get('wysiwyg_template_path', 'sites/all/files');
-      $jsfile = variable_get('wysiwyg_template_file', 'templates.js');
-      // include path to js file
-      /*
-      $settings = array(
-        'template' => array('template_external_list_url' => base_path() . $jspath . '/' . $jsfile),
-      );
-      drupal_add_js($settings, 'setting');
-      */
-      $tinymce = wysiwyg_get_editor($editor);
-      return array(
-        'template' => array(
-          'path' => $tinymce['library path'] .'/plugins/template',
-          'buttons' => array('template' => t('Insert templates')),
-          'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/template',
-          'internal' => TRUE,
-          'options' => array(
-            'template_external_list_url' => base_path() . $jspath . '/' . $jsfile,
-          ),
-          'load' => TRUE,
-        ),
-      );
-  }
-}
-
-/**
+ 
+ /**
  * Implementation of hook_menu().
  */
 function wysiwyg_template_menu() {
   $items = array();
-
-  $items['admin/settings/wysiwyg_template'] = array(
-    'title' => 'Wysiwyg Templates',
-    'description' => 'Wysiwyg Template plugin settings',
+  
+  
+  // template overview settings page
+  $items['admin/settings/wysiwyg/templates'] = array(
+	'title' => 'Wysiwyg Templates',
+	'description' => 'Create and modify Wysiwyg Templates',
+	'page callback' => 'drupal_get_form',
+	'type' => MENU_LOCAL_TASK,
+	'page arguments' => array('wysiwyg_template_overview'),
+	'access arguments' => array('administer filters'),
+	'file' => 'wysiwyg_template.admin.inc',
+  );
+  // add template
+  $items['admin/settings/wysiwyg/templates/add'] = array(
+    'title' => 'Add Wysiwyg Template',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('wysiwyg_template_admin_settings'),
+    'page arguments' => array('wysiwyg_template_template_form'),
     'access arguments' => array('administer filters'),
+	'type' => MENU_CALLBACK,
+	'file' => 'wysiwyg_template.admin.inc',
+  );
+  // edit template
+  $items['admin/settings/wysiwyg/templates/edit/%wysiwyg_template'] = array(
+    'title' => 'Edit Wysiwyg Template',
+    'page callback' => 'drupal_get_form',
+	'page arguments' => array('wysiwyg_template_template_form', 5),
+    'access arguments' => array('administer filters'),
+    'type' => MENU_CALLBACK,
+    'parent' => 'admin/settings/wysiwyg/templates',
+	'file' => 'wysiwyg_template.admin.inc',
+  );
+  //delete template
+  $items['admin/settings/wysiwyg/templates/delete'] = array(
+      'title' => 'Delete Wysiwyg template',
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('wysiwyg_template_delete_confirm'),
+      'access arguments' => array('administer filters'),
+      'file' => 'wysiwyg_template.admin.inc',
+      'type' => MENU_CALLBACK,
+  );
+  // javascript template list
+  $items['wysiwyg-templates/%/list'] = array(
+	'page callback' => 'wysiwyg_template_list_js',
+	'page arguments' => array(1),
+	'access arguments' => array('access content'),
+	'type' => MENU_CALLBACK,
+  );
+  // individual template html - referenced by javascript above
+  $items['wysiwyg-templates/%/load/%wysiwyg_template_html'] = array(
+	'page callback' => 'wysiwyg_template_html_print',
+	'page arguments' => array(3, 1),
+	'access arguments' => array('access content'),
+	'type' => MENU_CALLBACK,
   );
-
   return $items;
 }
 
 /**
- * Implementation of hook_menu().
+ * Wysiwyg template Wildcard menu loader - prepare item for the create/modify admin form
  */
-function wysiwyg_template_admin_settings() {
-  $form = array();
-  
-  $form['wysiwyg_template_path'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Path to your js file'),
-    '#default_value' => variable_get('wysiwyg_template_path', 'sites/all/files'),
-    '#description' => t('defaults to sites/all/files'),
-  );
-  $form['wysiwyg_template_file'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Name of your js file'),
-    '#default_value' => variable_get('wysiwyg_template_file', 'templates.js'),
-    '#description' => t('defaults to templates.js'),
-  );
-  $form['info'] = array(
-    '#title' => t('Next Steps'),
-    '#value' => '<p>'.t('Create a file called ').variable_get('wysiwyg_template_file', 'template.js').t('(from above) and upload it to ').variable_get('wysiwyg_template_path', 'sites/all/files').t('(from above).  The file should define the names and locations of the HTML templates you would like to use in your WYSIWYG editor.  You may copy and paste the syntax from the README.txt file.').'</p>',
+function wysiwyg_template_load($tid){
+	$template = wysiwyg_template_load_template($tid);
+	return $template;
+}
+/**
+ * Implementation of hook_theme(), for the admin template overview form.
+ */
+function wysiwyg_template_theme() {
+  return array(
+    'wysiwyg_template_overview' => array(
+      'arguments' => array('form' => array()),
+    ),
   );
-  
-  return system_settings_form($form);
 }
+ 
+
+//how too look at each editor as a plugin (inc)
+//three hooks:
+//config
+//template definition
+//template loading
+//do two first, then generalise
+/**
+ * Implementation of hook_wysiwyg_plugin().
+ */
+function wysiwyg_template_wysiwyg_plugin($editorName, $version) {
+	switch ($editorName) {
+		case 'tinymce':
+			return array(
+				'template' => array(
+					'buttons' => array('template' => t('Insert templates')),
+					'url' => 'admin/content/wysiwyg-template',
+					'internal' => TRUE,
+					'options' => array(
+						'template_external_list_url' => url('wysiwyg-templates/' . $editorName . '/list'), //load the javascript template list
+						'template_selected_content_classes' => variable_get('wysiwyg_template_selected_content_classes', ''),
+						//'template_replace_values' => array('caption' => 'Replace with this content')
+					),
+					'extended_valid_elements' => array( //tinymce specific - allows additional elements to be used
+					  'img[class|src|border=0|alt|title|width|height|align|name|style]',
+					),
+					'load' => TRUE,
+				),
+			);
+		break;
+		case 'ckeditor':
+			return array(
+				'templates' => array(
+					'buttons' => array('Templates' => t('Insert templates')),
+					'url' => 'admin/content/wysiwyg-template',
+					'internal' => TRUE,
+					'options' => array(
+						'templates_files' => array(url('wysiwyg-templates/' . $editorName . '/list')), //load the javascript template list
+					),
+					'load' => TRUE,
+				),
+			);
+		break;
+		case 'fckeditor':
+			return array(
+				'templates' => array(
+					'buttons' => array('Templates' => t('Insert templates')),
+					'url' => 'admin/content/wysiwyg-template',
+					'internal' => TRUE,
+					'options' => array(
+						'TemplatesXmlPath' => array(url('wysiwyg-templates/' . $editorName . '/list')), //load the javascript template list
+					),
+					'load' => TRUE,
+				),
+			);
+		break;
+	}
+}
+/**
+ * Generate javascript for template loading
+ */
+function wysiwyg_template_list_js($editorName){
+	//don't cache templates
+	drupal_set_header("CacheControl: no-cache");
+	drupal_set_header("Expires: -1");
+	drupal_set_header("Content-Type:text/javascript; charset=UTF-8");
+	$templates = wysiwyg_template_load_all();
+	switch($editorName){
+		case 'tinymce':
+			print 'var tinyMCETemplateList = ';
+			$outArray = array();
+			foreach($templates as $template){
+				$outArray[] = array(
+					$template['title'],
+					url('wysiwyg-templates/' . $editorName . '/load/') . $template['tid'],
+					$template['description'],
+				);
+			}
+			print json_encode($outArray) . ";";
+		break;
+		case 'ckeditor':
+			print "CKEDITOR.addTemplates( 'default', { templates:";
+			//todo: include an image with templates...
+			//imagesPath : CKEDITOR.getUrl( CKEDITOR.plugins.getPath( 'templates' ) + 'templates/images/' ),
+			// load the templates into the json array structure
+			foreach($templates as &$template){
+				$template['html'] = $template['body'];
+				unset($template['body']);
+				unset($template['tid']);
+			}
+			print json_encode($templates);
+			print "});";
+		break;
+		case 'fckeditor':
+			print '<?xml version="1.0" encoding="utf-8" ?>';
+			print '<Templates imagesBasePath="">';
+			foreach($templates as $template){
+				print '<Template title="' . check_plain($template['title']) . '" image="">';
+				print '<Description>' . check_plain($template['description']) . '</Description>';
+				print '<Html><![CDATA[';
+				print $template['body'];
+				print ']]></Html></Template>';
+			}
+ 			print '</Templates>';
+		break;
+		case '':
+		break;
+	}
+}
+/**
+ * Menu callback to display the loaded template
+ */
+function wysiwyg_template_html_print($body, $editorName){
+	//don't cache templates
+	drupal_set_header("CacheControl: no-cache");
+	drupal_set_header("Expires: -1");
+	drupal_set_header("Content-Type:text/html; charset=utf-8");
+	switch($editorName){
+		case 'tinymce':
+			print $body;
+		break;
+	}
+}
+/**
+ * Load a specific template for viewing, as called through the menu wildcard
+ */
+function wysiwyg_template_html_load($tid){
+	$template = wysiwyg_template_load_template($tid);
+	return $template['body'];
+}
+
+/**
+ * Wysiwyg template database load function
+ */
+function wysiwyg_template_load_template($tid){
+	$templateResults = db_query("SELECT * FROM {wysiwyg_templates} WHERE tid = %d", $tid);
+	$template = db_fetch_object($templateResults);
+	$tItem = null;
+	if($template != null){
+		$tItem = array(
+			'title' => $template->title,
+			'description' => $template->description,
+			'body' => $template->body,
+			'tid' => $tid,
+		);
+	}
+	
+	return $tItem;
+}
+/**
+ * Wysiwyg load all templates from the database
+ */
+function wysiwyg_template_load_all(){
+	$templateResults = db_query("SELECT * FROM {wysiwyg_templates}");
+	$templates = array();
+	while ($dbTemplate = db_fetch_object($templateResults)) {
+		$template = array(
+			'title' => $dbTemplate->title,
+			'description' => $dbTemplate->description,
+			'body' => $dbTemplate->body,
+			'tid' => $dbTemplate->tid,
+		);
+		$templates[] = $template;
+	}
+	return $templates;
+}
+/**
+ * Wysiwyg template database save function
+ */
+function wysiwyg_template_save_template($template){
+	$tItem = array(
+		'title' => $template['title'],
+		'description' => $template['description'],
+		'body' => $template['body'],
+	);
+	
+	//if it's an existing item we're updating
+	$key = null;
+	if($template['tid']){
+		$key = 'tid';
+		$tItem['tid'] = $template['tid'];
+	}
+	
+	//save to database
+	return drupal_write_record('wysiwyg_templates', &$tItem, $key);
+}
+
+function wysiwyg_template_delete_template($tid){
+	db_query("DELETE FROM {wysiwyg_templates} WHERE tid = %s", $tid);
+}
\ No newline at end of file
