// ==UserScript==
// @name           FipiFixer 2.0.3
// @namespace      http://www.filepile.org/
// @include        http://filepile.org/*
// @include        http://www.filepile.org/*
// @include        https://filepile.org/*
// @include        https://www.filepile.org/*
// ==/UserScript==

/*
FipiFixer 2.0
*/

///////////////////////////////
// CONFIGURATION OPTIONS
///////////////////////////////
// These variables control various optional features.
// For option variables whose value is "true", set the
// value to "false" to disable that option.
///////////////////////////////

// add [+] and [-] links after newer/older links for speed voting
var tigtib = true;

// confirm whether you really want to vote bad with speed vote link
var confirmtib = false;

// make HTML links open in a new window
var htmllinksnewwindow = true;

// add CSS so that visited file links fade to pale gray
var fadelinks = true;

var fadelinksCSS = "color: #CCCCCC ! important;";
var fadelinkshoverCSS = "color: #000000 ! important;";

// cause external (non-filepile.org) links to open in a new window
var extnewwindow = true;

// add a "top of page" link at the bottom of the page
var topofpage = true;

// highlight the filename of files with "nsfw" in the title
var hilitensfw = true;

var nsfwCSS = "text-decoration: none ! important; color: red ! important;";
var nsfwvisitedCSS = "text-decoration: none ! important; color: #FF9999 ! important;";

// show an alert/confirm message when you click on "nsfw" links
var alertnsfw = false;

// highlight the filename of files marked TMBO
var hilitetmbo = true;

var tmboCSS = "text-decoration: none ! important; color: red ! important;";
var tmbovisitedCSS = "text-decoration: none ! important; color: #FF9999 ! important;";

// show an alert/confirm message when you click on "tmbo" links
var alerttmbo = false;

// highlight the filename of files with "spoiler" in the title
var hilitespoiler = true;

var spoilerCSS = "color: #FF0099 ! important;";
var spoilervisitedCSS = "color: #FF99FF ! important;";

// show an alert/confirm message when you click on "spoiler" links
var alertspoiler = true;

// delete title attribute (which removes title tooltip) from images on file view page
var noimagetitle = true;

// increase table width (archive pages only)
var widetables = true;

// puts full filename in link (archive pages only)
var filenameinlinks = true;

// highlight files and comments of users you choose
var hiliteuser = new Array('user1','user2','user3');

var hiliteusercolor = "navy";


///////////////////////////////
// FUNCTIONS
///////////////////////////////
// Don't edit anything below, unless you know what you're doing, or
// don't really care if the script works or not.
///////////////////////////////

// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that
// (a) you leave this copyright notice intact, and
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site
//     with a link back to http://www.albionresarch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// ====================================================================


function URLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef";
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2)
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};

///////////////////////////////
// MAIN PROGRAM
///////////////////////////////

// what page are we on?

var isarchive = false;
var isuserpage = false;
var isdiscuss = false;
var isfile = false;
var isfrontpage = false;
var isdownload = false;

var thepath = document.location.pathname;

if (thepath == "/" || thepath == "/home") { isfrontpage = true; }
if (thepath.substr(0,8) == "/archive" ||  thepath.substr(0,7) == "/top100") { isarchive = true; }
if (thepath.substr(0,12) == "/archive/mp3") { isarchive = false; }
if (thepath.substr(0,6) == "/user/") { isuserpage = true; }
if (thepath.substr(0,8) == "/discuss") { isdiscuss = true; }
if (thepath.substr(0,6) == "/file/") { isfile = true; }

if (isfile && noimagetitle) {
	thefile = document.getElementById("the_file");
	if (thefile) {
		thefile.title = "";
	}
}

// add CSS rules
if (fadelinks) {
	document.styleSheets[0].insertRule(".filename:visited { " + fadelinksCSS + " }",0);
	document.styleSheets[0].insertRule(".filename:hover { " + fadelinkshoverCSS + " }",0);
}
if (hilitensfw) {
	document.styleSheets[0].insertRule(".nsfwFile { " + nsfwCSS + " }",0);
	if (fadelinks) {
		document.styleSheets[0].insertRule(".nsfwFile:visited { " + nsfwvisitedCSS + " }",0);
	}
}
if (hilitetmbo) {
	document.styleSheets[0].insertRule(".tmboFile { " + tmboCSS + " }",0);
	if (fadelinks) {
		document.styleSheets[0].insertRule(".tmboFile:visited { " + tmbovisitedCSS + " }",0);
	}
}
if (hilitespoiler) {
	document.styleSheets[0].insertRule(".spoilerFile { " + spoilerCSS + " }",0);
	if (fadelinks) {
		document.styleSheets[0].insertRule(".spoilerFile:visited { " + spoilervisitedCSS + " }",0);
	}
}

