// Torrent Stream
function torrent_stream(id) {
	//ShowLoading('');
	$.post(dle_root + "engine/modules/tracker/apps/torrentstream.php", {id:id},
		function(data){
			$('#ts_'+id).html(data);
			$('#ts_'+id).css("display","block");
			//HideLoading('');
		}
	);
}

// Мультитрекер
function multitracker(id,tr_id_array) {
	if(typeof tr_id_array[0]!='undefined') {
		n = tr_id_array[0];
		tr_id_array.splice(0,1);
		hash = $('#torrent'+id+'_hash').val();
		site = $('#t'+id+'_m'+n).val();
		$.ajax({
			async: true,
			type: "POST",
			url: dle_root + "engine/modules/tracker/multitracker.php",
   			data: {site:site, hash:hash, fid:id, edit:"yes"},
			dataType: "json",
   			success: function(data){
				$('#torrent'+id+'_multi'+n+'_seed').html(data.seed);
				$('#torrent'+id+'_multi'+n+'_leech').html(data.leech);
				$('#torrent'+id+'_multi'+n+'_down').html(data.down);
				if(data.seed>0 || data.leech>0 || data.down>0) multitracker_all(id, data.seed, data.leech, data.down);
				multitracker(id,tr_id_array);
   			}
 		});
	}
}
function multitracker_all(fid, seed, leech, down) {
	var seed_a = parseInt($('#torrent'+fid+'_multi_seed').html())+parseInt(seed);
	var leech_a = parseInt($('#torrent'+fid+'_multi_leech').html())+parseInt(leech);
	var down_a = parseInt($('#torrent'+fid+'_multi_down').html())+parseInt(down);
	$('#torrent'+fid+'_multi_seed').html(seed_a);
	$('#torrent'+fid+'_multi_leech').html(leech_a);
	$('#torrent'+fid+'_multi_down').html(down_a);
}

// Обновление информации в новости
function tr_refresh(nid) {
	//ShowLoading('');
	$.post(dle_root + "engine/modules/tracker/file_info.php",
		{edit:1, news_id:nid},
		function(data){
			//HideLoading('');
			$("#form_tr_info").fadeOut(100, function() {
				$(this).html(data);
				$(this).fadeIn(300);
			});
	});
}
function tr_refr(fid,nid) {
	//ShowLoading('');
	$.post(dle_root + "engine/modules/tracker/file_info.php",
		{edit:1, news_id:nid, file_id:fid},
		function(data){
			//HideLoading('');
			$("#torrent_"+fid+"_info").fadeOut(100, function() {
				$(this).html(data);
				$(this).fadeIn(300);
			});
	});
}

// Показ подробного списка юзеров
function show_user(fid,nid,edit) {
	//ShowLoading('');
	$.post(dle_root + "engine/modules/tracker/ajax.php",
		{edit:edit, news_id:nid, file_id:fid},
		function(data){
			//HideLoading('');
			$("#torrent_"+fid+"_"+edit).fadeOut(200, function() {
				$(this).html(data);
				$(this).fadeIn(300);
			});
	});
}

// Проверить
function check_tr(fid,nid,edit) {
	//ShowLoading('');
	$.post(dle_root + "engine/modules/tracker/ajax.php",
		{edit:edit, news_id:nid, file_id:fid},
		function(){
			//HideLoading('');
			tr_refr(fid,nid);
		});
}

// Удалить
function tr_del(fid,nid) {
	DLEconfirm('Удалить данный файл?', 'Удаление файла.', function() {
		//ShowLoading('');
		$.post(dle_root + "engine/modules/tracker/ajax.php",
			{edit:1, file_id:fid},
			function(){
				//HideLoading('');
				tr_refr(fid,nid);
			});
	});
}

// Починить
function tr_repair(fid,nid) {
	//ShowLoading('');
	$.post(dle_root + "engine/modules/tracker/ajax.php",
		{edit:2, file_id:fid},
		function(){
			//HideLoading('');
			tr_refr(fid,nid);
	});
}

// Удалить фейк
function tr_fake(fid,nid) {
	DLEconfirm('Выдать запрет пользователю загружать торенты в новость?', 'Наказать пользователя.', function() {
		//ShowLoading('');
		$.post(dle_root + "engine/modules/tracker/ajax.php",
			{edit:9, file_id:fid},
			function(){
				//HideLoading('');
				tr_del(fid,nid);
		});
	});
}

// Редактировать
function tr_editor(fid,nid) {
	$.post(dle_root + "engine/modules/tracker/editor.php",
		{edit:'edit', fid:fid},
		function(data){
			//$('body').prepend("<div id=\"torrent_editor\" style=\"display:none;\"></div>");
			$('#torrent_editor').html(data);
			$('#torrent_editor').dialog({
				zIndex: 600,
				width: 600,
				buttons: {
					"Закрыть": function() {
						jQuery(this).dialog('close');
					},
					"Отредактировать" : function() {
						tr_editor_save(fid,nid);
						jQuery(this).dialog('close');
					}
				},
				title: "Редактирование torrent-файла"
			});
		});
}
function tr_editor_save(fid,nid) {
	$.post(dle_root + "engine/modules/tracker/editor.php",
		$("#tr_edit_form").serialize(),
		function(){
			$('#torrent_editor').remove();
			tr_refr(fid,nid);
		});
}
