/*! Copyright (c) 2009 Radgeofrey Mission (http://www.codezyne.com)
 *
 * jQuery.CDZ_IMAGEPRELOADER.js (IE6+, FF, OPERA, SAFARI)
 *
 * Version: 1.0.0
 * Date: 14th November, 2009
 * 
 * Requires: jQuery 1.3.2+
 *
 * About: A very simple yet lightweight preloader, this preload images from <IMG> or CSS Backrounds, 
          I often use this to my site and now im sharing it.
 *
 * Example : $(document).cdzPreloadImages(function(total){alert("Preloader has started")},
										  function(loaded, total){alert("Preload Progress : " + loaded + " / " + total)}, 
										  function(){$('#loadingstatus').hide(); $('#mainpage').show();} );
	      The sample above will preload images of entire document, including background images. And assumimg that #loadingstatus div is visible
		  and #mainpage div is not visible, so upon complete preload : a callback function will be called to hide #loadingstatus and show #mainpage.
		  
Revision As of Dec 02, 2009 : trigger complete event if no image found
 */
(function($){
		  
		  
		  $.fn.preloadLinks = ''
		  $.fn.preloadSuccess = ''
		  
		  $.fn.cdzPreloadImages = function(onstart, onprogress, oncomplete)
		  {
			$.fn.preloadLinks = new Array();
		  	$.fn.preloadSuccess = new Array();
			$(this).find('[class], [style]').each(function(){
														
						if($(this).css("background-image")!='none')
						{
						$(this).preloadLinks[$(this).preloadLinks.length] = $(this).css("background-image").match(/url\((.*)\)/)[1];
						}												  
						});
			$(this).find('img').each(function(){
														
						if($(this).attr("src")!='')
						{
						$(this).preloadLinks[$(this).preloadLinks.length] = $(this).attr("src");
						}												  
						});
			onstart($(this).preloadLinks.length);
			for(q in  $(this).preloadLinks)
			{

						$.ajax({type: "GET", url: $(this).preloadLinks[q], complete: function() { 
																	
																	$(this).preloadSuccess[$(this).preloadSuccess.length]='Complete';
																	onprogress($(this).preloadSuccess.length, $(this).preloadLinks.length)
																	if($(this).preloadLinks.length == $(this).preloadSuccess.length)oncomplete();
																	
																								 }});
						
			
			}
			if($(this).preloadLinks.length==0)oncomplete();
			return $(this);
		  }
		  
		  
 })(jQuery);