// ==UserScript==
// @name                Etsy buyer info
// @version             5.0
// @date                2010-04-10
// @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==
//

emulateGM( true );

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 = true;

// find user links - decorate each user with suitable links
var links = document.getElementsByTagName( 'a' );
for ( var l = links.length - 1; l >= 0; l-- ) {
    var link = links[ l ];
    if ( link.href && link.href.indexOf( '/people/' ) > -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 += '/feedback';
    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>';
}


// emulate some GM_ functions for Opera and Chrome
// includes code for Chrome support by James Campos which is
// copyright      2009, James Campos
// license        cc-by-3.0; http://creativecommons.org/licenses/by/3.0/

function emulateGM ( use_cookies ) {
    if ( use_cookies == null ) use_cookies = false;
    if ((typeof GM_getValue == 'undefined') || (GM_getValue('a', 'b') == null)) {
        GM_registerMenuCommand = function(name, funk) {
            //todo
        }

        GM_addStyle = function(css) {
            var style = document.createElement('style');
            style.textContent = css;
            document.getElementsByTagName('head')[0].appendChild(style);
        }

        GM_log = function(message) {
            console.log(message);
        }

        if ( navigator && navigator.userAgent.indexOf( 'Chrome' ) > -1 ) {
            GM_deleteValue = function(name) {
                localStorage.removeItem(name);
            }

            GM_getValue = function(name, defaultValue) {
                var value = localStorage.getItem(name);
                if (!value)
                    return defaultValue;
                var type = value[0];
                value = value.substring(1);
                switch (type) {
                    case 'b':
                        return value == 'true';
                    case 'n':
                        return Number(value);
                    default:
                        return value;
                }
            }

            GM_setValue = function(name, value) {
                value = (typeof value)[0] + value;
                localStorage.setItem(name, value);
            }
        } else if ( window.opera ) {
            if ( use_cookies ) {
                GM_getValue = function( name, defaultValue ) {
                    var cookies = document.cookie.split( "; " );
                    for( var c = 0; c < cookies.length; c++ ) {
                        var cookie = cookies[c].split( "=" );
                        if( cookie[0] == name ) {
                            return unescape( cookie[1] );
                        }
                    }
                    return defaultValue;
                }
                
                GM_setValue = function( name, value ) {
                    var now = new Date;
                    var expires = new Date( now.getTime() + 31536000000 ).toGMTString();
                    document.cookie = escape( name ) + "=" + escape( value ) + ";expires=" + expires + ";path=/"; 
                }
            }
        }
    }
}