// accesskey values
var newerkey = ",";
var olderkey = ".";
var mainkey = "m";
var tigkey = "+";
var tibkey = "-";

// define regular expressions
var REnsfw = new RegExp("nsfw","i");
var REnsfwalt = new RegExp("\\[n\\]","i"); // because some people are using "[n]" for "nsfw"
var REspoiler = new RegExp("spoiler","i");
var REtmbo = new RegExp(" x[0-9]+ ");
//var REsnap = new RegExp("^([^\-]+) - (.+) - (\d+KB)$");
var REextwindow = new RegExp("filepile\.org$");

// get links and process them
var links = document.getElementsByTagName("a");

var isNSFW = 0;
var isTMBO = 0;
var isSpoiler = 0;

// kill the submit button's id, because for some reason you can't
// submit a form with code when its id/name = "submit"
if (isfile) {
	submitbutton = document.getElementById("submit");
	submitbutton.id = "";
}

for ( var i = 0, link; link = links[i]; i++ ) {

	// if it's a link to view a file, add "filename" CSS def
	if (fadelinks && link.pathname.substr(0,5) == "/file" && link.pathname.substr(0,12) != "/file/upload") {
		link.className = link.className + " filename";
	}

	// if links go to external (non-Filepile) sites, open them in a new window
	if (extnewwindow && !REextwindow.test(link.hostname) && (link.protocol == "http:" || link.protocol == "https:") && link.href.length > 0 && link.innerHTML != "blogs") {
		link.innerHTML = link.innerHTML + " <img src=\"http://wiki.filepile.org/skins/monobook/external.png\" width=\"10\" height=\"10\" border=\"0\" alt=\"external link\">";
		link.style.textDecoration = "underline";
		link.target = "_blank";
	}

	if (htmllinksnewwindow) {
		//alert ('foo');
		if (link.pathname.substr(0,14) == "/file/download" && link.href.substr(link.href.length-4,4) == "html") {
			link.target = "_blank";
		}
	}

	// if it has "nsfw" in the filename, highlight it
	if (REnsfw.test(link.title) || REnsfwalt.test(link.title)) {
		link.className = "nsfwFile";
		isNSFW = 1;
	}

	// if it has "spoiler" in the filename, highlight it
	if (REspoiler.test(link.title)) {
		link.className = "spoilerFile";
		isSpoiler = 1;
	}

	// if it's marked TMBO, highlight it
	var results = link.title.match(REtmbo);
	if (results) {
		resultN = results[0].replace(/[^0-9]/g,"");
		if (parseInt(resultN) > 0) {
			link.className = "tmboFile";
			isTMBO = 1;
		}
	}

	if ((isNSFW && alertnsfw) || (isTMBO && alerttmbo) || (isSpoiler && alertspoiler)) {
		var alerttext = "";
		if (isNSFW) { alerttext += "NSFW "; }
		if (isTMBO) { alerttext += "TMBO "; }
		if (isSpoiler) { alerttext += "Spoiler"; }
		alerttext = alerttext.replace(/ $/,"");
		alerttext = alerttext.split(" ").join(", ");
		alerttext = "This link is marked as: [" + alerttext + "] Are you sure you want to look at it?";
		link.setAttribute("onClick","return confirm('"+alerttext+"');");
	}

	isNSFW = 0;
	isTMBO = 0;
	isSpoiler = 0;

	// add accesskey to "newer"/"older" links
	if (link.text == String.fromCharCode(171)+"newer") {
		link.setAttribute("accesskey",newerkey);
		link.title = "alt+<";
		newerkey = "";
	}
	if ((link.text == "main" || link.text == "top") && link.rel == "up") {
		link.setAttribute("accesskey",mainkey);
		link.title = "alt+m";
		mainkey = "";
	}
	if (link.text == "older"+String.fromCharCode(187)) {
		link.setAttribute("accesskey",olderkey);
		link.title = "alt+>";
		olderkey = "";
	}

	// add [+] and [-] speed voting links
	if (isfile && tigtib) {
		if (link.text == "older"+String.fromCharCode(187)) {
			var navcontainer = link.parentNode;
			if (document.getElementById("comment_vote_good")) {
				navcontainer.innerHTML = navcontainer.innerHTML + ' <a href="#" accesskey="' + tigkey + '" title="alt+[+]" onClick="document.getElementById(\'comment_vote_good\').checked=true; document.forms[0].submit();">[+]</a> ';
				if (confirmtib) {
					navcontainer.innerHTML = navcontainer.innerHTML + '<a href="#" accesskey="' + tibkey + '" title="alt+[-]" onClick="if (confirm(\'Are you sure you want to vote bad?\')) { document.getElementById(\'comment_vote_bad\').checked=true;document.forms[0].submit(); }">[-]</a>';
				} else {
					navcontainer.innerHTML = navcontainer.innerHTML + '<a href="#" accesskey="' + tibkey + '" title="alt+[-]" onClick="document.getElementById(\'comment_vote_bad\').checked=true;document.forms[0].submit();">[-]</a>';
				}
			} else {
				navcontainer.innerHTML = navcontainer.innerHTML + ' <span style="color: #999999;">[+] [-]</span>';
			}
			tigkey = "";
			tibkey = "";
		}

	}

	// untruncate filenames in archive pages
	if (isarchive && filenameinlinks) {
		if (link.title.length > 0) {
			fbits = link.title.split(" - ");
			fname = fbits[1];
			link.innerHTML = fname;
		}
	}

	// highlight filenames posted by people in your highlight list
	if (!isdiscuss) {
		if (link.title.length > 0) {
			if (hiliteuser.length > 0) {
				for ( u = 0; u < hiliteuser.length; u++) {
					if (link.title.substr(0,hiliteuser[u].length) == hiliteuser[u]) {
						link.style.backgroundColor = hiliteusercolor;
						link.style.color = "white";
						link.style.fontWeight = "bold";
						if (isarchive || isfile) { link.innerHTML += " (" + hiliteuser[u] + ")"; }
					}
				}
			}
		}
	}

} // end of link processing

