var FCKeditor_lastFocus;
function FCKeditor_OnComplete( editorInstance )
{
    editorInstance.Events.AttachEvent( 'OnFocus', function () {
        FCKeditor_lastFocus = editorInstance;
        $('#fckToolbarPlace').show();
    }) ;
    editorInstance.Events.AttachEvent( 'OnBlur', function () {
        if (FCKeditor_lastFocus != editorInstance) return;
        $('#fckToolbarPlace').hide();
    }) ;
}


$(window).load(function () {
    $('body').prepend(
        $('<div id="fckToolbarPlace"></div>')
        .css({ left: 0, top: 0, position: 'fixed', width: '100%', right: 0, zIndex: 10 })
        .hide()
    );

    /* Replace all 'textarea.rich' with FCKEditors */
    var i = 0;
    $('textarea.rich').each(function () {
        var id = $(this).attr('id');
        if (id == null || id == '') {
            i++;
            id = 'FCKEditor'+i;
        }
        var oFCKeditor = new FCKeditor(id) ;
        oFCKeditor.BasePath = "/fckeditor/" ;
        oFCKeditor.Config["CustomConfigurationsPath"] = "/fckeditor/customconfig.js"  ;
        oFCKeditor.ReplaceTextarea() ;
        $(this).parents('dl').eq(0).addClass("fckReplaced");
    });
});
