// ==UserScript==
// @name           "Your Etsy" page navigation
// @version        1.2
// @date           2009-10-12
// @author         Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace      etsy.com
// @description    Adds page navigation to the top of your Your Etsy pages
// @include        http://www.etsy.com/your_shop.php*
// @include        http://www.etsy.com/sold_orders.php*
// @include        http://www.etsy.com/expiring_listings.php*
// @include        http://www.etsy.com/expired_listings.php*
// @include        http://www.etsy.com/your_shop_inactive.php*
// ==/UserScript==

// seek the "go to page" navigation input
var nodes = document.getElementsByTagName( 'input' );
for ( var n = nodes.length - 1; n >= 0; n-- ) {
    var node = nodes[ n ]
    if ( node.name && node.name == 'page_submit' ) {
        // found the node we want - work our way up the tree
        while ( node.nodeName != 'TD' || ( node.nodeName == 'TD' && node.className != 'centered_text' ) ) node = node.parentNode;
        var row = node.parentNode.cloneNode( true ); // found the navigation node - make a copy of it
        
        var navNodes = node.getElementsByTagName( 'input' );
        for ( var nv = 0; nv < navNodes.length; nv++ ) {
            var navNode = navNodes[ nv ];
            if ( navNode.name && navNode.name == 'page_submit' ) {
                newNavNode = document.createElement( 'input' );
                newNavNode.type = 'button';
                newNavNode.value = 'Go';
                newNavNode.addEventListener( 'click', genListener( navNodes[ nv - 1 ] ), true );
                navNode.parentNode.replaceChild( newNavNode, navNode );
            }
        }

        // continue on
        node = node.parentNode;
        while ( node.nodeName != 'TD' || ( node.nodeName == 'TD' && node.className != 'centered_text' ) ) node = node.parentNode;
        var table = node.parentNode.parentNode; // found the table we're going to add to

        // we're going to add after the last "..." separator row
        // except on the "inactive listings" page, when we add after the second-last
        var seek = ( document.location.href.indexOf( 'your_shop_inactive.php' ) > -1 ) ? 2 : 1;
        for ( var r = table.rows.length - 1; r >= 0; r-- ) {
            if ( table.rows[ r ].cells[ 0 ].style.height == '1px' ) {
                target = table.rows[ r + 2 ];
                seek--;
                if ( ! seek ) break; // found our separator
            }
        }
        // add the navigation
        table.insertBefore( row, target );
        // add the spacer if necessary
        if ( document.location.href.indexOf( 'your_shop.php' ) == -1 ) {
            var spacer = document.createElement( 'tr' );
            spacer.appendChild( document.createElement( 'td' ) );
            spacer.firstChild.style.height = '15px';
            table.insertBefore( spacer, target );
        }

        break;
    }
}    

function genListener( input ) {
    return function () {
        document.location.href = 'http://www.etsy.com' + document.location.pathname + '?page=' + input.value;
    }
}

