// ==UserScript==
// @name                Etsyhacks updates
// @version             1.3
// @date                2009-03-17
// @author              Ian Malpass ( ian AT etsyhacks DOT com )
// @namespace           etsy.com
// @description         Checks for new and updated etsyhacks
// @include             http://www.etsy.com/*
// @exclude             http://www.etsy.com/etsy_mini*
// @exclude             http://www.etsy.com/receipt_print.php*
// @exclude             http://www.etsy.com/header_storque.php*
// @exclude             http://www.etsy.com/footer_storque.php*
// ==/UserScript==

var lastUpdate = GM_getValue( 'lastUpdateLocal' );
if ( lastUpdate == null ) lastUpdate = 0;
var now = new Date;
if ( ( now.valueOf() - lastUpdate ) < 3600000 ) {
    // checked recently - just display any updates that haven't been cleared
    displayUpdate();
} else {
    // check for updates, then display
    checkForUpdates();
}

function displayUpdate () {
    var updates = GM_getValue( 'updates' );
    if ( updates == null ) return;
    updates = eval( updates );
    if ( updates.length == 0 ) return;

    var ximg = "data:image/gif,GIF89a%0C%00%0C%00%C6J%00)%00%00Q%00%0FZ%00%00c%00%00d%00%00l%00%00%3F%0C%11o%00%00-%10%26a%05%08k%04%00y%00%00z%00%00%80%00%00%83%00%00A%13%16%8A%00%00%8E%00%00-%1B%1B%92%00%00%85%04%00%93%00%004%18.%9D%00%00%86%09%038%1F%22d%15%10T%20-%C2*%1F%BB%2F%20%D8*%00%BF1%2F%E6%2C%00%AC9%3C%C242%F2%2B%00%C77%1C%C464%F5.%00%CF5%2B%F92%00%DF%3E%00%E5%3D%00%F4E%00%E4J%00%FFH%00%E5KA%E4LA%FBL%00%FFL%00%D6U%40%FFX%00%CE_K%FFUN%F3%60%3E%FFk%0B%FFsl%FFzp%FB~x%FF%892%FF%83r%FF%8F%84%FF%98%40%FF%D5%C9%FF%D6%C0%FF%DA%D4%FF%DA%D5%FF%DC%DF%FF%DE%D5%FD%EA%EB%FF%EE%EF%FF%EE%F1%FF%F0%F0%E6%F8%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%FF%2C%00%00%00%00%0C%00%0C%00%00%07_%80H%82%83%84%85%08%84%19E%84%178%14%40A%0B9%11%83%16852%0C%18%3A.'%06%83%0A67*%3B%2B%24%1AI%83D%0D%2C3%1E0%10B%84F%15%3E%201%26%0EG%84%09%3C)-(%23%1D%01%83%12%3D%1C4%05%03!%22%1F%00%83%13%2F%02%3FC%07%25%04%85%1B%84%0F%85%DC%84%81%00%3B";

    // create the 'updates' box
    var div = document.createElement( 'div' );
    div.style.position = 'absolute';
    div.style.top = '0px';
    div.style.textAlign= 'left';
    div.style.left = '0px';
    div.style.padding = '4px';
    div.style.background = 'rgb(245, 245, 241)';
    div.style.border = '1px solid #dddddd';
    div.innerHTML = '<div>Etsyhacks updates available</div>';

    // add the update links
    for ( var u = 0; u < updates.length; u++ ) {
        div.innerHTML += '<div><img src="' + ximg + '" style="cursor: pointer; float: left; padding-top: 1px;" width="12" height="12">&nbsp;<a target="_blank" href="http://www.etsyhacks.com/greasemonkey/' + updates[ u ].url + '/">' + updates[ u ].title + '</a></div>';
    }

    // add click handlers to remove specific updates
    var xs = div.getElementsByTagName( 'img' );
    for ( var x = 0; x < xs.length; x++ ) {
        xs[ x ].addEventListener( 'click', genX( updates[ x ], xs[ x ] ), true );
    }    

    // add the updates box to the page
    document.body.appendChild( div );
}    

// click handler to remove updates from the list
function genX( update, node ) {
    return function () {
        node.parentNode.style.display = 'none';
        var updates = GM_getValue( 'updates' );
        if ( updates == null ) return; // something screwy...
        updates = eval( updates );
        for ( var u = 0; u < updates.length; u++ ) {
            if ( updates[ u ].url == update.url && updates[ u ].title == update.title ) {
                // remove the update from the list
                updates.splice( u, 1 );
                break;
            }
        }
        if ( updates.length == 0 ) {
            // no more updates to display - hide the whole shebang
            node.parentNode.parentNode.style.display = 'none';
        }
        // stash our updated list of updates
        GM_setValue( 'updates', uneval( updates ) );
    }
}

function checkForUpdates () {
    // send the last time we checked as the argument (as specified by the server the last time we asked)
    var lastUpdate = GM_getValue( 'lastUpdate' ) || 0;
    // dispatch the request - do useful stuff when we get a reply
    GM_xmlhttpRequest( {
        method: 'GET',
        url: 'http://gm.etsyhacks.com/updates/' + lastUpdate,
        onload: parseUpdate
    } );
}

function parseUpdate ( response ) {
    if ( response.status == 200 ) {
        // get the existing list of updates, if any
        var updates = GM_getValue( 'updates' );
        if ( updates == null ) {
            updates = [];
        } else {
            updates = eval( updates );
        }
        // comma-separated response
        var bits = response.responseText.split( /,/ );
        GM_setValue( 'lastUpdate', bits[ 0 ] ); // first value is current server time
        // stash when we last checked in local time
        var date = new Date;
        GM_setValue( 'lastUpdateLocal', String( date.valueOf() ) );
        // add the updates
        for ( var u = 1; u < bits.length; u += 2 ) {
            updates.push( { url: bits[ u ], title: bits[ u + 1 ] } );
        }
        // stash the updates
        GM_setValue( 'updates', uneval( updates ) );
    }
    // display any updates we have on hand
    displayUpdate();
}

