(function($)
{
    $.fn.applyTemplate = function(option)
    {
        var params = option.params;
        var me = $(this);
        var html = me.html();
        html = html || "";
        for (var i=0; i<params.length; i++)
        {
            html = html.replaceAll('{'+params[i].name+'}', params[i].value);
        }
        html = html.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');

        var elem = $(html);

        return $(elem);
    };

    $.fn.encHTML = function() {
        return this.each(function(){
            var me   = jQuery(this);
            var html = me.html();
            me.html(html.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;'));
        });
    };

    $.fn.decHTML = function() {
        return this.each(function(){
            var me   = jQuery(this);
            var html = me.html();
            me.html(html.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>'));
        });
    };

    $.fn.replaceWith = function(replacement) {
        return this.each(function(){
            element = $(this);
            $(this).after(replacement).next()
                .attr('class', element.attr('class')).attr('id',element.attr('id'))
                .html(element.html())
                .prev().remove();
        });
    };

    String.prototype.replaceAll = function(s1, s2) {
            return this.replace(new RegExp(s1,"g"), s2);
    }

})(jQuery);
