// JavaScript Document

(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	var ph = 122;
	var ah = $(this).height();
	var mh = (ph - ah) / 2;
	if (ah > 0) $(this).css({marginTop : mh});
	//$(this).css({opacity: 0, visibility: "visible"}).animate({opacity: 0.7},600);
	});
};
})(jQuery); 



$.fn.preloader = function(options) {

		var diapo1 = jQuery("<img />",{
		
		id : 'diapo-1' ,
		src : 'imagenes/diapo-1.gif'
		
		}).hide().appendTo("body");

		var diapo2 = jQuery("<img />",{
		
		id : 'diapo-2' ,
		src : 'imagenes/diapo-2.gif'
		
		}).hide().appendTo("body");
		
		var diapo3 = jQuery("<img />",{
		
		id : 'diapo-3' ,
		src : 'imagenes/diapo-3.gif'
		
		}).hide().appendTo("body");	
		
		var diapo4 = jQuery("<img />",{
		
		id : 'diapo-4' ,
		src : 'imagenes/diapo-4.gif'
		
		}).hide().appendTo("body");	
		
        var defaults = {
                onDone : function() {
                },
                onEachLoad : function(img) {img.parents(".thmfoto").css({'width':''});
                },
                onLoadError : function(img) {
                },
                fadeIn : 300,
                delay : 40,
                interval : 200,
                parentWrap : '.thmfoto',
                loader : 'imagenes/indicator.gif'
        },
        options = $.extend(defaults, options),
        images = $(this).find('img'),
        loaderCss = {
                background : 'url(' + options.loader + ') 50% 45px no-repeat',
                display : 'inline-block'
        },
        delayTime = 0
        loadError = false;
        
        images.css({
                        visibility : 'visible',
                        opacity : 0
                }).each(function() {
                        if($(this).parent(options.parentWrap).length) 
                                $(this).parent(options.parentWrap).css(loaderCss);
                        else 
                                $(this).wrap('<a class="unwrap"/>').parent().css(loaderCss);
                });
        
		
		
        var timer = setInterval(function() {
                        init();
                }, options.interval);
        
        init = function() {
                images = images.filter(function() {
                
                                this.onerror = function() {
                                        loadError = true;
                                };                              
                                
                                if(loadError == 1) {
                                
                                        $(this).css({ visibility : 'visible',   opacity : 1 });
                                        
                                        if($(this).parent().hasClass('unwrap')) 
                                                $(this).unwrap();
                                        else 
                                                $(this).parent().attr('style', '');
                                                
                                        options.onLoadError($(this));
                                        
                                        return null;                                    
                                } else if(this.complete && this.naturalWidth !== 0) {                           
                                
                                        delayTime = delayTime + options.delay;
                                        $(this).vAlign().css({ visibility : 'visible' })
                                                .delay(delayTime).animate({ opacity : 0.7 }, options.fadeIn, function() {
                                                
                                                        if($(this).parent().hasClass('unwrap')) 
                                                                $(this).unwrap();
                                                        else 
                                                                $(this).parent().attr('style', '');
                                                                
                                                        options.onEachLoad($(this));
                                                });
                                } else
                                        return this;
                        }
                );
                
                if(images.length == 0) {
                        clearInterval(timer);
                        options.onDone();
                }               
        };
}
