/*! 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 Backgrounds, 
          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,arrai)
		  {
			$.fn.preloadLinks = new Array();
		  	$.fn.preloadSuccess = new Array();

			if(typeof(arrai)=="object" && arrai.length > 0 ){
				$(arrai).each(function(){
							//Solo para imagina
							var host = "http://"+window.location.host
							var path = window.location.pathname.replace("web.asp","");
							var cadena = this;
							var n = this.indexOf("_aspimagina/");
							if( n != -1 ){
								cadena = "aspimagina/" + cadena.substring("_aspimagina/".length).match(/(\.\.\/)+(.*)/)[2];							
							}
							else
								cadena = cadena.match(/(\.\.\/)+(.*)/)[2];
							
							if(this !='' ){$(this).preloadLinks.push(host+path+cadena);}					
							});
			}

			$(this).find('[class], [style]').each(function(){	
						if($(this).css("background-image")!='none'){
							$(this).preloadLinks.push($(this).css("background-image").match(/url\((.*)\)/)[1]);
						}												  
						});
			$(this).find('img').each(function(){
						if($(this).attr("src")!='') $(this).preloadLinks.push($(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);
