// ==UserScript==
// @name                Edit This Item
// @version             3.2
// @date                2009-11-11
// @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 ];

        var ul = getElementsByClassName( 'page-tools' )[ 0 ];
        var li = document.createElement( 'li' );

        // tweak the icon
        var editImg = document.createElement( 'span' );
        editImg.style.backgroundImage = "url( '/images/icon_pencil.gif' )";
        editImg.className = 'icon';
        li.appendChild( editImg );
        
        var container = document.createElement( 'div' );
        container.style.position = 'relative';
        li.appendChild( container );

        var editLink = document.createElement( 'a' );
        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.left = '21px';
        dropdown.style.width = '175px';
        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 );
        }

        container.appendChild( editLink );
        container.insertBefore( dropdown, editLink );
        editLink.addEventListener( 'mouseover', function () { dropdown.style.display = ''; }, false );
        ul.appendChild( li );

        var delLi = document.createElement( 'li' );
        var delImg = document.createElement( 'span' );
        delImg.style.backgroundImage = "none";
        delImg.className = 'icon';
        delImg.innerHTML = '<img src="' + ximg + '" style="margin: 2px 1px" width="12" height="12" />';
        var delLink = document.createElement( 'a' );
        delLink.innerHTML = 'Delete this item';
        delLink.href = '/listing_remove.php?delete_listing_id=' + listing_id;
        delLi.appendChild( delLink );
        ul.appendChild( delLi );
    }
}

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

// utility function to replicate getElementsByClassName() on older Firefoxes
function getElementsByClassName ( class, node ) {
    if ( node == null ) node = document;
    if ( node.getElementsByClassName ) {
        return node.getElementsByClassName( class );
    } else {
        var classElements = new Array();
        var els = node.getElementsByTagName( '*' );
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+class+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
            if ( pattern.test(els[i].className) ) {
                classElements[j] = els[i];
                j++;
            }
        }
        return classElements;
    }
}

