$(function() {
	// create new subscription
	$("#notify-subscribe-button").click(function() {
		jQuery.ajax({
			url: '/index.php',
			data: {mod: 'user',cmd: 'checkLogin', ajax: 1},
			success: function(data) {
				if (data.success == 1)
					$("#notify-subscribe-dialog").fadeIn("fast");
				else {
					$("#login-error, #register-error, #buy-no-login, #register-success, #verify-error").hide();					
					$("#login-dlg").height($("#body_page").height()).fadeIn();
				}	
				return false;
			}			
		});
		return false;
	});
	$("#notify-add-form").submit(function() {
		var self = $(this);
		$.post(self.attr("action"), self.serialize() + "&data=" +
			escape(window.location.search.replace("?mod=contacts&cmd=showSearch&", "")),
			function() {
				$("#notify-subscribe-dialog").hide();
			}
		);
		return false;
	});
	// (un)subscribe to query item
	$(".notify-subscribe").change(function() {
		$.post("?mod=notice", {
			"cmd": "subscribe",
			"ajax": "1",
			"status": $(this).attr("checked") ? "1" : "0",
			"id": $(this).val()
		});
	});
	// (un)select all items
	$("#notify-select-all").change(function() {
		$(".notify-select-item").attr("checked", $(this).attr("checked")).trigger("change");
	});
	// delete item
	$(".notice-delete-button").click(function() {
		var cnt = $(".notify-select-item:checked").length;
		if (!cnt)
			$.SaexPopup({
				message: "Выберите одну или несколько подписок для удаления",
			});
		else
			$.SaexPopup({
				message: "Удалить выбранные подписки?",
				buttons: [{
					title: "Удалить",
					handler: function() {
						$.post("?mod=notice",
							"cmd=del&ajax=1&" +
							$(".notify-select-item:checked").serialize(),
							function() {
								window.location.reload();
							}
						);
						return true;
					}
				},
				{
					title: "Нет, спасибо",
					handler: function() { return true; }
				}]
			});
		return false;
	});
	// reset new items count
	$(".notify-link").click(function() {
		$.post("?mod=notice",
			"cmd=reset&ajax=1&id=" +
			$(this).attr("id").replace("notify-link-", "")
		);
		return true;
	});
});

