// ==UserScript==
// @name                Etsy buyer info
// @version             4.0
// @date                2009-04-30
// @author              Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace           etsy.com
// @description         Adds links to buyer's email, convos, and feedback on to the Etsy receipt page for an order
// @include             http://www.etsy.com/receipt.php?order_id=*
// ==/UserScript==
//

GM_registerMenuCommand( 'Toggle visible email address', function () {
    showEmail = ! showEmail;
    GM_setValue( 'showEmail', showEmail );
    var mtl = document.getElementById( 'mailtoLink' );
    if ( mtl != null ) {
        mtl.innerHTML = ( showEmail ) ? emailAddress : 'email';
    }
} );

var showEmail = GM_getValue( 'showEmail' );
if ( showEmail == null ) showEmail = false;

// find user links - decorate each user with suitable links
var links = document.getElementsByTagName( 'a' );
for ( var l = 0; l < links.length; l++ ) {
    var link = links[ l ];
    if ( link.href && link.href.indexOf( '/profile.php' ) > -1 ) {
        decorateUser( link );
    }
}

function decorateUser ( userLink ) {
    var username = userLink.innerHTML;
    var table = userLink.parentNode.parentNode.parentNode;

    // Make feedback rating a hyperlink to their feedback page
    var feedback = userLink.href.replace( 'profile', 'feedback_public' );
    var feedbackTd = table.rows[ 1 ].cells[ 0 ];
    var rating = feedbackTd.innerHTML.substring( 13 );
    feedbackTd.innerHTML = 'Etsy rating: <a href="' + feedback + '">' + rating + '</a>';

    // Make their email address a hyperlink and add convo links
    var mailtoTd = table.rows[ 3 ].cells[ 0 ];
    var emailAddress = mailtoTd.innerHTML;
    var emailText = ( showEmail ) ? emailAddress : 'email';
    mailtoTd.innerHTML = '<a id="mailtoLink" href="mailto:' + mailtoTd.innerHTML + '">' + emailText + '</a> | <a href="convo_new.php?to_username=' + username + '">send convo</a> | <a href="convo_drop_search.php?drop_user_name=' + username + '&drop_tag=&submit=Go">convos</a>';
}


