/*
 *  This script adds links to the Google Cache and Coral Cache network on the fark homepage
 *  to make it easier to find a site that's been Farked into oblivian. It also removes the
 *  go.fark.com links and points them directly to the destination.
 *
 *
 *  To install this script you must be using the FireFox web browser from www.getfirefox.com
 *
 *  Next you will need to install the greasemonkey extension from:
 *  https://addons.mozilla.org/extensions/moreinfo.php?id=748
 *
 *  Finally, you can open this script in Firefox and choose Tools: Install User Script
 *
 */

// ==UserScript==
// @name        UnFark
// @namespace   http://www.cs.uni-magdeburg.de/~vlaube/Projekte/GreaseMonkey/
// @description Adds links to Coral (http://www.coralcdn.org/) and the Google Cache (http://www.google.com/) to prevent Farking Sites.
// @include     http://fark.com/*
// @include     http://*.fark.com/*
// ==/UserScript==

/*
 * This origional idea of this script was to take the "Slashdot - Add cache links" script from
 * http://www.cs.uni-magdeburg.de/~vlaube/Projekte/GreaseMonkey/slashdotaddcachelinkstoa.user.js
 * and adopt it to Fark.com
 *
 * I ran into a snag where it would crash firefox, so I took a peak at Fark Butler available at
 * http://dunck.us/code/greasemonkey/Fark-site-cleaner.user.js
 * and borrowed some code from that as well.
 *
 * The final script is not really written by me, but more of a merging of two scripts to do what
 * I want to do.
 * Bob/Paul
 */


(function () {

var coralcacheicon  = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs%2B9AAAAgUlEQVQY042O0QnCQBQEZy0sFiEkVVxa8GxAuLOLgD3cVRKwAytYf05JkGgGFt7H8nZkG10UgBNwZE0F7j77JiIJGPlNFhGzgwOQd%2FQytrEJdjtbrs%2FORAqRZBvZBrQxby2nv5iHniqokquUgM%2FH8Hadh57HNG05rlMgFXDL0vE%2FL%2BEXVN83HSenAAAAAElFTkSuQmCC";
var googleicon      = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAiklEQVQY02MUjfmmFxPFgAuIxnz7jwNcU9BngSjae%2FbDxJUPj1z%2BxMDAYKPLlx8u72wswMDAwASRnrjyIQMDw%2BoW3XfbbfPD5SFchOGCHof2nHmPaTgTpmuEPA8LeR6GsKHSNrp8E1c%2B3Hv2A8QKG10%2BiDjUaRD7Qmsuw51GlMcYnXcE4AqSyRn3Abz4culPbiCuAAAAAElFTkSuQmCC";
var background_left   = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAAOCAYAAADqtqZhAAAAZklEQVQI153OuwmAMBhF4ZOk%2BBuFgJbqCu7gBI6UWZzAEawcRC0VhNikCFj4wNpbna%2B7CqAbmxpw6o5BRKwGnIjYsqjQQJtlOcYYNECapAAXnv2GP%2FyLfttWYoxowIUQ9nmZUN%2BjJzhhGYq8ft%2BLAAAAAElFTkSuQmCC";
var background_middle = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAOCAYAAADuQ3ZcAAAAHElEQVQI12NYdMzpP5O6mgYDEwMDAznEzVs3GADbhgXE7HIXAgAAAABJRU5ErkJggg%3D%3D";
var background_right  = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAAOCAYAAADqtqZhAAAAdklEQVQI153OIQ6DQBQA0elfsaYkJFtZegU8aoOr6wl6Fs6CqyonQOIQXAGQJWmymC82raAC3XHPDXVXPuuuzAEOj%2F76UdU34CU7X7DWpkAlxhicOwHcBCA5JgAIu%2F5GWMOGGCPL8gJoZJpHfjuVqGoD%2BHvRDl9lDiMntdjqogAAAABJRU5ErkJggg%3D%3D";

function addGlobalStyle(css)
{
	var head, style;
	head = document.getElementsByTagName('head')[0];
	if (!head) { return; }
	style = document.createElement('style');
	style.type = 'text/css';
	style.innerHTML = css;
	head.appendChild(style);
}

function doLinks()
{
	var link, location, anchor, container, background_left, background_middle, background_right;

	var allLinks = window._content.document.getElementsByTagName("a");
	for (i = 0; i < allLinks.length; i++)
	{
		if(allLinks[i].href.match(/go.fark.com/i) && allLinks[i].getAttribute("onmouseover"))
   		{
			link = allLinks[i];

    		location = allLinks[i].getAttribute("onmouseover").split("'");
			link.setAttribute("href",location[1]);
			link.setAttribute("onmouseout","");
			link.setAttribute("onmouseover","");

			// add container
			container = document.createElement("div");
			container.className = "container";
			link.parentNode.insertBefore(container, link.nextSibling);

			// add background
			background_left = document.createElement("div");
			background_left.className = "backgroundimage_left";
			container.appendChild(background_left);

			background_middle = document.createElement("div");
			background_middle.className = "backgroundimage_middle";
			container.appendChild(background_middle);

			background_right = document.createElement("div");
			background_right.className = "backgroundimage_right";
			container.appendChild(background_right);

			//add coral cache link
			anchor = document.createElement("a");
			anchor.href = link.href;
			anchor.host += ".nyud.net:8090";
			anchor.title = "Coral - The NYU Distribution Network";
			anchor.className = "coralcacheicon";
			background_middle.appendChild(anchor);

			//add google cache link
			anchor = document.createElement("a");
			anchor.href = "http://www.google.com/search?q=cache:" + link.href;
			anchor.title = "Google Cache";
			anchor.className = "googleicon";
			background_middle.appendChild(anchor);

			// add a space & a br so it wraps nicely
			link.parentNode.insertBefore(document.createTextNode(" "), link.nextSibling);
			link.parentNode.insertBefore(document.createElement("br"), link.nextSibling);


		}//if
    }//for


}//fix links




addGlobalStyle("a.coralcacheicon, a.googleicon { padding-left: 15px; background: center no-repeat; }");
addGlobalStyle("a.coralcacheicon { background-image: url(" + coralcacheicon + ");}");
addGlobalStyle("a.googleicon     { background-image: url(" + googleicon + ");}");
addGlobalStyle("a.coralcacheicon:hover, a.googleicon:hover { opacity: 0.5; }");
addGlobalStyle("a.coralcacheicon:visited, a.googleicon:visited { padding-left: 15px; background: center no-repeat; }");
addGlobalStyle("a.coralcacheicon:visited { background-image: url(" + coralcacheicon + ");}");
addGlobalStyle("a.googleicon:visited     { background-image: url(" + googleicon + ");}");


addGlobalStyle("div.container { display:inline; white-space:nowrap;}");
addGlobalStyle("div.backgroundimage_left, div.backgroundimage_middle, div.backgroundimage_right { display: inline; background-repeat: no-repeat; }"); //background-position: center;
addGlobalStyle("div.backgroundimage_left { padding-left: 3px; background-image:url(" +  background_left + ");}");
addGlobalStyle("div.backgroundimage_middle { background-image:url(" +  background_middle + "); background-repeat: repeat-x; }");
addGlobalStyle("div.backgroundimage_right { padding-left: 3px; background-image:url(" +  background_right + ");}");
doLinks();
})();