jQuery(document).ready(function() {
	jQuery('#anything_slider').preloader({
		gradualDelay:false,
		afterShowAll:function(){
			var slider = jQuery(this);
			slider.anythingSlider({
				height:slideShow['height'],
				hashTags:false,
				buildArrows:slideShow['buildArrows'],
				toggleArrows:slideShow['toggleArrows'],
				buildNavigation:slideShow['buildNavigation'],
				toggleControls:slideShow['toggleControls'],
				autoPlay:slideShow['autoPlay'],
				startStopped:false,
				pauseOnHover:slideShow['pauseOnHover'],
				resumeOnVideoEnd:slideShow['resumeOnVideoEnd'],
				stopAtEnd:slideShow['stopAtEnd'],
				playRtl:slideShow['playRtl'],
				delay:slideShow['delay'],
				animationTime:slideShow['animationTime'],
				easing:slideShow['easing'],
				onSlideInit:function(base){
					if(base.$currentPage.hasClass('stoped')){
						base.startStop(false);
					}else{
						base.startStop(base.options.autoPlay);
					}
				}
			});
			
			if(slider.find('li.panel:eq(1)').hasClass('stoped')){
				slider.data('AnythingSlider').startStop(false);
			}
			slider.find('li.panel.click_stoped').click(function(){
				slider.data('AnythingSlider').startStop(false);
			});
			slider.find('.anything_caption').css({ opacity: slideShow['captionOpacity'] });		
			jQuery('#anything_slider_wrap').css('overflow','visible');
			jQuery('#anything_slider_loading').animate({opacity:0}, 1000,function(){jQuery(this).remove();});
		},
		beforeShowAll:function(){
			jQuery('<div id="anything_slider_loading"></div>').insertBefore(this);
			jQuery(this).show();
		}
	})
});


(function($) {

    $.fn.preloader = function(options) {
        var settings = $.extend({}, $.fn.preloader.defaults, options);


        return this.each(function() {
            settings.beforeShowAll.call(this);
            var imageHolder = $(this);
            
            var images = imageHolder.find(settings.imgSelector).css({opacity:0, visibility:'hidden'});  
            var count = images.length;
            var showImage = function(image,imageHolder){
                if(image.data.source != undefined){
                    imageHolder = image.data.holder;
                    image = image.data.source;  
                };
                
                count --;
                if(settings.delay <= 0){
                    image.css('visibility','visible').animate({opacity:1}, settings.animSpeed, function(){settings.afterShow.call(this)});
                }
                if(count == 0){
                    imageHolder.removeData('count');
                    if(settings.delay <= 0){
                        settings.afterShowAll.call(this);
                    }else{
                        if(settings.gradualDelay){
                            images.each(function(i,e){
                                var image = $(this);
                                setTimeout(function(){
                                    image.css('visibility','visible').animate({opacity:1}, settings.animSpeed, function(){settings.afterShow.call(this)});
                                },settings.delay*(i+1));
                            });
                            setTimeout(function(){settings.afterShowAll.call(imageHolder[0])}, settings.delay*images.length+settings.animSpeed);
                        }else{
                            setTimeout(function(){
                                images.each(function(i,e){
                                    $(this).css('visibility','visible').animate({opacity:1}, settings.animSpeed, function(){settings.afterShow.call(this)});
                                });
                                setTimeout(function(){settings.afterShowAll.call(imageHolder[0])}, settings.animSpeed);
                            }, settings.delay);
                        }
                    }
                }
            };
            
            if(count==0){
                settings.afterShowAll.call(this);
            }else{
                images.each(function(i){
                    settings.beforeShow.call(this);
                
                    image = $(this);
                
                    if(this.complete==true){
                        showImage(image,imageHolder);
                    }else{
                        image.bind('error load',{source:image,holder:imageHolder}, showImage);
                        if($.browser.opera){
                            image.trigger("load");//for hidden image
                        }
                    }
                });
            }
        });
    };


    //Default settings
    $.fn.preloader.defaults = {
        delay:1000,
        gradualDelay:true,
        imgSelector:'img',
        animSpeed:500,
        beforeShowAll: function(){},
        beforeShow: function(){},
        afterShow: function(){},
        afterShowAll: function(){}
    };
})(jQuery);
