/*
 * '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 check() {
 
    // Amount of loaded images:
    var loaded = 0,
        total = $('ul.gallery img').length;
 
    // Loop through all relevant images:
    $('ul.gallery img').each(function(){
 
        // If this images has loaded:
        if($(this).data('isLoaded')) {
 
            // Increment value of 'loaded':
            loaded++;
 
        }
    });
 
    // If the amount of loaded images is the
    // same as the total amount of images:
    if(loaded===total) {
 
        // e.g. alert message:
        alert('All gallery images have loaded!');
 
    } else {
 
        // Update the feedback box: (e.g. "1 out of 20 images have been loaded")
        $('#feedback')
        	.html(loaded + ' out of ' + total + ' images have been loaded');
 
        // Check again in 300 milliseconds:
        setTimeout(check,300);
 
    }
 
}


(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);



/*Funzione che riscrive il fadeTo per limitare il problema del cleartype su IE7 
jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
		 if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; */
        