(function($) {

    $.fn.update = function(success_function) {

        this.each(function() {
            var id_element = $(this).closest('[id]');
            var id = id_element.attr('id');
            
            var source = window.location.pathname;
            if (id_element.parents('[src]').length) {
                var src_element = id_element.parents('[src]:eq(0)');
                source = src_element.attr('src');
            }
            if (id_element.attr('src')) {
                source = id_element.attr('src');
            }
            var params = window.location.search;
            $.ajax({ url: source + params,
                     dataType: "html",
                     cache: false,
                     success: function (data) {
                        var rscript = /<script(.|\s)*?\/script>/gi;
                        var new_data = $("<div />")
                                            .append(data.replace(rscript, ""))
                                            .find('#' + id + ':eq(0)');

                        id_element.empty()
                                  .append(new_data.contents());
                        
                        if (id_element.is(':empty')) {
                            id_element.remove();
                        }
                        if (success_function && 
                            $.isFunction(success_function) ) {
                            success_function();
                        }
                     },
                     error: function () {
                        window.location = window.location.pathname;
                        return;
                     }});
        });

        return this;

};
})(jQuery);
