// ==UserScript==
// @name           Relisted items
// @version        2.5
// @date           2010-11-01
// @author         Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace      etsy.com
// @description    Show how many of an items are currently for sale when viewing your Sold Orders page.
// @include        http://www.etsy.com/your/listings/sold*
// @include        http://www.etsy.com/your/listings
// @include        http://www.etsy.com/your/listings?*
// ==/UserScript==

var username = whoami();
if ( username != null ) {
    if ( document.location.href.indexOf( 'your/listings/sold' ) > -1 ) {
        getListings();
    } else {
        var enableOnShop = GM_getValue( username + '_enableOnShop' );
        if ( enableOnShop ) {
            GM_registerMenuCommand( 'Disable relisted items hack on Your Shop pages', enableShop );
            getListings();
        } else {
            GM_registerMenuCommand( 'Enable relisted items hack on Your Shop pages', enableShop );
        }
    }
}    

function enableShop () {
    if ( username ) {
        var enableOnShop = GM_getValue( username + '_enableOnShop' );
        GM_setValue( username + '_enableOnShop', ! enableOnShop );
        location.reload();
    }
}    

function getListings () {
    var now = new Date;
    var lastUpdate = GM_getValue( username + '_lastUpdate' ) || "0";
    lastUpdate = Number( lastUpdate );
    if ( lastUpdate == null || ( now.valueOf() - lastUpdate ) > 300000 ) {
        GM_xmlhttpRequest( {
            method: 'GET',
            url: 'http://www.indecorous.com/hacks/content.cgi?login_name=' + username,
            onload: storeData
        } );
    } else {
        decoratePage();
    }
}

function storeData ( response ) {
    if ( response.status == 200 ) {
        var now = new Date;
        GM_setValue( username + '_forSale', '(' + response.responseText + ')' );
        GM_setValue( username + '_lastUpdate', String( now.valueOf() ) );
        decoratePage();
    }
}

function decoratePage () {
    document.getElementsByTagName( 'head' )[ 0 ].innerHTML += '<style type="text/css">\n#search_results_div { z-index: 1000 }\n</style>';
    var forSale = eval( GM_getValue( username + '_forSale' ) || '({})' );
    var links = document.getElementsByTagName( 'a' );
    var seek = '/transaction/';
    if ( document.location.href.indexOf( 'your/listings/sold' ) == -1 ) {
        seek = '/listing/';
    }
    for ( var l = 0; l < links.length; l++ ) {
        var imgNode;
        var link = links [ l ];
        if ( link.href && link.href.indexOf( seek ) > -1 ) {
            var node = link;
            //if ( seek == '/listing/' ) node = link.parentNode.parentNode;
            var imgs = node.getElementsByTagName( 'img' );
            if ( imgs.length ) {
                imgNode = imgs[ 0 ];
            } else {
                var title = link.innerHTML;
                var icon = document.createElement( 'div' );
                icon.style.width = '14px';
                icon.style.height = '14px';
                icon.style.textAlign ='center';
                icon.style.fontSize = '11px';
                icon.style.fontWeight = 'bold';
                icon.style.position = 'absolute';
                icon.style.margin = '1px';
                icon.style.border = '1px solid #000000';
                icon.style.background = '#ffffff';
                if ( seek == '/transaction/' || ( seek == '/listing/' && forSale[ title ] && forSale[ title ] > 1 ) ) {
                    icon.style.color = ( forSale[ title ] != null ) ? '#009900' : '#990000';
                    icon.innerHTML = ( forSale[ title ] != null ) ? forSale[ title ] : 0;
                    imgNode.parentNode.insertBefore( icon, imgNode );
                }
            }
        }
    }
}    


// find out who I'm logged in as
function whoami () {
    // find the user name by parsing links
    var nav = document.getElementById('user-nav');
    var link = getElementsByClassName('profile-link', nav);
    if (link.length) {
        text = link[0].href.match(/people\/(\w+)/)[1];
        GM_setValue( 'username', text );
        return text;
    } else {
        return null;
     }
}


