jQuery.fn.extend({
    slideShow: function(args) {
        return this.each(function() {
            new jQuery.slideShow(this);
        });
    }
});

jQuery.slideShow = function(object) {
    var $object = $(object);

    var go = function(direction) {
        var $current = $object.find('.slideshow-image:visible');
        $current.fadeOut('normal', function() {
            if (direction > 0) {
                $current.next('.slideshow-image').show();
            }
            else {
                $current.prev('.slideshow-image').show();
            }
            if (!can_go(direction)) {
                $object.find(direction > 0 ? '.slideshow-next' : '.slideshow-prev').addClass('slideshow-nav-disabled');
            }
            else {
                $object.find(direction > 0 ? '.slideshow-next' : '.slideshow-prev').removeClass('slideshow-nav-disabled');
            }
            $object.find(direction < 0 ? '.slideshow-next' : '.slideshow-prev').removeClass('slideshow-nav-disabled');
        });
    };
    var can_go = function(direction) {
        var $current = $object.find('.slideshow-image:visible');
        if ((direction > 0 && $current.next('.slideshow-image').length) ||
            (direction < 0 && $current.prev('.slideshow-image').length)) {
            return true;
        }
        return false;
    };

    $object.find('.slideshow-prev, .slideshow-next').each(function() {
        var $link = $(this);

        $link.click(function() {
            if ($link.hasClass('slideshow-next')) {
                if (can_go(1)) {
                    go(1);
                }
                else {
                    $object.find('.slideshow-next').addClass('slideshow-nav-disabled');
                }
            }
            else if ($link.hasClass('slideshow-prev')) {
                if (can_go(-1)) {
                    go(-1);
                }
                else {
                    $object.find('.slideshow-prev').addClass('slideshow-nav-disabled');
                }
            }
            return false;
        });
    });

}
