// ==UserScript==
// @name                Edit This Item
// @version             2.0
// @date                2009-01-19
// @author              Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace           etsy.com
// @description         Adds links to edit and delete 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 links to
edit and delete the item to the right-hand panel, under the "report to Etsy" link.
*/

// data URI for delete icon; we'll use a standard Etsy icon for delete
var ximg = "data:image/gif,GIF89a%0C%00%0C%00%E6%00%00%FFslc%00%00%E5KA%80%00%00%83%00%00%F92%00l%00%00%8E%00%00d%00%00%93%00%00%E5%3D%00k%04%00%FFk%0B%C4648%1F%22%F2%2B%004%18.Q%00%0Fa%05%08%E4J%00%FF%83r%FF%DE%D5o%00%00%3F%0C%11-%1B%1B%E6%2C%00%F3%60%3EA%13%16%F5.%00%FFzp%FF%D5%C9%F9%FF%E6%F4E%00%FFUN%FF%E3%E6%FF%F4%FF%BF1%2F%FF%DC%DFT%20-%FF%F2%ED%AC9%3C%E6%F8%FF%C2*%1F%F0%FF%FF%DF%3E%00%BB%2F%20y%00%00%FF%DA%D5%C242%FF%DA%D4%FFH%00%86%09%03Z%00%00%C77%1C%FF%8F%84%FF%EF%F4%FFX%00%FF%E6%E2%FA%E1%E4z%00%00-%10%26%FF%F3%F1%CF5%2B%FFL%00%9D%00%00%92%00%00%E4LA%FF%98%40%FB~x%CE_K%FD%EB%EB%FF%E3%F6d%15%10%85%04%00%FF%892)%00%00%FBL%00%FF%D6%C0%8A%00%00%D6U%40%D8*%00%FF%FF%FF%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00!%F9%04%00%00%00%00%00%2C%00%00%00%00%0C%00%0C%00%00%07g%80Q%82%83%84%85%3C%2B%83%0E%3A%1F%82%23%40%00IM1.%1D%077%82%10%00!O%3B3D%02%3E%17%83%0B%1A%0C%0AJ%205H)%83%15%03%138PLN%2F%84%22%09C%19%3F%1C%04G%84%12%14%2C2%05%0F-%11%83%186*E%06%01(0%24K%82FAB4%1E%25%16%0D%08%3D%839%26%84%1B'%85%E4%83%81%00%3B";

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;

            // alter the report link to become the "edit item" link
            var editRow = reportRow.cloneNode( true );
            var editLink = editRow.getElementsByTagName( 'a' )[ 0 ];
            editLink.innerHTML = 'Edit this item';
            editLink.href = '/reactivate_listing.php?listing_id=' + listing_id;

            // tweak the icon
            var editImg = editRow.getElementsByTagName( 'img' )[ 0 ];
            editImg.src = '/images/icon_pencil.gif';
            editImg.alt = 'Edit this item';
            editImg.width = 15;
            editImg.height = 15;

            // alter the report link to become the "delete item" link
            var deleteRow = reportRow.cloneNode( true );
            var deleteLink = deleteRow.getElementsByTagName( 'a' )[ 0 ];
            deleteLink.innerHTML = 'Delete this item';
            deleteLink.href = '/listing_remove.php?delete_listing_id=' + listing_id;

            // tweak the icon
            var deleteImg = deleteRow.getElementsByTagName( 'img' )[ 0 ];
            deleteImg.src = ximg;
            deleteImg.alt = 'Delete this item';
            deleteImg.width = 12;
            deleteImg.height = 12;
            deleteImg.style.margin = '2px 1px';

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

