// ==UserScript==
// @name                Forum navbar
// @version             2.2
// @date                2010-04-06
// @author              Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace           etsy.com
// @description         Adds a forum navbar to forums pages
// @include             http://www.etsy.com/forums_thread.php?*
// @include             http://www.etsy.com/forums_main.php*
// @include             http://www.etsy.com/forums_board.php?*
// @include             http://www.etsy.com/forums_user_threads.php*
// @include             http://www.etsy.com/forums_search.php?*
// ==/UserScript==

// set up some default categories
var forums = [{link:"http://www.etsy.com/forums_board.php?forum_id=1", title:"Site Help"}, {link:"http://www.etsy.com/forums_board.php?forum_id=4", title:"Business Topics"}, {link:"http://www.etsy.com/forums_board.php?forum_id=5000001", title:"Critiques"}, {link:"http://www.etsy.com/forums_board.php?forum_id=5000000", title:"Techniques &amp; Materials"}, {link:"http://www.etsy.com/forums_board.php?forum_id=5", title:"Teams &amp; Events"}, {link:"http://www.etsy.com/forums_board.php?forum_id=2", title:"Ideas"}, {link:"http://www.etsy.com/forums_board.php?forum_id=3", title:"Bugs"}, {link:"http://www.etsy.com/forums_board.php?forum_id=6", title:"Promotions"}, {link:"http://www.etsy.com/forums_board.php?forum_id=7", title:"Etc."}];

// if we have up-to-date data from the main forums page, use that
var fdata = GM_getValue( 'forums_array' );
if ( fdata != null ) {
    forums = eval( fdata );
}

var links = document.getElementsByTagName( 'a' );

if ( document.location.href.indexOf( 'forums_main.php' ) > -1 ) {
    // on the main forums page - make sure the list of available forums is up-to-date
    forums = [];
    for ( var a = 0; a < links.length; a++ ) {
        var link = links[ a ];
        if ( link.href && link.href.indexOf( 'forums_board.php' ) > -1 ) {
            if ( link.innerHTML.indexOf( 'View all posts' ) == -1 ) {
                // found the "title" link to the main forum - store it
                forums.push( { link: link.href, title: link.innerHTML } );
            }
        }
    }
    // stash the value for future use
    updateForumIds();
    GM_setValue( 'forums_array', uneval( forums ) );
}

if ( forums[ 0 ] && forums[ 0 ].id == null ) updateForumIds();

// seek the current nav bar, so we can add ours after it
var navTr;
var currentForum = "";
for ( var a = 0; a < links.length; a++ ) {
    var link = links[ a ];
    if ( link.href && link.href.indexOf( 'community.php' ) > -1 && link.parentNode && link.parentNode.className == 'dark_grey_text' ) {
        // found the nav bar - stash it for later
        navTr = link.parentNode.parentNode;
        var navLinks = navTr.getElementsByTagName( 'a' );
        // loop through the nav bar, looking for the current forum if there is one
        for ( var l = 0; l < navLinks.length; l++ ) {
            if ( navLinks[ l ].href && navLinks[ l ].href.indexOf( 'forums_board.php' ) > -1 ) {
                // found one - stash it too
                currentForum = navLinks[ l ].innerHTML;
                break;
            }
        }
        break;
    }
}    

if ( navTr && forums.length > 0 ) {
    // going to make our nav bar just like the current one
    var newNav = navTr.cloneNode( true );
    var navLinks = [];
    // create the links
    for ( var f = 0; f < forums.length; f++ ) {
        var style = "";
        var fid = new RegExp( 'forum_id=' + forums[ f ].id + '$' );
        if ( currentForum == forums[ f ].title || document.location.href.match( fid ) ) {
            // current forum - highlight it
            style = ' style="font-weight: bold"';
        }
        // and create the link HTML
        navLinks.push( '<a href="' + forums[ f ].link + '"' + style + '>' + forums[ f ].title + '</a>' );
    }
    // add an extra item for "threads I've posted in"
    var style = "";
    if ( document.location.href.indexOf( 'forums_user_threads.php' ) > -1 ) {
        // current page - make it bold
        style = ' style="font-weight: bold"';
    }
    navLinks.push( '<a href="http://www.etsy.com/forums_user_threads.php"' + style + '>My Topics</a>' );
    // add the HTML to the navbar
    newNav.cells[ 0 ].innerHTML = navLinks.join( ' &bull; ' );

    var table = navTr.parentNode;
    var sepRow = table.rows[ navTr.rowIndex + 1 ];
    table.insertBefore( sepRow.cloneNode( true ), sepRow ); // add a dotted separator line
    table.insertBefore( newNav, sepRow ); // add our new navbar
    var footerSpace = document.createElement( 'tr' );
    footerSpace.insertCell( 0 );
    footerSpace.cells[ 0 ].height = '20px';

    table.appendChild( footerSpace );
    table.appendChild( sepRow.cloneNode( true ) );
    table.appendChild( newNav.cloneNode( true ) );
    table.appendChild( sepRow.cloneNode( true ) );
}    

// utility function to replicate getElementsByClassName() on older Firefoxes
function getElementsByClassName ( class, node ) {
    if ( node == null ) node = document;
    if ( node.getElementsByClassName ) {
        return node.getElementsByClassName( class );
    } else {
        var classElements = new Array();
        var els = node.getElementsByTagName( '*' );
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+class+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
            if ( pattern.test(els[i].className) ) {
                classElements[j] = els[i];
                j++;
            }
        }
        return classElements;
    }
}

function updateForumIds () {
    for ( var f = 0; f < forums.length; f++ ) {
        var match = forums[ f ].link.match( /forum_id=(\d+)/ );
        if ( match ) {
            forums[ f ].id = match[ 1 ]
        }
    }
}

