PageNav = Class.create({

    initialize: function(containerId, contentPrefix, contentSelector) {
        this.container = $(containerId);
        this.getElement(contentPrefix);

        if (this.container && this.container.empty() && this.element) {
            this.selector = contentSelector;
            this.previous();
            this.next();
        }
    },

    getElement: function(contentPrefix) {
        var id = document.body.id;
        if (id) this.element = $(contentPrefix+id);
    },

    previous: function(type) {
        var previous = this.getLink('previous');
        if (!previous) {
            var previous = this.getLink('up');
        }
        this.writeLink(previous, 'previous');
    },

    next: function(type) {
        var next = this.getLink('next');
        if (!next) {
            var next = this.element.down(this.selector);
        }
        this.writeLink(next, 'next');
    },

    getLink: function(type) {
        var link = this.element.up(this.selector);
        if (!link) return false;
        return link[type](this.selector);
    },

    writeLink: function(link, type) {
        if (link) {
            var clone = link.down('a').cloneNode(true);
            clone.id = null;

            link = new Element('div');
            link.className += ' '+type;
            link.appendChild(clone);
        }

        if (link && link.nodeType == 1) this.container.appendChild(link);
    }

});



Event.onDOMReady(function() {
    new PageNav('pagenav', 'sn-', 'li');
});
