//jQuery's own toggle(bool) method doesn't work when jQuery effects library is in use, q.v. http://dev.jqueryui.com/ticket/4473
$.fn.showIf = function(bool) { if (bool) this.show(); else this.hide(); }

function Cyclable(items, nav)
{
    var t = this;
    t.items     = items;
    t.current   = items.eq(0);
    
    t.nav           = nav;
    t.nav.next      = nav.children(".next");
    t.nav.prev      = nav.children(".prev");
    t.nav.splitter  = nav.children(".splitter");
    
    t.nav.next.click(function() { t.showNext(); return false; });
    t.nav.prev.click(function() { t.showPrev(); return false; });
    
    t.showItem(t.current, false);  
    t.nav.showIf(t.items.length > 1);
}

Cyclable.prototype = {
    showItem: function(item, animate)
    {
        if (!item.length) return;

        this.items.hide();
        if (animate) item.fadeIn("fast"); else item.show();
        
        this.current = item;
        this.syncNav();
    },
    syncNav: function()
    {
        var hasPrev = this.current.prev().length,
            hasNext = this.current.next().length;
        this.nav.prev.showIf(hasPrev);
        this.nav.next.showIf(hasNext);
        this.nav.splitter.showIf(hasPrev && hasNext);
    },
    showPrev: function()    { this.showItem(this.current.prev(), true); },
    showNext: function()    { this.showItem(this.current.next(), true); }
}