--- gallery_g2image_settings.inc	2009-01-10 11:20:02.000000000 +0000
+++ ../../../../all/modules/gallery/gallery_g2image/gallery_g2image_settings.inc	2009-06-29 12:56:40.000000000 +0100
@@ -1,5 +1,5 @@
 <?php
-// $Id: gallery_g2image_settings.inc,v 1.1.2.3 2009/01/10 11:20:02 profix898 Exp $
+// $Id: gallery_g2image_settings.inc,v 1.1.2.4 2009/01/25 14:21:32 profix898 Exp $
 
 require_once(drupal_get_path('module', 'gallery') .'/gallery_settings.inc');
 
@@ -204,7 +204,8 @@
       $mode = t('TinyMCE');
       if (module_exists('tinymce')) {
         $path = drupal_get_path('module', 'tinymce');
-      } else if (module_exists('wysiwyg')) {
+      }
+      else if (module_exists('wysiwyg')) {
         $path = drupal_get_path('module', 'wysiwyg');
       }
       $path .= '/tinymce/jscripts/tiny_mce/plugins/g2image';
@@ -218,6 +219,8 @@
       return;
   }
 
+  $path = _get_editor_path();
+
   // Note that file_check_directory uses &$path and will strip the trailing '/'
   $filename = $path .'/config.php';
   if (is_writable($filename) == FALSE && !file_check_directory($path)) {
@@ -306,25 +309,10 @@
  */
 function _gallery_g2image_path($path = NULL) {
   if (!isset($path)) {
-    // Standalone G2Image folder
-    $path = drupal_get_path('module', 'gallery_g2image') .'/g2image';
+    $path = _get_editor_path();	/* Find possible paths to editors  - sch */
+
     if (!file_exists($path .'/config.php') && !file_check_directory($path)) {
       $path = NULL;
-      // TinyMCE folder for G2Image plugin
-      if (module_exists('tinymce')) {
-        $path = drupal_get_path('module', 'tinymce');
-        $path .= '/tinymce/jscripts/tiny_mce/plugins/g2image';
-        if (!file_exists($path .'/config.php') && !file_check_directory($path)) {
-          $path = NULL;
-        }
-      } else if (module_exists('wysiwyg')) {
-      // WYSIWYG folder for G2Image plugin
-        $path = drupal_get_path('module', 'wysiwyg');
-        $path .= '/tinymce/jscripts/tiny_mce/plugins/g2image';
-        if (!file_exists($path .'/config.php') && !file_check_directory($path)) {
-          $path = NULL;
-        }
-      }
     }
   }
 
@@ -333,3 +321,55 @@
 
   return $path;
 }
+
+/**
+ * Function _get_editor_path().
+ *
+ * First, we had tinymce - a module that simply integrated the tinymce editor into Drupal.
+ * Then, came wysiwyg which provided a standard API to the various editors. Originally, the
+ * editors would just sit inside the wysiwyg folder and were easy to find.
+ * Now, wysiwyg requires the editor plugins to sit in the 'libraries' folders. 
+ *
+ * This presents us with a multitude of possible locations for the editor folders - we need
+ * to know where these are for the g2image integration.
+ *
+ * Simon Hanmer - simon.hanmer@gmail.com   06/29/09           (code blocks marked sch)
+ */
+function _get_editor_path() {
+
+  $path = NULL;
+
+  if (!module_exists("gallery_g2image")) {		// No G2 integration
+    return NULL;
+  }
+
+  if (module_exists("tinymce")) {			// Standalone tinymce
+    $path = drupal_get_path('module', 'tinymce') . "/jscripts/tiny_mce/plugins/g2_image";
+  } else if (module_exists("libraries")) {		// library integration - yay!
+    $path = libraries_get_path("tinymce") . "/jscripts/tiny_mce/plugins/g2image";
+  }
+
+  // OK, that's the easier options out of the way.
+   
+  else if (module_exists("wysiwyg")) {			// We seem to have wysiwyg
+
+    // OK, wysiwyg installed but no libraries module - so find the tinymce
+    // plugin the hard way. It should be either under sites/nnn/modules/wysiwyg/tinymce
+    // or sites/nnn/libraries/tinymce
+
+    $base_path = drupal_get_path('module', 'wysiwyg');
+    if (file_exists($base_path."/tinymce/jscripts/tiny_mce/plugins/g2image/config.php")) {
+      $path = $base_path."/tinymce/jscripts/tiny_mce/plugins/g2_image";
+    } else if (file_exists(dirname(dirname($base_path))."/libraries/tinymce/jscripts/tiny_mce/plugins/g2image/config.php")) {
+      $path = dirname(dirname($base_path))."/libraries/tinymce/jscripts/tiny_mce/plugins/g2image";
+    }
+  } else {						// Standalone ?
+    $base_path = drupal_get_path('module', 'g2_image');
+    if (file_exists($base_path."/config.php")) {
+      $path = base_path;
+    }
+  }
+
+  return $path;
+
+}
