function wwTextReplacement() {

    var self = this;

    this.rpcUrl = "/rpc.php?action=replace_text";
    this.cachePath = "/static/img/cache/";

    this.replacements = new Object();

    this.addReplacement = function ( items_name, item ) {

        if( typeof this.replacements[items_name] == "undefined" ) {

            this.replacements[items_name] = new Array();
        }

        this.replacements[items_name].push( item );

    }

    this.replaceText = function ( items_name ) {

        if (    typeof this.replacements[items_name] == "undefined"
            || this.replacements[items_name].length == 0 ) {

            return false;
        }

        for ( var i = 0; i < this.replacements[items_name].length; i++) {

            var curr = this.replacements[items_name][ i ];
            var elems = new Array();

            if ( typeof curr.dontselect != 'undefined' && curr.dontselect != '' && curr.dontselect ) {
                elems = $(curr.selector).not($(curr.dontselect));
            }
            else {
                elems = $(curr.selector);
            }

            if( elems.length > 0 ) {

                for( var j = 0; j < elems.length; j++ ) {

                    if( !elems[ j ] )
                        continue ;

                    var text = this.extractText( elems[ j ] );

                    text = this.trim(text);

                    while( elems[j].hasChildNodes() ) {
                        elems[j].removeChild(elems[j].firstChild);
                    }

                    var tokens = curr.wordwrap ? text.split(' ') : [text];

                    for( var k = 0; k < tokens.length; k++ ) {

                        //curr.params1.text = escapeText( tokens[k] ) + ' ';
                        //curr.params2.text = escapeText( tokens[k] ) + ' ';

                        curr.params1.text = tokens[k] + ' ';
                        if ( curr.params2 )
                            curr.params2.text = tokens[k] + ' ';

                        var elem = $(elems[j]);

                        var img = $('<img class="replacement" />');
                        img.attr('alt', tokens[k]+' ');

                        var src1 = this.rpcUrl + '&' + jQuery.param(curr.params1);
                        img.attr('src' , src1);
                        img.data('src1' , src1);

                        // hover bild?
                        if ( curr.params2 ) {
                            var src2 = this.rpcUrl + '&' + jQuery.param(curr.params2);
                            img.data('src2', src2);

                            elem.bind('mouseenter',function() {
                                $('img.replacement', $(this) ).each(function(){
                                    $(this).attr('src', $(this).data('src2'));
                                });
                            });

                            elem.bind('focus',function() {
                                $('img.replacement', $(this) ).each(function(){
                                    $(this).attr('src', $(this).data('src2'));
                                });
                            });

                            elem.bind('mouseleave',function() {
                                if( this==document.activeElement ) return;
                                $('img.replacement', $(this) ).each(function(){
                                    $(this).attr('src', $(this).data('src1'));
                                });
                            });

                            elem.bind('blur',function() {
                                $('img.replacement', $(this) ).each(function(){
                                    $(this).attr('src', $(this).data('src1'));
                                });
                            });
                        }

                        elem.append(img);
                    }
                }
            }
        }
    }

    this.extractText = function ( element ) {

        if(typeof element == "string")
            return element;
        else if(typeof element == "undefined")
            return element;

        var text = "";
        var kids = element.childNodes;

        for( var i = 0; i < kids.length; i++ ) {

            if( kids [ i ].nodeType == 1 )
                text += this.extractText( kids [ i ] );
            else if( kids[ i ].nodeType == 3 )
                text += kids [ i ].nodeValue;
        }

        return text;
    }

    this.trim = function ( text ) {

        return(text.replace(/\s+/g," ").replace(/\s+$/,"").replace(/^\s+/,""));
    }
}
