Index: includes/insert.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/insert/includes/insert.inc,v
retrieving revision 1.1
diff -u -r1.1 insert.inc
--- includes/insert.inc	21 Oct 2009 06:23:43 -0000	1.1
+++ includes/insert.inc	18 Nov 2009 03:21:03 -0000
@@ -7,6 +7,22 @@
  */
 
 /**
+ * Insert implementation of hook_theme().
+ */
+function insert_insert_theme() {
+  return array(
+    'insert_image' => array(
+      'arguments' => array('item' => NULL, 'widget' => NULL),
+      'template' => 'templates/insert-image',
+    ),
+    'insert_link' => array(
+      'arguments' => array('item' => NULL, 'widget' => NULL),
+      'template' => 'templates/insert-link',
+    ),
+  );
+}
+
+/**
  * Implementation of hook_insert_styles().
  */
 function insert_insert_styles() {
@@ -20,7 +36,7 @@
 /**
  * Implementation of hook_insert_content().
  */
-function insert_insert_content($item, $style, $widget) {
+function insert_insert_content($item, $style, $field) {
   $style_name = $style['name'];
 
   if ($style_name == 'auto') {
@@ -31,10 +47,10 @@
   }
 
   if ($style_name == 'image') {
-    return theme('insert_image', $item, $widget);
+    return theme('insert_image', $item, $field);
   }
   else {
-    return theme('insert_link', $item, $widget);
+    return theme('insert_link', $item, $field);
   }
 }
 
@@ -43,7 +59,7 @@
  */
 function template_preprocess_insert_image(&$vars) {
   $vars['url'] = file_create_url($vars['item']['filepath']);
-  $vars['class'] = !empty($vars['widget']['insert_class']) ? $vars['widget']['insert_class'] : '';
+  $vars['class'] = !empty($vars['field']['widget']['insert_class']) ? $vars['field']['widget']['insert_class'] : '';
   $image_info = @image_get_info($vars['item']['filepath']);
   $vars['width'] = isset($image_info['width']) ? $image_info['width'] : '';
   $vars['height'] = isset($image_info['height']) ? $image_info['height'] : '';
@@ -54,6 +70,6 @@
  */
 function template_preprocess_insert_link(&$vars) {
   $vars['url'] = file_create_url($vars['item']['filepath']);
-  $vars['class'] = !empty($vars['widget']['insert_class']) ? $vars['widget']['insert_class'] : '';
+  $vars['class'] = !empty($vars['field']['widget']['insert_class']) ? $vars['field']['widget']['insert_class'] : '';
   $vars['name'] = $vars['item']['filename'];
 }
Index: includes/imagecache.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/insert/includes/imagecache.inc,v
retrieving revision 1.1
diff -u -r1.1 imagecache.inc
--- includes/imagecache.inc	21 Oct 2009 06:23:43 -0000	1.1
+++ includes/imagecache.inc	18 Nov 2009 03:21:03 -0000
@@ -7,6 +7,18 @@
  */
 
 /**
+ * Insert implementation of hook_theme().
+ */
+function imagecache_insert_theme() {
+  return array(
+    'imagecache_insert_image' => array(
+      'arguments' => array('item' => NULL, 'field' => NULL, 'preset_name' => NULL),
+      'template' => 'templates/imagecache-insert-image',
+    ),
+  );
+}
+
+/**
  * Implementation of hook_insert_styles().
  */
 function imagecache_insert_styles() {
@@ -15,6 +27,7 @@
   foreach ($presets as $preset) {
     $insert_styles['imagecache_' . $preset['presetname']] = array(
       'label' => t($preset['presetname']),
+      'widgets' => array('filefield', 'imagefield'),
     );
   }
   return $insert_styles;
@@ -23,9 +36,9 @@
 /**
  * Implementation of hook_insert_content().
  */
-function imagecache_insert_content($item, $style, $widget) {
+function imagecache_insert_content($item, $style, $field) {
   $preset_name = preg_replace('/^imagecache_/', '', $style['name']);
-  return theme('imagecache_insert_image', $item, $widget, $preset_name);
+  return theme('imagecache_insert_image', $item, $field, $preset_name);
 }
 
 /**
@@ -34,5 +47,5 @@
 function template_preprocess_imagecache_insert_image(&$vars) {
   $vars['filepath'] = $vars['item']['filepath'];
   $vars['url'] = imagecache_create_url($vars['preset_name'], $vars['item']['filepath']);
-  $vars['class'] = !empty($vars['widget']['insert_class']) ? $vars['widget']['insert_class'] : '';
+  $vars['class'] = !empty($vars['field']['widget']['insert_class']) ? $vars['field']['widget']['insert_class'] : '';
 }
Index: insert.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/insert/insert.js,v
retrieving revision 1.4
diff -u -r1.4 insert.js
--- insert.js	22 Oct 2009 00:12:25 -0000	1.4
+++ insert.js	18 Nov 2009 03:21:03 -0000
@@ -29,12 +29,20 @@
   }
 
   function insert() {
-    var widgetType = $(this).attr('rel');
-    var settings = Drupal.settings.insert.widgets[widgetType];
+    var fieldName = $(this).attr('rel');
+    var settings = Drupal.settings.insert.widgets[fieldName];
     var wrapper = $(this).parents(settings.wrapper).filter(':first').get(0);
     var style = $('.insert-style', wrapper).val();
     var content = $('input.insert-template[name$="[' + style + ']"]', wrapper).val();
 
+    // Perform AJAX content replacement if needed.
+    if (settings.ajax) {
+      $.getJSON(
+        Drupal.settings.insert.ajaxCallback + '/' + settings.typeName + '/' + fieldName,
+        data
+      );
+    }
+
     // Update replacements.
     for (var fieldName in settings.fields) {
       var fieldValue = $(settings.fields[fieldName], wrapper).val();
Index: insert.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/insert/insert.module,v
retrieving revision 1.1
diff -u -r1.1 insert.module
--- insert.module	21 Oct 2009 06:23:43 -0000	1.1
+++ insert.module	18 Nov 2009 03:21:03 -0000
@@ -8,6 +8,21 @@
  */
 
 /**
+ * Implementation of hook_menu().
+ */
+function insert_menu() {
+  $items = array();
+
+  $items['insert/ajax'] = array(
+    'type' => MENU_CALLBACK,
+    'page callback' => 'insert_ajax',
+    'access callback' => TRUE,
+  );
+
+  return $items;
+}
+
+/**
  * Implementation of hook_elements().
  */
 function insert_elements() {
@@ -22,25 +37,31 @@
 }
 
 /**
- * Implementation of hook_init().
+ * Implementation of hook_form_alter().
  */
-function insert_init() {
-  // Default file and image implementations.
-  module_load_include('inc', 'insert', 'includes/insert');
-
-  // FileField support.
-  if (module_exists('filefield')) {
-    module_load_include('inc', 'insert', 'includes/filefield');
-  }
-
-  // ImageField support
-  if (module_exists('imagefield')) {
-    module_load_include('inc', 'insert', 'includes/imagefield');
+function insert_form_alter(&$form, &$form_state, $form_id) {
+  if (preg_match('/_node_form$/', $form_id) && isset($form['#node'])) {
+    foreach (element_children($form) as $key) {
+      if (isset($form[$key]['#field_name'])) {
+        $field = content_fields($form[$key]['#field_name'], $form['#node']->type);
+        if (!empty($field['widget']['insert'])) {
+          foreach (element_children($form[$key]) as $delta) {
+            if (is_numeric($delta) && (!isset($form[$key][$delta]['#after_build']) || array_search('insert_process', $form[$key][$delta]['#after_build'] === FALSE))) {
+              $form[$key][$delta]['#after_build'][] = 'insert_process';
+            }
+          }
+        }
+      }
+    }
   }
+}
 
-  // ImageCache support
-  if (module_exists('imagecache')) {
-    module_load_include('inc', 'insert', 'includes/imagecache');
+/**
+ * Implementation of hook_init().
+ */
+function insert_init() {
+  foreach (insert_module_implementations() as $name => $label) {
+    module_load_include('inc', 'insert', 'includes/' . $name);
   }
 }
 
@@ -48,28 +69,39 @@
  * Implementation of hook_theme().
  */
 function insert_theme() {
-  return array(
+  $items = array(
     'insert_widget' => array(
       'arguments' => array('field' => NULL),
       'template' => 'templates/insert-widget',
     ),
+  );
 
-    // Theme functions in includes/insert.inc.
-    'insert_image' => array(
-      'arguments' => array('item' => NULL, 'widget' => NULL),
-      'template' => 'templates/insert-image',
-    ),
-    'insert_link' => array(
-      'arguments' => array('item' => NULL, 'widget' => NULL),
-      'template' => 'templates/insert-link',
-    ),
+  foreach (insert_module_implementations() as $name => $label) {
+    $items = array_merge($items, (array) module_invoke($name, 'insert_theme'));
+  }
 
-    // Theme functions in includes/imagecache.inc.
-    'imagecache_insert_image' => array(
-      'arguments' => array('item' => NULL, 'widget' => NULL, 'preset_name' => NULL),
-      'template' => 'templates/imagecache-insert-image',
-    ),
-  );
+  return $items;
+}
+
+/**
+ * Get a list of all module implementations provided by Insert.
+ */
+function insert_module_implementations() {
+  static $modules;
+
+  if (!isset($modules)) {
+    $all_modules = module_list();
+    $modules = array();
+    $includes_dir = dirname(__FILE__) . '/includes';
+    foreach (file_scan_directory($includes_dir, '.inc$') as $key => $details) {
+      // Default file and image implementations.
+      if (module_exists($details->name)) {
+        $modules[$details->name] = $all_modules[$details->name];
+      }
+    }
+  }
+
+  return $modules;
 }
 
 /**
@@ -147,8 +179,8 @@
 /**
  * Given an item and an insert style, return the output.
  */
-function insert_content($item, $style, $widget) {
-  return module_invoke($style['module'], 'insert_content', $item, $style, $widget);
+function insert_content($item, $style, $field) {
+  return module_invoke($style['module'], 'insert_content', $item, $style, $field);
 }
 
 /**
@@ -168,7 +200,10 @@
   // Add base settings only once.
   if (!isset($js_added)) {
     $js_added = array();
-    $settings = array('fileDirectoryPath' => file_directory_path());
+    $settings = array(
+      'fileDirectoryPath' => file_directory_path(),
+      'ajaxCallback' => url('insert/ajax'),
+    );
     drupal_add_js(array('insert' => $settings), 'setting');
     drupal_add_js(drupal_get_path('module', 'insert') . '/insert.js');
   }
@@ -180,12 +215,15 @@
     $insert_settings = array(
       'maxWidth' => $field['widget']['insert_width'],
       'wrapper' => $insert_widget['wrapper'],
+      'ajax' => !empty($insert_widget['ajax']),
       'fields' => $insert_widget['fields'],
+      'typeName' => $field['type_name']
     );
     drupal_add_js(array('insert' => array('widgets' => array($field['widget']['type'] => $insert_settings))), 'setting');
   }
 
-  if ($element['fid']['#value']) {
+  // TODO: This check is FileField-specific. Make a property in hook_insert_widgets().
+  if (!isset($element['fid']) || $element['fid']['#value']) {
     $insert_styles = array_filter((array) $field['widget']['insert_styles']);
     $default = !empty($field['widget']['insert_default']) ? $field['widget']['insert_default'] : 'auto';
     if (empty($insert_styles)) {
@@ -195,7 +233,7 @@
       if ($enabled && ($style = insert_style_load($style_name))) {
         $element['insert_templates'][$style_name] = array(
           '#type' => 'hidden',
-          '#value' => insert_content($item, $style, $field['widget']),
+          '#value' => insert_content($item, $style, $field),
           '#id' => $element['#id'] . '-insert-template-' . str_replace('_', '-', $style_name),
           '#name' => $element['#name'] . '[insert_template][' . $style_name . ']',
           '#attributes' => array('class' => 'insert-template'),
@@ -308,6 +346,35 @@
 }
 
 /**
+ * Menu callback; Process content via AJAX and return it.
+ */
+function insert_ajax($type_name = NULL, $field_name = NULL, $style_name = NULL) {
+  $return = array('success' => FALSE);
+  // Crazy big if statement to check that the widget exists and is enabled.
+  if ((isset($type_name) && isset($field_name) && isset($style_name)) &&
+      ($field = content_fields($field_name, $type_name)) &&
+      ($style = insert_style_load($style_name)) &&
+      ($insert_widget = insert_widget_load($field['widget']['type'])) &&
+      $field['widget']['insert_styles'][$style['name']]) {
+    // Access checks on the field and node level.
+    if (!node_access('create', $type_name) || (function_exists('content_access') && !content_access('edit', $field))) {
+      $return['error'] = t('Access denied.');
+    }
+    else {
+      $item = array();
+      foreach ($insert_widget['fields'] as $key => $selector) {
+        $item[$key] = $_GET[$key];
+      }
+      if ($content = insert_content($item, $style, $field)) {
+        $return['success'] = TRUE;
+        $return['content'] = $content;
+      }
+    }
+  }
+  drupal_json($return);
+}
+
+/**
  * Preprocess variables for the insert-widget.tpl.php file.
  */
 function template_preprocess_insert_widget(&$vars) {
Index: includes/emvideo.inc
===================================================================
RCS file: includes/emvideo.inc
diff -N includes/emvideo.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ includes/emvideo.inc	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,42 @@
+<?php
+// $Id: filefield.inc,v 1.1 2009/10/21 06:23:43 quicksketch Exp $
+
+/**
+ * @file
+ * Insert support for Embedded Video module.
+ */
+
+/**
+ * Implementation of hook_insert_widgets().
+ */
+function emvideo_insert_widgets() {
+  return array(
+    'emvideo_textfields' => array(
+      'wrapper' => '.form-element',
+      'ajax' => TRUE,
+      'fields' => array(
+        'embed' => 'input.form-text',
+      ),
+    ),
+  );
+}
+
+/**
+ * Implementation of hook_insert_styles().
+ */
+function emvideo_insert_styles() {
+  $formatters = emvideo_field_formatter_info();
+  foreach ($formatters as $key => $formatter) {
+    $formatters[$key]['widget'] = 'emvideo_textfields';
+  }
+  return $formatters;
+}
+
+/**
+ * Implementation of hook_insert_content().
+ */
+function emvideo_insert_content($item, $style, $field) {
+  $node = (object) array('nid' => NULL, 'type' => NULL, 'title' => NULL);
+  $item = emfield_parse_embed($field, $item['embed'], 'emvideo');
+  return module_invoke('emvideo', 'field_formatter', $field, $item, $style['name'], $node);
+}
