;(function($) {
    $.preloadImages = function(images, func) {
        var i = 0;
        var cache = [];
        var loaded = 0;
        var num = images.length;
        
        for ( ; i < num; i++ ) (function(i) {
            var new_image = $('<img/>').attr('src', images[i]).load(function(){
                loaded++;
                
                if(loaded == num)
                {                                                
                    func();                   
                }
            });						
            cache.push(new_image);
        })(i);
        
        return true;
    };
    
    $.fn.imgSlider = function() {        
        if (!$(this).length || $(this).length>1) {            
            return this;
        }
        
        var direction = 'right';
        var e = this;
        var timeout_id = 0;
        var in_progress = false
        
        function slider_animate(new_dir)
        {
            clearTimeout(timeout_id);
            timeout_id = setTimeout(auto_animate, 5000);
            in_progress = true;
            var dir = direction;
            if(new_dir)
            {
                dir = new_dir;
            }
            
            if(dir == 'right')
            {
                $(e).find('ul:first').children('li:first').animate({'width':0, 'padding':0, 'border':0, 'opacity':0, 'margin-right':0}, 1000, 'easeOutExpo', function(){                    
                    $(this).appendTo($(this).parent()).css({'width':'', 'padding':'', 'border':'', 'opacity':1, 'margin-right':''});
                    in_progress = false;
                });
            }
            else
            {                
                $(e).find('ul:first').children('li:last').fadeTo(0,0, function(){
                    var old_width = $(this).width();
                    $(e).find('ul:first').children('li:eq(2)').css({'opacity':1}).animate({'opacity':0, 'width':0}, 1000, 'easeOutExpo', function(){
                        $(this).css({'width':'', 'opacity':1});
                        in_progress = false;
                    });
                    $(this).css({'opacity':1, 'margin-left':'-'+old_width+'px'}).prependTo($(this).parent()).animate({
                        'margin-left':0
                    }, 1000, 'easeOutExpo');                    
                });              
            }
        }
        
        $(e).find('ul:first > li').hover(function(){
            clearTimeout(timeout_id);
        },function(){
            timeout_id = setTimeout(auto_animate, 3000);
        });
        
        function auto_animate()
        {
            slider_animate('right');            
        }
        
        e.find('.next').click(function(){
            if(!in_progress)
            {
                slider_animate('right');
            }
        });
        
        e.find('.prev').click(function(){
            if(!in_progress)
            {
                slider_animate('left');
            }
        });       
        
        timeout_id = setTimeout(auto_animate, 3000);        
      
        return true;
    };
})(jQuery);
