// ==UserScript==
// @name           PHPBB3 Next/Prev Links
// @namespace      http://www.pretentiousname.com/
// @description    Add next-topic and previous-topic links to PHPBB3's default prosilver theme.
// @include        http://resource.dopus.com/viewtopic.php?*
// ==/UserScript==

//////////////////////////////////////////////////////
// by Leo Davidson (http://www.pretentiousname.com) //
//////////////////////////////////////////////////////
// History:
// 1.2  (12/Mar/2010): Sigh, that didn't work with single-post threads.
// 1.1  (12/Mar/2010): Oops, first version depended on stuff which was only on the page for moderators.
// 1.0  (12/Mar/2010): First release


var anchorElements = document.evaluate('//div[@id="page-body"]/h2/a', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

if (anchorElements.snapshotLength < 1) { return; }

var firstUrlSplit = anchorElements.snapshotItem(0).getAttribute("href").split("?");

if (firstUrlSplit.length != 2 || firstUrlSplit[0] != "./viewtopic.php") { return; }

var urlArgs = firstUrlSplit[1].split("&");

var topicId;
var forumId;

for (var i = 0; i < urlArgs.length; i++)
{
    var argSplit = urlArgs[i].split("=");
    if (argSplit.length == 2)
    {
        if (argSplit[0] == "t")
        {
            topicId = argSplit[1];
        }
        else if (argSplit[0] == "f")
        {
            forumId = argSplit[1];
        }
    }
}

if (topicId == null || forumId == null) { return; }

var paginationElements = document.evaluate('//div[@class="topic-actions"]/div[@class="pagination"]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

if (paginationElements.snapshotLength != 2) { return; }

for (var i = 0; i < paginationElements.snapshotLength; i++)
{
    var paginationDiv = paginationElements.snapshotItem(i);

    var newDiv = document.createElement("div");
    newDiv.setAttribute("class", "right-box");

    var prevAnchor = document.createElement("a");
    prevAnchor.setAttribute("class","left");
    prevAnchor.setAttribute("href", document.baseURI.split("?")[0] + "?f=" + forumId + "&t=" + topicId + "&view=previous");
    prevAnchor.appendChild(document.createTextNode("previous topic"));
    newDiv.appendChild(prevAnchor);

    newDiv.appendChild(document.createTextNode(" \u2022 "));

    var nextAnchor = document.createElement("a");
    nextAnchor.setAttribute("class","right");
    nextAnchor.setAttribute("href", document.baseURI.split("?")[0] + "?f=" + forumId + "&t=" + topicId + "&view=next");
    nextAnchor.appendChild(document.createTextNode("next topic"));
    newDiv.appendChild(nextAnchor);

    var parentDiv = paginationDiv.parentNode;

    var clearDiv = document.createElement("div");
    clearDiv.setAttribute("class", "clear");

    if (i == 0)
    {
        parentDiv.insertBefore(clearDiv, paginationDiv);
        parentDiv.insertBefore(newDiv, clearDiv);
    }
    else
    {
        parentDiv.insertBefore(newDiv, paginationDiv.nextElementSibling);
        parentDiv.insertBefore(clearDiv, newDiv);
    }

}
