(function($) {
    $.Enhancer = {
        modules: new Array(),
        add: function(fn) {
            this.modules.push(fn);
        },
        removeViewState: function(html) {
            this.lastViewState = "";
            var p = html.indexOf('"javax.faces.ViewState"');
            if (p < 0) return html;
            p = html.indexOf('value="', p) + 7;
            if (p < 7) return html;
            var end = html.indexOf('"', p);
            if (end < 0) return html;
            var lastViewState = html.substring(p, end);
            html = html.substring(0, p) + this.removeViewState(html.substring(end));
            this.lastViewState = lastViewState;
            return html;
        }
    };
    $.escape = function(myid) {
        return myid.replace(/:/g,"\\:").replace(/\./g,"\\.");
    };
    $.enhance = function(text, selector) {
        text = $.Enhancer.removeViewState(text);
        var lastViewState = $.Enhancer.lastViewState;
        var object = selector ? $(selector, text) : $(text);
        object = object.enhance();
        if (lastViewState) {
            object.find('input#javax\\.faces\\.ViewState').val(lastViewState);
        }
        return object;
    };
    $.fn.enhance = function() {
        var s = "";
        for (var i = 0; i < $.Enhancer.modules.length; i++) {
            var t = new Date().getTime();
            $.Enhancer.modules[i].apply(this);
            t -= new Date().getTime();
            s += ", " + -t;
        }
//        alert(s.substring(2));
        return this;
    };
    $(function(e){
        if (e.returnValue !== false) $('html').enhance();
    });
})(jQuery);

