$(document).ready(function() {
	$("a").focus(function() {
		this.blur();
	});
	$("#error")
			.vkfade()
			.append("<span id='close_error' class='close_link' href='#'>Щёлкните по этому сообщению, чтобы закрыть его.</span>");
	$("#error").click(function() {
		$("#error_container").slideUp("fast");
	})

	$("a.showfull_link").html("Развернуть&nbsp;&darr;").prev("div")
			.after("<div class=\"full\"></div>");
	$("a.showfull_link")
			.after("<img class=\"loading_img\" src=\"/3ngine/ajax/loader.gif\" />");
	$("img.loading_img").hide();
	$("div.full").hide();

	$("a.showfull_link").click(function() {
		$(this).next("img.loading_img").show();

		if ($(this).prev("div.full").html() == '') {
			get_page($(this));
		} else {
			$(this).prev("div.full").animate({
				opacity : 'toggle',
				height : 'toggle'
			}, 1000, '', function() {
				if ($(this).next("a.showfull_link").is(":contains('Свернуть')")) {
					$(this).next("a.showfull_link")
							.html("Развернуть&nbsp;&darr;")
							.next("img.loading_img").hide();;
				} else
					$(this).next("a.showfull_link")
							.html("Свернуть&nbsp;&uarr;")
							.next("img.loading_img").hide();;
			});

		}
		return false;

	}

	);

	$("#show_profile").click(function() {
		$('#options').toggle('slow')
	});
	$('#months').change(refreshCalendar);
	$('#years').change(refreshCalendar);

	function refreshCalendar() {
		$('#months').attr('disabled', 'disabled');
		$('#years').attr('disabled', 'disabled');
		$('#cal_wrap').fadeTo("slow", .3, function() {

			$.post("/3ngine/ajax/calendar.php", {
				month : $('#months').val(),
				year : $('#years').val()
			}, function(xml) {

				$('#calendar_content').html($('output', xml).text());
				$('#cal_wrap').fadeTo("slow", 1, function() {
					$('#months').attr('disabled', '');
					$('#years').attr('disabled', '');
				});

			});
		});
	}

	function get_page(obj) {
		$.post("/3ngine/ajax/pages.php", {
			newsid : $(obj).prev('div').prev().attr('id'),
			lang : $('#lang').val()
//			est : $(obj).prev('div').prev().attr('rel')
		}, function(xml) {
			$('#news-id-' + $("id", xml).text()).next("div").html($("story",
					xml).text()).animate({
				opacity : 'toggle',
				height : 'toggle'
			}, 1000, '', function() {
				$(this).next("a.showfull_link").html("Свернуть&nbsp;&uarr;")
						.next("img.loading_img").hide();
			});
		});
	}
});

function initLinks() {
	for (i in document.links) {
		link = document.links[i];
		if (link.rel && link.rel.indexOf('external') != -1) {
			link.onclick = onExternalLinkActivate;
			link.onkeypress = onExternalLinkActivate;
		}
	}
}

function onExternalLinkActivate() {
	window.open(this.href);
	return false;
}

window.onload = initLinks;
