/*
 * 'whenLoaded' jQuery plugin
 *
 * @author James Padolsey
 * @description Runs a callback function (specified as argument) when all selected elements have loaded.
 *              Works only with certain elements, i.e. those which have an onload event (e.g. iframe,img etc.)
 * @namespace jQuery.whenLoaded
 * @namespace jQuery.fn.whenLoaded
 * @version 1.0
 * 
 */

(function($){
    $.fn.whenLoaded = function(fn){
        var $elements = this,
            total = this.length;
        $elements.each(function(){
            $(this).load(function(){
                $(this).data('isLoaded',true);
            });
        });
        function check() {
            var loaded = 0;
            $elements.each(function(){
                if($(this).data('isLoaded')) {
                    loaded++;
                }
            });
            if(loaded===total) {
                fn.call($elements);
            } else {
                setTimeout(check,300);
            }
        }
        check();
    }
})(jQuery);