// ==UserScript==
// @name                Shop tools
// @version             4.2
// @date                2009-11-16
// @author              Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace           etsy.com
// @description         Adds links to edit your shop to your shop and listings pages
// @include             http://www.etsy.com/view_listing.php?*
// @include             http://www.etsy.com/shop.php?*
// @include             http://www.etsy.com/shop/*
// @include             http://www.etsy.com/shop_policy.php?*
// ==/UserScript==

/*
This script will first check to see if you're viewing your own shop, or one of your items. 
If you are, then it adds a "shop tools" section to the right-hand navigation bar, under 
"page tools", with links to edit your shop's appearance, policies, etc.
It also adds a link to your "orders" page next to the link to your sold items list on your
shop pages.
*/

var links = document.getElementsByTagName( 'a' );
var shopPattern = /(profile|shop(_policy)?).php\?user_id=(\d+)/; // find shop for user ID extraction

var yourShop; // what's your shop ID?
var thisShop; // what's the current shop's ID?

var l = 0;
for ( 1; l < links.length; l++ ) {
    var link = links[ l ];
    if ( link.href ) {
        var match = shopPattern.exec( link.href );
        if ( match ) {
            // we have a shop link
            if ( link.title == 'Your Shop' ) {
                // this is the link to your shop in the top nav bar
                yourShop = match[ 3 ];
            } else if ( link.innerHTML == 'profile' ) {
                // this is the link to the shop policies in the "seller info" box on a listings page
                thisShop = match[ 3 ];
                // if we find this, we're done
                break;
            } else if ( link.firstChild && link.firstChild.data && link.firstChild.data == 'Shop Policies' ) {
                // this is the link to the shop policies on a shop page
                thisShop = match[ 3 ];
                // if we find this, we're done
                break;
            }
        }
    }
}

if ( yourShop == thisShop ) {
    // it *is* our shop - add the extra links

    // search through our array of links, looking for the "sold" section
    for ( 1; l < links.length; l++ ) {
        var link = links[ l ];
        if ( link.href && link.href.indexOf( 'shop_sold.php' ) > -1 ) {
            // found it - add the link to the orders page
            link.parentNode.innerHTML += ' (<a href="/sold_orders.php">orders</a>)';
            break
        }
    }

    var nav = [
        { label: 'Featured in shop',        url: '/rank_featured.php' },
        { label: 'Appearance',              url: '/shop_appearance.php' },
        { label: 'Policies',                url: '/shop_policy_edit.php' },
        { label: 'Sections',                url: '/sections.php' },
        { label: 'Batch sort sections',     url: '/sections_batch.php' },
        { label: 'Shipping options',        url: '/shipping_profile_edit.php' },
        { label: 'Apply shipping profiles', url: '/shipping_batch.php' },
        { label: 'Payment methods',         url: '/payment_profile.php' },
        { label: 'Vacation mode',           url: '/shop_vacation.php' }
    ];
    var box = document.getElementById( 'toolbox' );
    if ( box ) {
        var title = document.createElement( 'h3' );
        title.innerHTML = 'shop tools';
        box.appendChild( title );
        var ul = document.createElement( 'ul' );
        for ( var i = 0; i < nav.length; i++ ) {
            var li = document.createElement( 'li' );
            li.innerHTML = '<a href="' + nav[ i ].url + '">' + nav[ i ].label + '</a>';
            ul.appendChild( li );
        }
        ul.className = 'page-tools';
        box.appendChild( ul );
    } else {
        var tables = getElementsByClassName( 'grey_border' ); // right hand nav is in a "grey_border" table
        var node;
        for ( var t = 0; t < tables.length; t++ ) {
            if ( tables[ t ].nodeName == 'TABLE' && tables[ t ].rows[ 0 ].cells[ 0 ].innerHTML.match( /('s|seller|user) info/ ) ) {
                // found the info table - this is where we add the links
                node = tables[ t ].tBodies[ 0 ];
                break;
            }
        }
        for ( r = 0; r < node.rows.length; r++ ) {
            var html = node.rows[ r ].cells[ 0 ].innerHTML;
            if ( html.indexOf( 'page tools' ) > -1 || html.indexOf( 'user info' ) > -1 ) {
                // found the "page tools" section
                var newTitleRow = node.rows[ r ].cloneNode( true );
                var newLinksRow = node.rows[ r + 1 ].cloneNode( true );
                if ( node.rows[ r + 2 ] ) {
                    // we have extra rows below us
                    node.insertBefore( newTitleRow, node.rows[ r + 2 ] );
                    node.insertBefore( newLinksRow, node.rows[ r + 3 ] );
                } else {
                    // we're at the end of the table
                    node.appendChild( newTitleRow );
                    node.appendChild( newLinksRow );
                }
                // label the new section
                newTitleRow.cells[ 0 ].innerHTML = 'shop tools';
                // build the table of links for the new section
                var html = '<table>';
                for ( var i = 0; i < nav.length; i++ ) {
                    html += '<tr><td style="padding: 3px 0px 0px 5px;"><a href="' + nav[ i ].url + '">' + nav[ i ].label + '</a></td></tr>';
                }
                // and insert the new table into our section's table cell
                newLinksRow.cells[ 0 ].innerHTML = html;
                break;
            }
        }
    }
}

// utility function to replicate node.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;
    }
}


