// ==UserScript==
// @name           Section browser
// @version        1.0
// @date           2009-07-09
// @author         Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace      etsy.com
// @description    Add a "other items in section" browser to the View Listing page
// @include        http://www.etsy.com/view_listing.php?*
// ==/UserScript==

var dataLength = 4;
decoratePage();

function decoratePage () {
    var links = document.getElementsByTagName( 'a' );
    for ( var l = 0; l < links.length; l++ ) {
        var link = links[ l ];
        if ( link.href ) {
            var match = link.href.match( /shop.php\?user_id=(\d+)&section_id=(\d+)/ );
            if ( match ) {
                var user_id = match[ 1 ];
                var section_id = match[ 2 ];
                var section_name = link.innerHTML;
                var vdgt = document.getElementsByClassName( 'very_dark_grey_text' );
                for ( var v = 0; v < vdgt.length; v++ ) {
                    if ( vdgt[ v ].innerHTML.indexOf( "seller's other items" ) > -1 ) {
                        var lastMod = Number( GM_getValue( user_id + '_' + section_id + '_lastMod' ) || 0 );
                        var now = new Date();
                        if ( now.valueOf() - lastMod > 300000 ) {
                            getData( vdgt[ v ], user_id, section_id, section_name );
                        } else {
                            addSectionBrowser( vdgt[ v ], user_id, section_id, section_name );
                        }
                        return;
                    }
                }
                return;
            }
        }
    }
}

function getData ( node, user_id, section_id, section_name ) {
    GM_xmlhttpRequest( {
        method: 'GET',
        url: 'http://gm.etsyhacks.com/sections/' + user_id + '/' + section_id,
        onload: function ( response ) { storeData( node, user_id, section_id, section_name, response ) }
    } );
}

function storeData ( node, user_id, section_id, section_name, response ) {
    if ( response.status == 200 ) {
        GM_setValue( user_id + '_' + section_id, '(' + response.responseText + ')' );
        var now = new Date();
        GM_setValue( user_id + '_' + section_id + '_lastMod', String( now.valueOf() ) );
        addSectionBrowser( node, user_id, section_id, section_name );
    } else {
        return;
    }
}

function addSectionBrowser ( node, user_id, section_id, section_name ) {
    var data = eval( GM_getValue( user_id + '_' + section_id ) || '([])' );
    var title = node.parentNode;
    var table = title.parentNode;
    var display = table.rows[ title.rowIndex + 1 ];
    var sectionTitle = title.cloneNode( true );
    sectionTitle.cells[ 0 ].innerHTML = section_name;
    var sectionDisplay = display.cloneNode( true );
    var listing_id = document.location.href.match( /listing_id=(\d+)/ )[ 1 ];
    var i = 0;
    while ( i < data.length && data[ i ] != listing_id ) i += dataLength;
    var imgs = sectionDisplay.getElementsByTagName( 'img' );
    var next = i - dataLength;
    var prev = i + dataLength;
    var url, itemLink;
    if ( data[ prev ] != null ) {
        url = imgUrl( data, prev );
        itemLink = 'http://www.etsy.com/view_listing.php?listing_id=' + data[ prev ] + '&ref=vl_other_1';
    } else {
        url = 'http://www.etsy.com/images/grey.gif';
        itemLink = null;
    }
    addNav( imgs[ 1 ], imgs[ 0 ], url, itemLink, data[ prev + 1 ] );
    if ( data[ next ] != null ) {
        url = imgUrl( data, next );
        itemLink = 'http://www.etsy.com/view_listing.php?listing_id=' + data[ next ] + '&ref=vl_other_2';
    } else {
        url = 'http://www.etsy.com/images/grey.gif';
        itemLink = null;
    }
    addNav( imgs[ 2 ], imgs[ 3 ], url, itemLink, data[ next + 1 ] );
    var itemCount = ( data.length / 4 );
    var a = sectionDisplay.getElementsByTagName( 'a' );
    a = a[ a.length - 1 ];
    a.innerHTML = itemCount + ' item' + ( ( itemCount == 1 ) ? '' : 's' ) + ' for sale';
    a.href = 'http://www.etsy.com/shop.php?user_id=' + user_id + '&section_id=' + section_id;
    table.insertBefore( sectionDisplay, display.nextSibling );
    table.insertBefore( sectionTitle, display.nextSibling );
}

function addNav( img, arrow, imgUrl, itemUrl, alt ) {
    addLink( img, itemUrl );
    addLink( arrow, itemUrl );
    img.src = 'http://www.etsy.com/images/grey.gif';
    img.src = imgUrl;
    img.alt = alt;
}

function addLink( node, url ) {
    if ( node.parentNode.nodeName == 'A' ) {
        if ( url == null ) {
            node.parentNode.parentNode.replaceChild( node, node.parentNode );
        } else {
            node.parentNode.href = url;
        }
    } else {
        if ( url == null ) {
        } else {
            var a = document.createElement( 'a' );
            a.href = url;
            node.parentNode.insertBefore( a, node );
            a.appendChild( node );
        }
    }
}    

function imgUrl ( data, idx ) {
    return ( data[ idx + 3 ] == null ) ? data[ idx + 2 ] : 'http://' + data[ idx + 2 ] + '.etsy.com/il_75x75.' + data[ idx + 3 ] + '.jpg';
}    

