// ==UserScript==
// @name           pitchforked
// @namespace      http://www.kevinlingerfelt.com/
// @description    finding mp3s on pitchfork sucks
// @include        http://www.pitchforkmedia.com/*
// ==/UserScript==

var links = document.getElementsByTagName('a');
var display = false;

var div_styles = '' +
	'position: absolute;' +
	'top: 0;' +
	'left: 0;' +
	'width: 100%;' +
	'height: 95px;' +
	'background-color: #e6f7fd;' +
	'border-bottom: 2px solid #800;' +
	'text-align: left;' +
	'z-index: 99;' +
	'overflow: scroll;' +
	''

var list_styles = '' +
	'list-style-type: none;' +
	'margin: 0;' +
	'padding: 5px 1%;' +
	'width: 48%;' +
	''

var title_styles = '' +
	'font-family: Arial, sans-serif;' +
	'font-size: 14px;' +
	'font-weight: bold;' +
	'color: #333;' +
	''	

var list_element_styles = '' +
	'padding: 2px 0;' +
	''

var link_styles = '' +
	'font-family: Arial, sans-serif;' +
	'font-size: 12px;' +
	'font-weight: normal;' +
	'color: #800;' +
	'text-decoration: underline;' +
	''

var audio_list = document.createElement('ul');
var audio_title = document.createElement('li');
audio_title.setAttribute('style', title_styles);
audio_title.appendChild(document.createTextNode('Audio'));
audio_list.appendChild(audio_title);

for (var i = 0; i < links.length; i++) {
	url = links[i].getAttribute("href");
	if (url != null && links[i].firstChild != null &&
		links[i].firstChild.nodeValue != 'Digg this article' &&
		links[i].firstChild.nodeValue != 'Add to del.icio.us' &&
		links[i].firstChild.nodeValue != 'Permalink') {
			
			if (url.search(/mp3$/i) >= 0 || url.search(/mp3stream$/) >= 0) {
				display = true;
				link = links[i].cloneNode(true)
				link.setAttribute('style', link_styles);
				var li = document.createElement('li');
				li.setAttribute('style', list_element_styles);
				li.appendChild(link);
				audio_list.appendChild(li);
			}
	}
}

if (display) {
	document.body.removeChild(document.getElementById("top"));
	var div = document.createElement('div');
	div.setAttribute('style', div_styles);
	audio_list.setAttribute('style', list_styles);
	div.appendChild(audio_list);
	document.body.insertBefore(div, document.body.firstChild);
}