// add "top of page" link at bottom of page
// link to top of page
if (topofpage) {
	var body = document.getElementsByTagName("BODY")[0];
	var div = document.createElement("div");
	div.innerHTML = '<br clear="all"><a href="#">top of page ^</a>';
	body.appendChild(div);
}

if ((isarchive || isuserpage) && widetables) {
	var divs = document.getElementsByTagName("div");
	for ( var i = 0, div; div = divs[i]; i++ ) {
		if (div.className == "archivepile") {
			div.style.width = "900px";
		}
		if (div.className == "archive") {
			div.style.width = "900px";
		}
		if (div.className == "content") {
			div.style.width = "900px";
		}
	}
	var tables = document.getElementsByTagName("table");
	for ( var i = 0, table; table = tables[i]; i++ ) {
		if (table.className == "filelist") {
			table.style.width = "900px";
		}
	}
	var cells = document.getElementsByTagName("td");
	for ( var i = 0, cell; cell = cells[i]; i++ ) {
		if (cell.className == "fname") { cell.style.width = "550px"; }
	}
}

/////////////////////////////////////////////////////////////////////
// change log
/////////////////////////////////////////////////////////////////////
/*

I should have started this sooner, but I'm a lazy programmer.

1.0.3  - 2007/07/18 - added [+] and [-] speed voting links
1.0.4  - 2007/07/19 - removed "no new window" setting, since it's now in your FP settings page.
1.0.5  - 2007/07/19 - [+] and [-] links remain even after voting, but disabled (greyed out),
                      in order to keep the newer/older links in the same place on screen
1.0.6  - 2007/07/27 - adds "top of page" link at bottom of page (7/27/2007)
1.0.7  - 2007/08/10 - if hilitensfw is set to 1, files that have "NSFW" in the filename are
                      highlighted in red text
1.0.8  - 2008/01/09 - added row highlighting to user pages
1.0.9  - 2008/03/06 - added copy "newer/older" nav links to bottom of page
1.0.10 - 2008/03/12 - added function to delete title attribute from image on file view page
1.0.11 - 2008/05/18 - changed method of row highlighting; rows now change color onmouseover
                      instead of static alternating colors

2.0.0  - 2008/05/31 - Complete rewrite
2.0.1  - 2008/09/13 - Added accesskey property to "newer","older","[+]" and "[-]" links
2.0.2  - 2008/10/21 - Fixed "[+]" and "[-]" links to account for updated form controls
2.0.3  - 2008/11/02 - Added accesskey property to "main"/"top" link

*/