Skillopedia_ThreadActions = new function() {
	this.Share = 0;
	this.Complain = 0;

	this.favourite = function(thread) {
		if (!User.ID || !thread) {
			alert('Пожалуйста, зарегистрируйтесь или войдите, чтобы добавлять в избранное');
		}

		JsHttpRequest.query(
				'_parts/thread_actions/favourite.php',
				{
					'id': thread
				},
				function(result, data) {
					if (result === false) {
						alert('Уже в вашем избранном');

						return;
					}

					document.getElementById('actions_favourite').innerHTML = 'В избранном';
					document.getElementById('actions_favourite').href = 'favourites.php';
					document.getElementById('actions_favourite').className = 'secondary';
				},
				true
		);
	}

	// Скачать
	this.download = function(thread) {
		document.getElementById('actions_download_baloon').style.display = 'block';
	}

	this.complain = function(thread) {
		document.getElementById('actions_share_baloon').style.display = 'none';
		document.getElementById('actions_complain_baloon').style.display = 'block';
	}

	// Выдать ссылки на скачивание
	this.doDownload = function(thread) {
		jQuery.ajax({
			url: '/api/get_link.php',
			type: 'post',
			data: 'code=' + thread,
			success: function(answer) {
				if (/^Error/.exec(answer)) {
					jQuery('#download_err').html(answer.replace('Error: ', ''));
					jQuery('#download_err').show();
				} else {
					jQuery('#download_links').html(answer);
					jQuery('#download_err').hide();
				}
			}
		});
	}

	this.doComplain = function(thread) {
		if (!User.ID || !thread) {
			alert('Пожалуйста, зарегистрируйтесь или войдите, чтобы пожаловаться');

			return;
		}


		JsHttpRequest.query(
				'_parts/thread_actions/complain.php',
				{
					'id': thread
				},
				function(result, data) {
					if (result !== false) {
						document.getElementById('actions_complain').innerHTML = 'Пожаливались';
						document.getElementById('actions_complain').className = 'secondary';
					}
					else alert('Что-то не вышло');

					Skillopedia_ThreadActions.hide();
				},
				false
		);
	}

	this.doShare = function(thread) {
		email = document.getElementById('actions_share_email').value;

		if (!email) {
			return;
		}


		JsHttpRequest.query(
				'_parts/thread_actions/share.php',
				{
					'id': thread,
					'reciptient': email
				},
				function(result, data) {
					if (result !== false) {
						document.getElementById('actions_share').innerHTML = 'Отправлено';
						document.getElementById('actions_share').className = 'secondary';
					}
					else alert('Что-то не вышло');

					Skillopedia_ThreadActions.hide();
				},
				false
		);
	}

	this.share = function() {
		document.getElementById('actions_complain_baloon').style.display = 'none';
		document.getElementById('actions_share_baloon').style.display = 'block';
	}

	this.hide = function() {
		document.getElementById('actions_share_baloon').style.display = 'none';
		document.getElementById('actions_complain_baloon').style.display = 'none';
	}
}

try {
	document.getElementById('main').onclick = Skillopedia_ThreadActions.hide;
} catch(e) {
}

