(function($) {
	
	//Initialisation du préloader
	var buffer = 5;
	var text = 'chargement...';
	var preloaded = 0;
	var active = true;

	$.fn.preloader = function() {
	
		//	Récupération des balises utiles
		var imgs = $( ' img', $(this) );
		total = imgs.length;
		
		//	Récupération des dimensions
		var width = $(this).width()+'px';
		var height = $(this).height()+'px';
			
		//	Mise en place du préloader
		$(this).prepend( '<p class="loading">'+text+'</p>' );
		$( '.loading' ).css({ 'width': width, 'height': height });
	
		//	Parcours des images pour tester le chargement
		imgs.each(function(){
			// Get image instance.
			var image = new Image();
			image.src = $(this).attr('src');
	
			if (image.complete)        
				imageLoaded();        
			else        
				image.onload = imageLoaded;
    	 
		});
	
	};
	
	function imageLoaded(){ 
	    
	    preloaded++;
		if ( ( preloaded >= buffer || preloaded == total ) && active){
			active = false;
			$( '.loading' ).fadeOut( 'slow', function(){ 
				$( '.loading' ).remove();
			});
		}

	}//	imageLoaded()

})(jQuery);
