(function($){
    if($ != undefined){
        $.fn.slideShow = function(){
            return $(this).each(function(){
                var $slideShow = $(this);
                var $nav = $slideShow.find('.navigation');
                var $navPrev = $slideShow.find('.prev');
                var $navNext = $slideShow.find('.next');
                var $entries = $slideShow.find('ul.entries');
                var $first = $slideShow.find('ul.entries li:first');
                var $last = $slideShow.find('ul.entries li:last');
                var $visible = $first.css('z-index',1).removeClass('active');
                var $next = null;
                var $prev = null;
                var interval;
                var i = 0;
                var speed = 1000;
                var delay = 6000;

                if($entries.find('li').length > 1){
                    $slideShow.find('ul.entries li:not(:first)').hide();
                    var swopImages = function($to){
                        $visible.css('z-index',2);
                        $to.css('z-index',1).css('opacity',1).show();
                        $visible.fadeOut(speed,function(){
                            $visible.css('z-index','').hide();
                            $to.find('h2, p').css('background','');
                            $visible = $to;
                        });
                    }

                    var showNext = function(){
                        $visible.stop(true,true);
                        $next = $visible.next();
                        i++;
                        if(!$next.is('li')){
                            $next = $first;
                            i = 0;
                        }
                        swopImages($next,i);
                    };

                    var showPrev = function(){
                        $visible.stop(true,true);
                        $prev = $visible.prev();
                        i--;
                        if(!$prev.is('li')){
                            $prev = $last;
                        }
                        swopImages($prev,i);
                    };

                    var stop = function(){
                        clearInterval(interval);
                    }

                    var start = function(){
                        speed = 1000;
                        interval = setInterval(showNext,delay);
                    }

                    $navNext.click(function(e){
                        e.preventDefault();
                        speed = 400;
                        showNext();
                    });

                    $navPrev.click(function(e){
                        e.preventDefault();
                        speed = 400;
                        showPrev();
                    });

                    $slideShow.hover(stop,start);

                    start();
                }else{
                    $nav.hide();
                }
            });
        };

        $(document).ready(function(){
            $('.slide-show').slideShow();
        });
    }
})(jQuery);
