(function($) {

// Poll every so often to check if the element is ready.
var INTERVAL_MS = 23;
var interval = null;
var checklist = [];

$.ondomload = {

	add: function(selector, fn) {
			checklist.push({selector: selector, fn: fn});
			if (!interval) {
				interval = setInterval(check, INTERVAL_MS);
			}
			return this;
		},
	stop: function() {
		clearInterval(interval);
		interval = null;
		//console.log('stop ondomload end of checking');
	}
}

function check() {
	var docReady = $.isReady; // check doc ready first; thus ensure that check is made at least once _after_ doc is ready
	for (var i = 0, len = checklist.length; i < len; ++i ) {
		var el = $(checklist[i].selector);
		var fn = checklist[i].fn;
		var cont = fn.apply(el, [$]);
		
		if(!cont) {
			checklist[i] = checklist[--len];
			checklist.pop();
		}
	}
	if (checklist.length ==0) {
		$.ondomload.stop();
	}
};

})(jQuery);
