// ==UserScript==
// @name           Relisted items
// @version        1.1
// @date           2009-07-17
// @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/sold_orders.php*
// ==/UserScript==

var username = whoami();
if ( username != null ) {
    getListings();
}    

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://gm.etsyhacks.com/contents/' + 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' );
    for ( var l = 0; l < links.length; l++ ) {
        var link = links [ l ];
        if ( link.href && link.href.indexOf( 'view_transaction.php' ) > -1 ) {
            var imgs = link.getElementsByTagName( 'img' );
            if ( imgs.length ) {
                var img = imgs[ 0 ];
                var title = img.alt;
                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';
                icon.style.color = ( forSale[ title ] != null ) ? '#009900' : '#990000';
                icon.innerHTML = ( forSale[ title ] != null ) ? forSale[ title ] : 0;
                link.insertBefore( icon, img );
            }
        }
    }
}    


// find out who I'm logged in as
function whoami () {
    // find the user name by parsing links
    var links = document.getElementsByTagName( 'a' );
    for ( var l = 0; l < links.length; l++ ) {
        var link = links[ l ];
        if ( link.href ) {
            if ( link.href.indexOf( 'login.php' ) > -1 ) {
                // not logged in - no username
                return null;
            }
            if ( link.href.indexOf( 'your_etsy.php' ) > -1  ){
                // username is next to the Your Etsy link
                var cell = link.parentNode;
                var text = cell.parentNode.cells[ cell.cellIndex - 1 ].innerHTML;
                var match = text.match( /Happy Birthday (.+)!/ );
                text = ( match ) ? match[ 1 ] : text.substring( 0, text.indexOf( ':' ) );
                return text;
            }
         }
     }
}

