// Memeorandum Colors user script
// version 0.1
// 2008-10-07
// Created by Andy Baio, http://waxy.org/
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Memeorandum Colors", and click Uninstall.
//
// Thanks to Mark Pilgrim's Dive Into Greasemonkey for the guidance.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          Memeorandum Colors
// @namespace     http://waxy.org/2008/10/memeorandum_colors/
// @description   Color Memeorandum stories based on linking bias
// @include       http://memeorandum.com/*
// @include       http://www.memeorandum.com/*
// ==/UserScript==

req = GM_xmlhttpRequest({
    method: 'GET',
    headers: {
        'User-Agent': 'Mozilla/4.0 (compatible) Greasemonkey (gzip)',
        'Accept': 'application/xml,text/xml',
        'Accept-Encoding': 'gzip'
    },
    url: 'http://spreadsheets.google.com/feeds/list/pVNrsh7EqwD6HkFTmS3v9aw/od6/public/values',
    onload: function(responseDetails) {
        if (responseDetails.status == 200) {
            var parser = new DOMParser();
            var xmlDoc = parser.parseFromString(responseDetails.responseText, "application/xml");
            var entries = xmlDoc.getElementsByTagName('entry');

            var scores = [];
            for (var i = 0; i < entries.length; i++) {
                var url = entries[i].getElementsByTagName('gsx:url')[0].textContent;
                var score = entries[i].getElementsByTagName('gsx:score')[0].textContent;                
                scores[url] = score;
            }
            names = colorExpanded(scores);                   
            colorCollapsed(names);
        }
    }
});

function colorExpanded(scores) {
    var allElements;
    var names = [];
    
    allElements = document.evaluate(
        "//div[@class='lnkr']/cite/a",
        document,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null);
        
    for (var i = 0; i < allElements.snapshotLength; i++) {
        a = allElements.snapshotItem(i);
        memeUrl = a.href;
        memeName = a.textContent;
        if (scores[memeUrl]) {
            var score = scores[memeUrl];
            score = Math.round(score*1000)/10;            
            // a.textContent += ' (' + score + ')';
            
            // generate a hex color based on the score
            color = setColor(score);
            a.style.backgroundColor = color;
            if (score != 0) { a.style.textDecoration = 'none'; }
            
            // set color for rewriting collapsed view
            names[memeName.substr(0,14)] = color;
        }
    }
    return names;
}

function colorCollapsed(names) {
    var allElements;
    allElements = document.evaluate(
        "//div[@class='mlk']/a",
        document,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null);
        
    for (var i = 0; i < allElements.snapshotLength; i++) {
        a = allElements.snapshotItem(i);
        memeName = a.textContent.substr(0,14);
        if (names[memeName] != 'none') {
            a.style.backgroundColor = names[memeName];
            a.style.textDecoration = 'none';
        }
    }
}

function setColor(p) {
    if (p < 0) {
        if  (p < -10) { color = '#8686ff'; } 
        else if (p < -5) { color = '#aaaaff'; } 
        else { color = '#ccccff'; }
    } else if (p > 0) {
        if (p > 30) { color = '#ff6666'; } 
        else if (p > 5) { color = '#ff9999'; } 
        else { color = '#ffcccc'; }
    } else {
        color = 'none';
    }
    return color;
}
