if(jQuery) (function($){
$.extend($, {
 fck:{
  waitFor: 10,
  config: { Config: {} },
  user_config: '',
  toolbar: 'Hrcenter',
  path: '/js/fck/',
  list: [],
  loaded: false,
  intercepted: null,
  content: function(i, v){
   try{
    var x = FCKeditorAPI.GetInstance(i);
    if(v) x.SetHTML(v);
    return x.GetXHTML(true);
   }catch(e){ return ''; };
  },
  setHTML: function(i, v){
   if(typeof i=='object'){
    v = i.html;
    i = i.InstanceName || i.instance;
   };
   return $.fck.content(i, v);
  },
  update: function(){
   var e = $.fck.list;
   for(var i=0;i<e.length;i++){
    var ta = e[i].textarea;
    var ht = $.fck.content(e[i].InstanceName);
    ta.val(ht).filter('textarea').text(ht);
    if(ht!=ta.val())
     alert('Critical error in FCK plugin:'+'\n'+'Unable to update form data');
   }
  },
  create: function(option){
   var o = $.extend({}, $.fck.config || {}, option || {});
   $.extend(o, {
    selector: (o.selector || 'textarea.fck, textarea.fckeditor'),
    BasePath: (o.path || o.BasePath || $.fck.path)
   });
   var e = $(o.e);
   if(!e.length>0) e = $(o.selector);
   if(!e.length>0) return;
   if(!$.fck.loading && !$.fck.loaded){
    $.fck.loading = true;
    $.getScript(
     o.BasePath+'fckeditor.js',
     function(){ $.fck.loaded = true; }
    );
   };
   var start = function(){
    if($.fck.loaded){
     $.fck.editor(e,o);
    }
    else{
     if($.fck.waited<=0){
      alert('jQuery.fckeditor plugin error: The FCKEditor script did not load.');
     }
     else{
      $.fck.waitFor--;
      window.setTimeout(start,1000);
     };
    }
   };
   start(e);
   return e;
  },

  intercept: function(){
   if($.fck.intercepted) return;
   $.fck.intercepted = {
    ajaxSubmit: $.fn.ajaxSubmit || function(){}
   };
   $.fn.ajaxSubmit = function(){
    $.fck.update();
    return $.fck.intercepted.ajaxSubmit.apply( this, arguments );
   };
  },

  editor: function(e /* elements */, o /* options */){
   o = $.extend($.fck.config || {}, o || {});
   $.extend(o,{
    Width: (o.width || o.Width || '100%'),
    Height: (o.height || o.Height|| '500px'),
    BasePath: (o.path || o.BasePath || $.fck.path),
    ToolbarSet: $.fck.toolbar,
    Config: (o.config || o.Config || {})
   });
   $.extend(o.Config,{
    CustomConfigurationsPath: o.user_config
   });
   e = $(e);
   if(e.size()>0){
    var a = ($.fck.list || []);
    e.each(
     function(i,t){
						if((t.tagName||'').toLowerCase()!='textarea')
							return alert(['An invalid parameter has been passed to the $.fckeditor.editor function','tagName:'+t.tagName,'name:'+t.name,'id:'+t.id].join('\n'));
      var T = $(t);
      if(!t.name) t.name = 'fck'+($.fck.list.length+1);
      if(!t.id) t.id = t.name;
      if(t.id && !t.fck){
       var n = a.length;
       a[n] = new FCKeditor(t.name);
       $.extend(a[n], o);
       a[n].ReplaceTextarea();
       a[n].textarea = T;
       t.fck = a[n];
      };
     }
    );
    $.fck.list = a;
   };
   return e;
  },
  start: function(o/* options */){
   $.fck.intercept();
   return $.fck.create(o);
  }
 }
});
$.extend($.fn, {
 fck: function(o){
  //(function(opts){ $.fck.start(opts); })($.extend(o || {}, {e: this}));
  $.fck.start($.extend(o || {}, {e: this}));
 }
});
})(jQuery);

