﻿(function($) {
	$.fn.disposable = function(cln) {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
		return this.each(function() {
			var el = this;
			if (!el.dispose) {
				el.dispose = cleanup;
				$(window).bind("unload", cleanup);
                if (!prm.get_isInAsyncPostBack()) {
                    prm.add_beginRequest(cleanup);
                }
			}
			
			function cleanup() {
				prm.remove_beginRequest(cleanup);
				if (!el)
					return;
				$(el).unbind();
				$(window).unbind("unload", cleanup);
				el.dispose = null;
				el = null;
			};
		});
	};
})(jQuery);

