Ajax.FCKeditor = Class.create();
Ajax.FCKeditor.version = '0.1 beta';
Ajax.FCKeditor.author = 'Johannes Gamperl <info@jg-webdesign.de>';
Ajax.FCKeditor.prototype = {
    initialize: function(element, options) {
        this.element = element;
        this.oFCKeditor = null;
        this.options = Object.extend({
            ToolbarSet : "Ajax.Editor",
            BasePath   : "http:/zend.mjsolutions.at/js/FCKeditor/",
            Height     : Element.getDimensions(this.element).height+50,
            Width      : Element.getDimensions(this.element).width,
            phpScript  : "http:/zend.mjsolutions.at/js/Ajax.Editor.php"
        }, options || {});        
        this.registerEvents();
        this.createFCKeditor();
    },
    
    registerEvents : function()
    {
        this.mouseOverMode = this.options.mouseOverMode || this.mouseOverMode;
        this.mouseOutMode = this.options.mouseOutMode || this.mouseOutMode;
        this.onclickListener = this.enterEditMode.bindAsEventListener(this);
        Event.observe(this.element, 'click', this.onclickListener);
        this.onmouseoverListener = this.mouseOverMode.bindAsEventListener(this);
        Event.observe(this.element, 'mouseover', this.onmouseoverListener);
        this.onmouseoutListener = this.mouseOutMode.bindAsEventListener(this);
        Event.observe(this.element, 'mouseout', this.onmouseoutListener);    
    },
  
    enterEditMode: function(evt)
    {
        $(this.element).innerHTML = $('FCK_DUMMY_'+this.element).innerHTML;
    },
  
    mouseOverMode: function(evt)
    {
        Element.setStyle(this.element, {border:'1px solid #ff0000'} );
    },
  
    mouseOutMode: function(evt)
    {
        Element.setStyle(this.element, {border:''} );
    },
    
    createFCKeditor : function()
    {
        this.oFCKeditor = new FCKeditor('FCK_EDIT.'+this.element);
        this.oFCKeditor.BasePath = this.options.BasePath;
        this.oFCKeditor.Height 	 = this.options.Height;
        this.oFCKeditor.Width 	 = this.options.Width;
        this.oFCKeditor.Value	 = $(this.element).innerHTML;
        this.oFCKeditor.ToolbarSet = this.options.ToolbarSet;
        this.oFCKeditor.Create();
        $('FCK_EDIT.'+this.element).parentNode.id = 'FCK_DUMMY_'+this.element;
        $('FCK_EDIT.'+this.element).parentNode.innerHTML = ' \
            <form method="post" onsubmit="return FCKeditor_OnSubmit(this.parentNode.id,\''+this.options.phpScript+'\');">'
            +$('FCK_EDIT.'+this.element).parentNode.innerHTML+
            '</form> \
        ';
        Element.hide($('FCK_DUMMY_'+this.element));
    }
};
// FCKEditor Submit-Event 
function FCKeditor_OnSubmit(id,phpScript) 
{
    var path = location.pathname.substring(location.pathname.lastIndexOf('/')+1);
    var e = FCKeditorAPI.GetInstance('FCK_EDIT.'+id);
    var txt = e.GetXHTML(true);
    var myAjax = new parent.Ajax.Request(
        phpScript,
        {
            method: 'post',
            postBody: 'file='+path+'&div='+id+'&content='+escape(txt),
            onComplete : function(r)
            {
                $(id).innerHTML = txt;
                $('FCK_EDIT.'+id).value = txt;
            },
            onFailure : function(r)
            {
                alert("Error: " +r.status+ "/t" +r.statusText);
            }
        }
    );
    return false;
}
