(function( $ ){

  $.fn.customSlider = function() {
	  
    $div = $(this);
    $loader = $div.find('> .custom-slider-load-background');
    $textUl = $div.find('> .custom-slider-text > ul.custom-slider-list');

    $loader.find('> img').each(function() {
        $bgDiv = $('<div></div>');
        $bgDiv.addClass('custom-slider-background')
        .css({
            'backgroundImage': 'url(' + $(this).attr('src') +')'
        });
        $(this).parent().after($bgDiv);
    });
    
    $bgDivs = $div.find('> .custom-slider-background');
    
    $bgDivs.filter(':last').show();
    
    $textUl.find(' > li').each(function() {
        jQuery(this).prependTo(jQuery(this).parent());
    });
    
    $textUl.find('> li:last').delay(1000).fadeIn(1000);

    setInterval(function() {
        $bg = $div.find('> .custom-slider-background:last');
        $text = $textUl.find('> li:last');
        
        $bg.prev().show();
        $bg.fadeOut(1000)        
            .queue(function(){
                jQuery(this).insertAfter($loader);
                jQuery(this).dequeue();
            });
            
        $text.fadeOut(1000);
        
        $text.queue(function() {
            jQuery(this).prependTo(jQuery(this).parent());
            $textUl.find('> li:last').delay(1000).fadeIn(1000);
            jQuery(this).dequeue();
        });
    }, 10000);
  };
})( jQuery );


