window.addEvent('domready', function() {
	var link = $$('h1 a.subscribe');
	if (link.length) {
		link = link[0];
		
		var requestIsSent = false;
		var request = new Request.JSON({
			url: '',
			onRequest: function() {
				requestIsSent = true;
			},
			onSuccess: function(responseJSON) {
				if (link.hasClass('unsubscribe')) {
					link.removeClass('unsubscribe');
				} else {
					link.addClass('unsubscribe');
				};
				requestIsSent = false;
			}
		});
		
		var subscribe = function(e) {
			new Event(e).stop();
			if (!requestIsSent) {
				if (this.hasClass('unsubscribe')) {
					request.options.url = '/news/unsubscribe/' + this.get('href').match(/\d+/)[0] + '/';
				} else {
					request.options.url = '/news/subscribe/' + this.get('href').match(/\d+/)[0] + '/';
				};
				request.send();
			};
		};
		
		link.addEvent('click', subscribe);
	};
});
