/*
 * oPreloader - jQuery Plugin
 * a imager preloader developed for my portfolio
 *
 * Copyright (c) 2010 Andrés Bott
 * Examples and documentation at: http://andresbott.com or http://oPreloader.andresbott.com
 * 
 * Version: 0.1(04/MAr/2010)
 * Developed with: jQuery v1.4
 * 
 * licensed under the LGPL license:
 *   http://www.gnu.org/licenses/lgpl.html
 *
 * to Do; automaticly seach images inside the div and preload them all
 *
 */

(function($){
 $.fn.oPreload = function(options) {

	var defaults = {
		divSelector: false,
		borderSelector: false,
		imageSelector:false,
		fadeTime:500
	};

	var options = $.extend(defaults, options);


	// array donde se van a cachear las imagenes
	var preloaded_images= new Array();

	//itinieramos la primera vez sobre las imagenes
	$(this).each(function(i){

		// definimos los valores de las imagenes
		preloaded_images[i]= new Array();

		preloaded_images[i]["loaded"] = false;
		preloaded_images[i]["url"] =  $(this).find(options.imageSelector).attr('src')
		preloaded_images[i]["node"]=$(this);

		// obtenemos las medidas de la imagen para sustituirla por un div
		preload_img_w=$(this).find(options.imageSelector).width();
		preload_img_h=$(this).find(options.imageSelector).height();

		// ocultamos los divs
		$(this).find(options.divSelector).hide();

		//añadimos el div de precarga
		$(this).append('<div class="oPreloader_preloadbox"></div>')

		$(this).find(".oPreloader_preloadbox").addClass("oPreload_preload");
		$(this).css({"width": preload_img_w+"px" , "height" : preload_img_h+"px" });
		$(this).find(".oPreloader_preloadbox").css({"width": preload_img_w+"px" , "height" : preload_img_h+"px" });

	})// end of each

	var loadOrder=Array();

	var loadOrderi=0;
	
// 	itinieramos la segunda vez sobre las imagenes y precargamos las imagenes
	$(this).each(function(i){

		preloaded_images[i]["img"] = new Image();

		$(preloaded_images[i]["img"]).load(function(){
					preloaded_images[i]["loaded"]=true;
					preloaded_images[i]["show"]=false;
// 					loadOrder.push(i);

					loadOrder[loadOrderi]=i;
					loadOrderi=loadOrderi+1;
			}).attr('src', preloaded_images[i]["url"]);

	})// end of seccond each each



	var images_length=preloaded_images.length;
	var images_loaded=0
	var loadOrderNext=0;

	start_showing()

	function start_showing(){
		// si esta definida la primera imagen a cargar
		if(typeof loadOrder[loadOrderNext] =="number"){
			
			see_preloaded(loadOrder[loadOrderNext]);
		}else{
		//sino espera un momento
			setTimeout(function(){start_showing();},500);
		}
	
	}

	function see_preloaded(i){
		// si ya esta cargada la imagen
		if(preloaded_images[i]["loaded"]==true){
			// si no fue mostrada
			if(preloaded_images[i]["show"]==false){
				preloaded_images[i]["show"]=true;

				images_loaded=images_loaded+1;

				$(preloaded_images[i]["node"]).find(".oPreloader_preloadbox").remove()
				$(preloaded_images[i]["node"]).find(options.divSelector).fadeIn(options.fadeTime,function(){
					//callback
					//si aun quedan imagenes por mostrar
					if(images_loaded!=images_length){
						loadOrderNext=loadOrderNext+1
						see_preloaded(loadOrder[loadOrderNext])
					}
				});
			}
		}else{
			setTimeout(function(){see_preloaded2(i);},500);
		}
	}// en of function

 };
})(jQuery);
