var URL = 'http://tracker.gamemp3s.net/webui/?callback=?';
var DOWNLOAD_PREFIX = 'http://tracker.gamemp3s.net/webui/download?id=';
var INFO_PREFIX = 'http://tracker.gamemp3s.net/webui/torrent_info?callback=?&id='

var torrentHeadings = [
	{
		"sTitle": "Name",
		"fnRender": function (oObj) {
			return '<a id="torrent-' + oObj.aData[5] + '" href="' + DOWNLOAD_PREFIX + oObj.aData[5] + '">' + oObj.aData[0] + '</a>';
		},
		"bUseRendered": false
	 },
	{ "sTitle": "Seeds" },
	{ "sTitle": "Leechers" },
	{ "sTitle": "Completed" },
	{
		"sTitle": "Added",
		"fnRender": function (oObj) {
			return Date.parse(oObj.aData[4]).toString('MMMM d, yyyy');
		},
		"bUseRendered": false
	},
	{ "sTitle": "ID", "bVisible": false },
	{ "sTitle": "Category", "bVisible": false },
	{ "sTitle": "Uploaded By", "bVisible": false }
];

var torrentData = [];
var torrentCategories = ['#gamemp3s'];

function makeQTips() {
	$.each(torrentData, function(i, torrent) {
		var name = torrent[0];
		var info_hash = torrent[5];
		var info_url = INFO_PREFIX + info_hash;
		
		$('#torrent-' + info_hash).qtip({
			content: 'Loading&hellip;',
			style: {
				name: 'green',
				width: {
					max: '850'
				},
				'max-height': '400px',
				'overflow-y': 'auto',
				'top': '20px',
				'right': '20px',
				tip: {
					corner: 'leftMiddle'
				}
			},
			position: {
				corner: {
					target: 'rightMiddle',
					tooltip: 'leftMiddle'
				},
				adjust: {
					x: 10,
					screen: true
				}
			},
			hide: { delay: '250', when: 'mouseout', fixed: true },
			show: { when: { event: 'mouseover' }, solo: false },
			api: {
				onRender: function() {
					var self = this;
					$.getJSON(info_url, {}, function(torrent) {
						function renderFiles(files) {
							if (files instanceof Array) {
								text = '<ul>';
								$.each(files, function(i, file) {
									text = text + '<li style="font-size: 93%;">' + file + '</li>';
								});
								text = text + '</ul>';
							} else {
								text = '<strong>' + files + '</strong>';
							}

							return text;
						}

						renderedHTML =
							'<p><strong><em>' + torrent.name + '</strong></em></p>'+
							'<p><strong>Category:</strong> ' + torrent.category + '</p>'+
							'<p><strong>Uploaded By:</strong> ' + torrent.uploaded_by + '</p>'+
							'<p><strong>Files (' + torrent.num_files + ' Total):</strong>'+
							renderFiles(torrent.files)+'</p>'+
							'';
						self.updateContent(renderedHTML);
					});
				}
			}
		});
	});
}

function populateTable() {
	$('#torrents').html('<table cellpadding="0" cellspacing="0" border="0" class="display" id="torrents_table" style="width: 100%;"></table>');

	torrentTable = $('#torrents_table').dataTable({
		"aaData": torrentData,
		"aoColumns": torrentHeadings,
		"bPaginate": false,
		"aaSorting": [[4, 'desc']],
		"bSortClasses": false,
		"bFilter": false
	});
			
	makeQTips();

	torrentTable.fnFilter(torrentCategories[0], 6);

	$("#torrent_search_box").keyup(function(event) {
		torrentTable.fnFilter($(this).val());
	});

	torrentCategoryFilter = $('ul#torrent_category_filter');

	torrentCategoryFilter.html('<li><a href="#all">all</a></li>');

	$('ul#torrent_category_filter li a[href="#all"]').click(function() {
		torrentTable.fnFilter('', 6);
	});

	$.each(torrentCategories, function(i, category) {
		$('ul#torrent_category_filter').append('<li><a href="#' + category + '">' + category + '</a></li>');
		$('ul#torrent_category_filter li a[href="#' + category + '"]').click(function() {
			torrentTable.fnFilter(category, 6);
		});
	});
}

function downloadTorrents() {
	if (torrentData.length == 0) {
		$.getJSON(URL, {}, function (torrents) {
			$.each(torrents, function(i, torrent) {
				torrentData.push(
					[torrent.name, torrent.seeds, torrent.leechers, torrent.completed, torrent.pub_date, torrent.info_hash, torrent.category, torrent.uploaded_by]
					);

				if (torrentCategories.indexOf(torrent.category) == -1) {
					torrentCategories.push(torrent.category);
				}
			});
			if (torrentData.length > 0) {
				populateTable();
			} else {
				setTimeout('downloadTorrents()', 500);
			}
		});
	}
}

downloadTorrents();

function populateTimer() {
	if (torrentData.length == 0) {
		setTimeout('populateTimer()', 10);
	} else {
		populateTable();
	}
}

$(document).ready(function($) {
	populateTimer();
});

