From aff137f891952aaa6ff581ad9edc39198f82abc7 Mon Sep 17 00:00:00 2001
From: michal <michal_cksource@922884.no-reply.drupal.org>
Date: Fri, 24 Jun 2011 15:56:15 +0200
Subject: [PATCH 244/244] [#1198068] Confusing behavior with IMCE button implimentation

---
 plugins/imce/plugin.js |   24 ++++++++++++++++++++----
 1 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/plugins/imce/plugin.js b/plugins/imce/plugin.js
index 5210020..bb4c507 100644
--- a/plugins/imce/plugin.js
+++ b/plugins/imce/plugin.js
@@ -34,26 +34,42 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
       
       //function which receive imce response
       window.ckeditor_setFile = function (file, win) {
-        var cfunc = win.location.href.split('&');
       
+        var cfunc = win.location.href.split('&');      
         for (var x in cfunc) {
           if (cfunc[x].match(/^CKEditorFuncNum=\d+$/)) {
             cfunc = cfunc[x].split('=');
             break;
           }
         }
-        
+         
+         
         CKEDITOR.tools.callFunction(cfunc[1], file);
         win.close();
       };
+      
     }
   } );
   function setFile(file) {
+  		var sel = this.getSelection(),
+			text = sel.getSelectedText();
     //checking if it is image
     if (file.width != 0 && file.height != 0) {
+       if (text)
+       {
+      	this.insertHtml('<a href="' + document.location.protocol + '//'+ document.location.host +  file.url + '">' + text + '</a>');
+       //this.insertHtml('<img src="' + file.url + '" style="width:' + file.width + 'px;height:' + file.height + 'px;" alt="' + file.name + '" />');
+			 }else{
       this.insertHtml('<img src="' + file.url + '" style="width:' + file.width + 'px;height:' + file.height + 'px;" alt="' + file.name + '" />');
-    } else {
-      this.insertHtml('<a href="' + file.url + '">' + file.name + '</a>');
+      }
+    } else {    
+			if (text)
+			{
+				this.insertHtml('<a href="' +document.location.protocol + '//'+ document.location.host + file.url + '">' + text + '</a>');
+			}else
+			{
+	      this.insertHtml('<a href="' + document.location.protocol + '//'+ document.location.host +  file.url + '">' + file.name + '</a>');
+      }
     }
   }
 } )();
-- 
1.7.4.msysgit.0

