// code taken/adapted from Kupu
// Copyright (c) 2003-2005, Kupu Contributors

var FCK=oFCKeditor;


// taken from $Id: kupuhelpers.js 12353 2005-05-16 12:29:05Z duncan $
function ContextFixer(func, context) {
    /* Make sure 'this' inside a method points to its class */
    this.func = func;
    this.context = context;
    this.args = arguments;
    var self = this;
    
    this.execute = function() {
        /* execute the method */
        var args = new Array();
        // the first arguments will be the extra ones of the class
        for (var i=0; i < self.args.length - 2; i++) {
            args.push(self.args[i + 2]);
        };
        // the last are the ones passed on to the execute method
        for (var i=0; i < arguments.length; i++) {
            args.push(arguments[i]);
        };
        return self.func.apply(self.context, args);
    };

};



// taken from $Id: kupuhelpers.js 12353 2005-05-16 12:29:05Z duncan $
function addEventHandler(element, event, method, context) {
    /* method to add an event handler for both IE and Mozilla */
    var wrappedmethod = new ContextFixer(method, context);
    var args = new Array(null, null);
    for (var i=4; i < arguments.length; i++) {
        args.push(arguments[i]);
    };
    wrappedmethod.args = args;
    try {
        if (_SARISSA_IS_MOZ) {
            element.addEventListener(event, wrappedmethod.execute, false);
        } else if (_SARISSA_IS_IE) {
            element.attachEvent("on" + event, wrappedmethod.execute);
        } else {
            throw _("Unsupported browser!");
        };
        return wrappedmethod.execute;
    } catch(e) {
        alert(_('exception ${message} while registering an event handler ' +
                'for element ${element}, event ${event}, method ${method}',
                {'message': e.message, 'element': element,
                    'event': event,
                    'method': method}));
    };
};



// adapted from $Id: kupuploneeditor.js 9879 2005-03-18 12:04:00Z yuppie $
makeLinksRelative = function(FCK, contents) {
    var base=FCK.BaseHref.replace('http://www.isola4you.org','');
    var href = base.replace(/\/[^\/]*$/, '/');
    var hrefparts = href.split('/');
    return contents.replace(/(<[^>]* (?:src|href)=")([^"]*)"/g,
        function(str, tag, url, offset, contents) {
            url=url.replace('http://www.isola4you.org','');
            var urlparts = url.split('#');
            var anchor = urlparts[1] || '';
            url = urlparts[0];
            var urlparts = url.split('/');
            var common = 0;
            while (common < urlparts.length &&
                   common < hrefparts.length &&
                   urlparts[common]==hrefparts[common])
                common++;
            var last = urlparts[common];
            if (common+1 == urlparts.length && last=='emptypage') {
                urlparts[common] = '';
            }
            // The base and the url have 'common' parts in common.
            if (common > 0) {
                var path = new Array();
                var i = 0;
                for (; i+common < hrefparts.length-1; i++) {
                    path[i] = '..';
                };
                while (common < urlparts.length) {
                    path[i++] = urlparts[common++];
                };
                if (i==0) {
                    path[i++] = '.';
                }
                str = path.join('/');
                if (anchor) {
                    str = [str,anchor].join('#');
                }
                str = tag + str+'"';
            };
            return str;
        });
};

changeBeforeSave = function(event) {

    var myInstance = FCKeditorAPI.GetInstance(this.name) ;
    myInstance.SetHTML( makeLinksRelative(FCK,myInstance.GetXHTML()) )
}


loadHandler = function (inputname) {
   oTextarea = document.getElementById(inputname) ;		
   if ( !oTextarea ) oTextarea = document.getElementsByName(inputname)[0] ;
   addEventHandler(oTextarea.form, 'submit', changeBeforeSave,oTextarea);
}

