// ==UserScript==
// @name                Edit This Item
// @version             3.1
// @date                2009-03-30
// @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*
// @include             http://www.etsy.com/edit_listing5.php?listing_id=*
// ==/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.
*/

if ( document.location.href.indexOf( 'edit_listing5.php' ) > -1 ) {
    var match = window.location.search.match( /listing_id=(\d+)/ );
    var listing_id = match[ 1 ];
    // check to see if we have a "go to page" flag set
    var goto = GM_getValue( listing_id );
    if ( goto ) {
        // we do - clear the flag, and go to the page
        GM_setValue( listing_id, "" );
        document.location = document.location.href.replace( 'edit_listing5', 'edit_listing' + goto );
    }
} else {    
    // on the View Listing page - add the edit links

    // data URI for delete icon; we'll use a standard Etsy icon for edit
    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.indexOf( "'s shop" ) > -1 ) {
                    // 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;

                // build the drop-down menu of edit pages to jump to
                var dropdown = document.createElement( 'div' );
                dropdown.id = 'edit_dropdown';
                dropdown.style.position = 'absolute';
                dropdown.style.marginLeft = '-6px';
                dropdown.style.marginTop = '-6px';
                dropdown.style.border = '1px solid #cccccc';
                dropdown.style.padding = '5px';
                dropdown.style.background = '#f9f9f7';
                dropdown.style.display = 'none';
                dropdown.addEventListener( 'mouseout',  function ( event ) { if ( event.relatedTarget.id != 'edit_dropdown' && event.relatedTarget.parentNode.id != 'edit_dropdown' ) { dropdown.style.display = 'none'; } }, true );
                dropdown.appendChild( editLink.cloneNode( true ) );
                dropdown.firstChild.style.display = 'block';
                dropdown.firstChild.style.marginBottom = '5px';
                // add the links to edit specific pages
                var editNav = [ { title: 'Edit title, description, materials', page: 1 }, { title: 'Edit tags', page: 2 }, { title: 'Edit price, quantity, shipping', page: 3 }, { title: 'Edit pictures', page: 4 } ];
                for ( var e = 0; e < editNav.length; e++ ) {
                    if ( e > 0 ) dropdown.appendChild( document.createElement( 'br' ) );
                    var elnk = document.createElement( 'a' );
                    dropdown.appendChild( document.createTextNode( String.fromCharCode( '8226' ) + ' ' ) );
                    elnk.innerHTML = editNav[ e ].title;
                    elnk.href = '/reactivate_listing.php?listing_id=' + listing_id; // still need to go here to get the listing into "edit" mode
                    // listener sets the "jump to page X" flag
                    elnk.addEventListener( 'click', genEditClick( listing_id, editNav[ e ].page ), true );
                    dropdown.appendChild( elnk );
                }

                editLink.parentNode.insertBefore( dropdown, editLink );
                editLink.addEventListener( 'mouseover', function () { dropdown.style.display = ''; }, false );

                // 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;
            }
        }
    }
}

function genEditClick ( listing_id, page ) {
    return function () { GM_setValue( listing_id, page ) };
}    

