/*
  Basis Objekt
*/
var fck = {
    instances: []
};

/*
  Funktion zur Initialisierung von FCKeditor Textareas
  Alle Sichtbaren FCKEditor Textareas werden erzeugt
*/
fck.init = function () {
    //alle FCK Textareas holen
    var textareas = document.getElementsBySelector('textarea.fckeditor');
    for (var i = 0; i < textareas.length; i++) {
        if (textareas[i].offsetHeight > 0) {
            this.create(textareas[i].id);
        }
    }
}

/*
  Erzeugt ein FCK Instanz basierend auf einer Textarea
  Als Argument muss die id der Textarea uebergeben werden
*/
fck.create = function (id) {
    if (  typeof this.instances[id] != 'undefined'                        ||
          document.getElementById(id).className.search(/fckeditor/) == -1 ||
          document.getElementById(id).offsetHeight == 0 ) {
        return;
    }
    this.instances[id] = new FCKeditor(id);
    this.instances[id].Height = 400;
    this.instances[id].Config['SkinPath'] = '/fckeditor/editor/skins/office2003/';
    this.instances[id].ToolbarSet = 'live';
    if(typeof cdata.siteHandle != 'undefined') {
        this.instances[id].Config['EditorAreaCSS'] = '/css/' + cdata.siteHandle + '/screen.css';
    }
    this.instances[id].ReplaceTextarea();
}

/*
  Disabled den FCK Editor einer Textarea, so das diese wieder eine
  normale Textarea ist.
*/
fck.disable = function (id) {
    var oEditor         = FCKeditorAPI.GetInstance(id);
    if(typeof oEditor == 'undefined') {
        return;
    }
    oEditor.UpdateLinkedField();

    // Neue Div/Textarea bauen
    var activeTextarea  = document.getElementById(id);
    var name            = activeTextarea.getAttribute('name');
    var div             = document.createElement('div');
    var textarea        = document.createElement('textarea');
        textarea.value  = activeTextarea.value;
        textarea.setAttribute('id', id);
        textarea.setAttribute('name', activeTextarea.getAttribute('name'));
        textarea.className = activeTextarea.className.replace(/fckeditor/, '');
    div.appendChild(textarea);

    // aktives Div ermitteln
    var activeDiv = activeTextarea.parentNode;

    // aktives div durch neues div ersetzen
    activeDiv.parentNode.replaceChild(div, activeDiv);
    delete(fck.instances[id]);

}
