$(document).ready(
	function()
	{
		$("#loading_win").hide();

		$("#loader")
			.ajaxStop(stopAjaxRequest)
			.hide();
		$(".ex").click(
			function()
			{
				var a = $(this);

				$("#loader").show("fast");

				$("#example:visible")
					.animate({height: "hide"}, "fast");

				$("#hidenews:visible").animate({height: "hide"},"fast");

				$("#example").find("#anchors").empty()
					.end()
					.find("div").remove()
					.end()
					.animate(
						{height: "hide"},
						"fast",
						function()
						{
							$.get(a.attr("href") + "?" + Math.random(), handleResponse);
						}
					);

				return false;
			}
		);

		$("#comment_form").ajaxForm(
			{
				success: addcomment
			}
		);



		$("#changelog div").hide();
		$("#changelog-link").click(
			function(){
				$("#changelog div").toggle();
				return false;
			}
		);
		$("#hidenews").hide();

		$("#wrapper > li").each(
			function(index){
				var obj = $(this);

				// Находим элементы h2 и устанавливаем им значения CSS и функции onclick
				// После чего прячем элементы p

				// Вместо метода css с параметрами можно вызывать метод
				// addClass(index % 2 == 0 ? "even" : "odd")
				// если у нас уже есть css-классы "even" и "odd"

				obj
					.find("#nheader")
					.css(
						{
							backgroundColor: index % 2 == 0 ? "#FFF" : "#F5F5F5",
							cursor: "hand",
							cursor: "pointer",
							color: "#1c1c1c"
						}
					)
					.click(
						function(){
							obj.find("p").toggle();
						}
					)
					.end()
					.find("p").hide();
			}
		);
	}
);

function handleResponse(data)
{
	var anchors = $("#anchors");
	var example = $("#example");

	$("item[@type='description']", data).each(
		function(i){
			var obj = $(this);
			var id = "description" + (i > 0 ? "-" + (i + 1) : "");

			var title = $(this).attr("title");
			if(title == '') title = lStr.description;

			anchors.append("<li><a href=\"#" + id + "\">" + title + (i > 0 ? " " + (i + 1) : "") + "</a></li>");

			var d = $("<div id=\"" + id + "\"></div>");
			d.append(obj.text()).addClass("fragment");
			example.append(d);
		}
	);

	$("item[@type='demo']", data).each(
		function(i){
			var obj = $(this);
			var id = "demo" + (i > 0 ? "-" + (i + 1) : "");

			anchors.append("<li><a href=\"#" + id + "\">" + lStr.demo + (i > 0 ? " " + (i + 1) : "") + "</a></li>");

			var d = $("<div id=\"" + id + "\"></div>");
			d.append(obj.text()).addClass("fragment");
			example.append(d);
		}
	);

	$("item[@type='alternative']", data).each(
		function(i){
			var obj = $(this);
			var id = "alternative" + (i > 0 ? "-" + (i + 1) : "");

			anchors.append("<li><a href=\"#" + id + "\">" + lStr.alternative + (i > 0 ? " " + (i + 1) : "") + "</a></li>");

			var d = $("<div id=\"" + id + "\"></div>");
			d.append(obj.text()).addClass("fragment");
			example.append(d);
		}
	);

	example.tabs();

	example.animate({height: "show"}, "fast");
	$("#hidenews").show("fast");
}

function stopAjaxRequest()
{
	$("#loader").animate({height: "hide"}, "fast");
	$("#hidenews").animate({height: "show"}, "fast");
}

function hidenews()
{
	$("#example").animate({height: "hide"}, "fast");
	$("#hidenews").animate({height: "hide"}, "fast");
}

function addcomment(response, status)
{
	$("#comment_result").html($("item", response).text());
	eval($("script", response).text());
}
function show_loading(response, status)
{
	$("#loading_win").show();
}

function handleError()
{
}
