// ==UserScript==
// @author Raul Ochoa
// @version 0.100
// @name IMDB Bittorrent
// @namespace https://rau1.com/
// @description Download torrents directly from the IMDB website
// @include http://www.imdb.com/title/*
// @include http://imdb.com/title/*
// @include http://www.mininova.org/imdb/*/*
// ==/UserScript==

/* --- IMDB Bittorrent ---

2008/04/20 - v0.100
Copyright (c) 2008, Raul Ochoa <rochoaf@gmail.com>
Released under the GPL license: http://www.gnu.org/copyleft/gpl.html

v0.100 - 20/04/2008
 - First public release

*/

var href = window.location.href;


if (href.search("mininova") != -1) {
	function load() {
		var body = document.getElementsByTagName("body")[0];
		body.style.display = 'none';
		var newbody = document.createElement("body");
		newbody.innerHTML = '<div id="previous" class="nav inactive" onclick="previous()" style="margin-left: 0;">&lt;</div><div id="next" class="nav" onclick="next()">&gt;</div><div id="current"></div>';
		newbody.setAttribute("style", "padding: 0;");
		document.getElementsByTagName("html")[0].appendChild(newbody);
		try {
			table = document.getElementsByTagName("table")[0];
			items = table.getElementsByTagName("tr");
			if (items.length > 1) {
				show(items[1]);
			}
		} catch (e) {
			document.getElementById("next").setAttribute("class", "nav inactive");
			document.getElementById("current").innerHTML = "There are not available torrents";
		}
	}
	
	function show(item) {
		var td = item.getElementsByTagName("td");
		var a = item.getElementsByTagName("a");
		var title;
		var link;
		for (var i = 0; i<a.length; i++) {
			if (a[i].getAttribute("class") == "dl") {
				link = a[i];
				title = a[i+1].innerHTML;
				break;
			}
		}
		document.getElementById("current").innerHTML = "[<a href=\""+ link.href + "\">" + link.innerHTML + "Download</a>] " +  title + "<br/>" + td[2].innerHTML + " | Seeds: " + td[3].innerHTML + " - Leechers: "+ td[4].innerHTML;
	}
	
	function next() {
		if (current < items.length-1) {
			show(items[++current]);
			if (current == items.length-1) {
				document.getElementById("next").setAttribute("class", "nav inactive");	
			} else {
				document.getElementById("next").setAttribute("class", "nav");
			}
			document.getElementById("previous").setAttribute("class", "nav");
		} else {
			
		}
	}
	
	function previous() {
		if (current > 1) {
			show(items[--current]);
			document.getElementById("next").setAttribute("class", "nav");
			if (current == 1) {
				document.getElementById("previous").setAttribute("class", "nav inactive");
			} else {
				document.getElementById("previous").setAttribute("class", "nav");
			}
		}
	}
	
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.innerHTML = "var table; var items; var current = 1;" + load.toString() + show.toString() + next.toString() + previous.toString() + "load();";
	document.getElementsByTagName("head")[0].appendChild(script);
	
	var style = document.createElement("style");
	style.type = "text/css";
	style.innerHTML = ".nav { border: solid 1px #36f; font-size: 16px; font-weight: bold; font-family: tahoma, verdana, arial, sans-serif; float: left; padding: 7px 12px; cursor: pointer; color: #36f; margin-left: 4px; } .nav:hover { color: #333; } .inactive { border: solid 1px #999; color: #999; } .inactive { border: solid 1px #999; color: #999; cursor: default; } .inactive:hover { color: #999; } #current { margin-left: 90px; border: solid 1px #ccc; font-size: 11px; padding: 2px 6px; font-family: tahoma, verdana, arial, sans-serif; positon: fixed; top: 0; height: 31px;}";
	document.getElementsByTagName("head")[0].appendChild(style);
	
} else {
	function load() {
		var href = window.location.href;
		var title = document.getElementsByTagName("h1")[0];
		var mininova = document.createElement('iframe');
		mininova.setAttribute("style", "border: 0; width: 100%; height: 38px; margin-top: 5px;");
		var imdbId = href.substring(href.length-8,href.length-1);
		mininova.src = "http://www.mininova.org/imdb/" + imdbId + "/seeds";
		title.parentNode.appendChild(mininova);
	}
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.innerHTML = load.toString() + "load();";
	document.getElementsByTagName("head")[0].appendChild(script);
}