// ==UserScript==
// @name                Renew listing
// @version             1.0
// @date                2009-01-21
// @author              Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace           etsy.com
// @description         Adds link to renew the item when viewing an item in your shop
// @include             http://www.etsy.com/view_listing.php*
// ==/UserScript==

/*
This script will first check to see if you're viewing an item in your own shop. If you are, then it adds a link to
renew the item to the right-hand panel, under the "report to Etsy" link.
*/

var refreshUri = "data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%10%00%00%00%10%08%06%00%00%00%1F%F3%FFa%00%00%00%01sRGB%00%AE%CE%1C%E9%00%00%00%06bKGD%00%FF%00%FF%00%FF%A0%BD%A7%93%00%00%00%09pHYs%00%00%0B%12%00%00%0B%12%01%D2%DD~%FC%00%00%00%07tIME%07%D9%01%15%13'%3A%A1%EB%1F%A4%00%00%02SIDAT8%CB%B5%93%CFK%D3a%1C%C7%DF%CF%F3%9D%FB%F1u%EBkN%A6)%9AZ%986%05%A3Qh%3F(%93%D1%A5Ct%ED%14%04u%10%0A%82%88.%5D%F2%14%9D%22%FB%03%3A%D4%25%94%0A%3CH%19%8A%2B%17%B2%DA%94a%5B%C5l%BA%A9%9B%CE%7D%F7%DD%F7%D7%9E%A7%93c%85%11!%BDo%9F%C3%E7%C5%E7%F5%7C%3E%0F9%FF%C0%85%DD%84b%97%B1T%16%5Dm%BDw%0CC%BF%E8%AA%DE%DB%CD%98%B9%A9%19%EA%94%DD*%8E%84%16%03%93%7F%02%90m%85%F6%A6%CE%87%DE%E6c7%0E7%F6%11%C6%19l%16%11%8C%97%F06%FAl%BD%A8%17n%86%16%03Ow%02%08%07%FD64%D7%B7%9Dn%AD%EF%7C%229%EA%C8%86%92%86j%16%90W3P%F4%1Cz%9AN%89k%F2%8FAw%8D'%BA%B2%9E%88%EE%A8%40%08%BD%E6q5%93%E4f%DC%C8%177%A6%01%14%24%D1%7D%C6%DB%D4%E7%CC%14%928%D22%B0%E7%7D%FC%D5poG%DF%B8nj%AC%D6%E9%19%2F1s%3E%10%99%18%A2%00%209k%FB3%85%15%9E%CA%26.%85c%C1%81p%2Cx!%A7d%BC%A1%C4%BBe%9BE%04%E7%25%1Cm%1D%ECrX%AB_PBo%5D%E8%B9~V%12%EB%AE%96%B7%60%11%AC%9E%AC%9C%0E~MF_n%8F%16%8E%05%13%AA%5E%F0GSA%5D%A0U%A8%B6J%90%C4%BA%93%ED%9E%EE%BB%94P%A8%86%B2T%06%08T0%14U%9E%FE%DD%EF%D3%97%0F%F3Y9%F5%BA%C4%0C0%CE%E0m%ECw%F9Z%FCb%D1%C8C%D1%B66%CBo%A0h%F2w%CEY%B2%B2%F9%B8w%E0%B6nj%5D%8A%26%9B%F3%CB%01%F8%F6%FB%C18%85%CE4%C8Z%16%84%D0%E52%A0%A8%CA%B3n%A9%A1c%BB%B9%FB%80%CF%B6Oj%BD%7F%EE%D0e!W%5C%83%C9%0D%00%80f*%A0%84b5%9F%E0%9C%B3PY%C1-5%ACU%5Ee%24%FEQKo%25FF%3F%3F%DAZH%CF%C0d%06Lf%40%A0U%B0R%3BrJ%A6%08%60%AE%0CXZ%8D%DDsX%9D%A9J%85%40db(%A7%AC7%C6W%C3%8Fg%E2c%DF%22%C9)n%17D8%AA%5C%D0Kjrvar%EC%97K%FC%5B%FA%7B%FC'(%A1%C3N%7B%8DO7%D5%E7o%E6F%AF%FC%13%E0%BF%FD%C6%9F%F8i%0E%B9%EAQ%0Cf%00%00%00%00IEND%AEB%60%82";

var links = document.getElementsByTagName( 'a' );
var shopPattern = /shop.php\?user_id=(\d+)/; // find shop links

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[ 1 ];
            } else if ( link.firstChild && link.firstChild.data && link.firstChild.data == 'shop' ) {
                // this is the link to the shop in the "seller info" box
                thisShop = match[ 1 ];
                // if we find this, we're done
                break;
            }
        }
    }
}

if ( yourShop == thisShop ) {
    // it's our item
    // find the listing ID
    var match = window.location.search.match( /listing_id=(\d+)/ );
    var listing_id = match[ 1 ];

    // search for the "report to Etsy" link
    for ( 1; l < links.length; l++ ) {
        var link = links[ l ];
        if ( link.firstChild && link.firstChild.data && link.firstChild.data == 'Report this item to Etsy' ) {
            reportRow = link.parentNode;
            while ( reportRow && reportRow.nodeName != 'TR' ) reportRow = reportRow.parentNode;

            // form to send the relisting data to the relisting component
            var form = document.createElement( 'form' );
            form.action = "http://www.etsy.com/renew_listings_expiring.php";
            form.method = "POST";
            form.innerHTML = '<input type="hidden" name="renew_listing_id[]" value="' + listing_id + '" /><input type="hidden" name="submit" value="renew" />';
            document.body.appendChild( form );
            
            // alter the report link to become the "relist item" link
            var relistRow = reportRow.cloneNode( true );
            var relistLink = relistRow.getElementsByTagName( 'a' )[ 0 ];
            relistLink.innerHTML = 'Renew this item';
            relistLink.addEventListener( 'click', function () { form.submit() }, false ); // only parse the page on click
            relistLink.style.cursor = 'pointer';
            relistLink.removeAttribute( 'href' ); // going to use an onclick handler instead

            // tweak the icon
            var relistImg = relistRow.getElementsByTagName( 'img' )[ 0 ];
            relistImg.src = refreshUri;
            relistImg.alt = 'Renew this item';
            relistImg.width = 15;
            relistImg.height = 15;

            // attach to document, under the "report to Etsy" link
            reportRow.parentNode.insertBefore( relistRow, reportRow.nextSibling );
            break;
        }
    }
}



