/**
 * componente para loading inteligente para MooTools
 */

var LI_load = 0;
var LI_limitacao = 0;
var LI_nInt = 0;
var LI_carregar = null;
var LI_totalImgs = 0;
var LI_vAnt = 0;
var LI_steps = false;
var LI_verifica = false;
var LI_divImg = null;
var LI_place = null;

var Loading = {
		// função que seleciona os videos        

	       init: function(LI_nIt,LI_attr01) {
	    	   LI_nInt = LI_nIt;
	    	   LI_place = LI_attr01;
	    	   window.addEvent('domready', function() {
	    		   $$(LI_place).each(function(item,index){
	    			   LI_parentNode = item.getParent();
		    		   LI_divImg  = new Element('div');	
		    		   LI_divImg.inject(LI_parentNode);

		    		   LI_divImg.grab(item);
		    		   LI_divImg.setStyle('visibility','hidden');
		    		 
		    		   });
	    		   
	    		   
	    		   Loading.carregando();
		    	   
		    	   
	    	   })
	       },

		carregando: function(){
			
			clearInterval(LI_carregar);
				//console.log(LI_load);
				LI_vAnt = LI_nInt;
				LI_load += LI_nInt;
				LI_totalImgs = $$(LI_place).length;
				LI_load = LI_load  >= LI_totalImgs ? LI_totalImgs : LI_load;
	    	   
	    	   $$(LI_place).each(function(item,index){	    		   
	    		   LI_valor = item.getAttribute('small');
	    			   if(item.getAttribute('small')){
	    				   if((index) < LI_load){
	   						
	   							item.src = item.getAttribute('small');							
	   							item.getParent().setStyle('visibility','visible');
	   							
	   												
	   							
	   						}	
		    		   }
	    	   })
	    	   
	    	   
	    	   LI_verifica = Loading.verificaCarregamento();
	    	   
	    	   if((LI_load < LI_totalImgs) && !LI_verifica){
	    		   LI_carregar = Loading.carregando.delay(3000);
	    		   LI_verifica = false;
	    	   }
		},
	    
       verificaCarregamento: function(LI_place){
	    	
	    	$$(LI_place).each(function(item,index){
	    		LI_avalia = Loading.IsImageOk(item);
		    	if(LI_avalia){ LI_steps = true;  }
		    	else {LI_steps = false; }
	    	})
	    	
	    	return LI_steps;
	    },
		    
	    IsImageOk: function(LI_img) {   	
	    	
	    	if (!LI_img.complete) {
	    	return false;
	    	}		    	
	    	if (typeof LI_img.naturalWidth	!= "undefined" && LI_img.naturalWidth == 0) {
	    	return false;
	    	}		    	
	    	return true;
	    	
	    }

  
		
		
		

}



