// ==UserScript==

// @name            Pandora History
// @author          Bruno Torres <http://www.brunotorres.net/>
// @namespace       http://www.brunotorres.net/greasemonkey/
// @description     Creates a History of what was played on your pandora radios
// @include         http://*pandora.com*

// ==/UserScript==

(function() {
	o = String.prototype;
	o.trim = function() { //Contribuição do Diego <http://plentz.org/>
		return this.replace(/^\s*|\s*$/g,'');
	}
	var oldTitle = document.title;
	var text;
	var p;
	var title;
	var song;
	var artist;
	var div = document.createElement('div');
	div.id = 'gmhistory';
	div.style.position = 'absolute';
	div.style.left = '775px';
	div.style.top = '300px';
	div.style.width = '250px';
	div.style.height = '280px';
	div.style.color = '#000';
	div.style.border = '1px solid #333';
	div.style.padding = '5px';
	var h3 = document.createElement('h3');
	var h3text = document.createTextNode('History for the session');
	h3.appendChild(h3text);
	h3.style.font = 'bold 12px arial';
	h3.style.height = '15px';
	h3.style.margin = '5px 0';
	h3.style.textAlign = 'center';
	div.appendChild(h3);
	var clear = document.createElement('a');
	clear.style.display = 'block';
	clear.style.font = 'normal 10px arial';
	clear.style.cursor = 'pointer';
	var cleartext = document.createTextNode('Clear List');
	clear.appendChild(cleartext);
	clear.onclick = function(){
		div.removeChild(ul);
		ul = document.createElement('ul');
		ul.style.height = '220px';
		ul.style.overflow = 'auto';
		ul.style.margin = '0';
		ul.style.padding = '0';
		div.insertBefore(ul, ad);
	}
	div.appendChild(clear);
	var ul = document.createElement('ul');
	ul.style.height = '220px';
	ul.style.overflow = 'auto';
	ul.style.margin = '0';
	ul.style.padding = '0';
	div.appendChild(ul);
	var ad = document.createElement('p');
	ad.style.font = 'normal 10px arial';
	var adtext = document.createTextNode('History by ');
	ad.appendChild(adtext);
	var a = document.createElement('a');
	a.href = 'http://brunotorres.net';
	var atext = document.createTextNode('Bruno Torres');
	a.appendChild(atext);
	ad.appendChild(a);
	div.appendChild(ad);
	document.getElementsByTagName('body')[0].appendChild(div);
	function compareTitle(){
		if (document.title != oldTitle)
		{
			title = document.title;
			title = title.split(' - ');
			title = title[0];
			title = title.split(' by ');
			song = title[0].trim();
			artist = title[1].trim();
			li = document.createElement('li');
			li.style.font = 'normal 10px/14px arial';
			li.style.margin = '0 0 0 14px';
			text = document.createTextNode(song+' by '+artist);
			li.appendChild(text);
			ul.appendChild(li);
			oldTitle = document.title;
		}
	}
	var interval = setInterval(compareTitle, 10000);
	delete o;
})();
