PageNav = Class.create({

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

        if (this.container && this.container.empty() && this.item) {
            this.getPrevious();
            this.getNext();
            this.writeLinks();
        }
    },

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

    getPrevious: function() {
        this.previous = this.item.previousSibling;
        while (this.previous && (!this.previous.tagName || this.previous.tagName.toLowerCase() != 'li')) this.previous = this.previous.previousSibling;

        var first = this.item.parentNode.previousSibling;
        while (first && (!first.tagName || first.tagName.toLowerCase() != 'h2')) first = first.previousSibling;
        if (!this.previous && first) this.previous = first;

        if (this.previous) {
            this.previous = $(this.previous);
            this.previous = this.writeWrappers(this.previous);
            this.previous.className += ' previous';
        }
    },

    getNext: function() {
        this.next = this.item.nextSibling;
        while (this.next && (!this.next.tagName || this.next.tagName.toLowerCase() != 'li')) this.next = this.next.nextSibling;

        if (this.next) {
            this.next = $(this.next);
            this.next = this.writeWrappers(this.next);
            this.next.className += ' next';
        }
    },

    writeWrappers: function(node) {
        var link = node.down('a');
        if (!link) return false;

        var clone = $(node.down('a').cloneNode(true));
        clone.addClassName('arrow');

        var wrapper = Builder.node('div', [clone]);

        return wrapper;
    },

    writeLinks: function() {
        if (this.previous && this.previous.nodeType == 1) this.container.appendChild(this.previous);
        if (this.next && this.next.nodeType == 1) this.container.appendChild(this.next);
    }

});



Event.onDOMReady(function() {
    new PageNav('pagenav', 'fn-', 'featurenav', 'li');
    $$('#featurenav a').each(function(link){
        link.observe('mousedown',function(evt) {
            document.cookie='s_iphone_nav=left nav; domain=.apple.com; path=/';
        });
    });
    $$('#pagenav a').each(function(link){
        link.observe('mousedown',function(evt) {
            document.cookie='s_iphone_nav=bottom nav; domain=.apple.com; path=/';
        });
    });
}, false);
