$(function()
{
	$('.rblock').after('<p class="rblock-dn"></p>');
	$(".rblock *:hidden").parent("*").next(".rblock-dn").css({"top":"-13px"});
	
	function liFormat (row, i, num) {var result = row[0] + '' + row[1] + '';return result;}
	function selectItem(li) {$("#findax").focus();}
	$("#findax").autocomplete("/ajax/autocomplete.php", {delay:100,minChars:2,matchSubset:1,autoFill:false,matchContains:1,cacheLength:10,selectFirst:false,formatItem:liFormat,maxItemsToShow:10,width:217,onItemSelect:selectItem});
	$("a.photo").fancybox({"frameWidth":640,"frameHeight":520,"centerOnScroll":false});
	
	
	// Менюшки на главной
	$("#nall, #top_m, #skoro, #reit, #nmario").hide();
	$(".rating li:first").addClass('fir');
	var vis = $(".rating li.vis");
	vis.html("<span>" + vis.text() + "</span>");
	$("#" + vis.attr("alt")).show();

	$(".rating li").click(function()
	{
		$(this).parent().find("li").each(function()
		{
			$(this).html("<a>" + $(this).text() + "</a>");
		});

		$("#nall, #nmario, #top_m, #skoro, #reit").hide();
		$(this).html("<span>" + $(this).text() + "</span>");
		$("#" + $(this).attr("alt")).show();
	});

	// Раскрывать правые блоки
	$(".rblock h2").wrapInner("<span><em></em></span>");
	$(".rblock h2").next("*:visible").parent().find("h2 span em").css("background", "url('/image/ico/ar1.png') left 6px no-repeat");
	$(".rblock h2").click(function()
	{
		disp = $(this).next("*").css("display");
		if (disp == "block")
		{
			$(this).find("span em").css("background", "url('/image/ico/ar2.png') 3px 2px no-repeat");
			$(this).next("*").slideUp();
			$(this).parent("*").next(".rblock-dn").css({"top":"-13px"});
			
		}
		else
		{
			$(this).find("span em").css("background", "url('/image/ico/ar1.png') left 6px no-repeat");
			$(".rblock").css('height', 'auto');
			$(this).next("*").slideDown();
			$(this).parent("*").next(".rblock-dn").css({"top":"0"});
		}
	});
	
	$("#zvon").click(function(){
		$("#zvon img").hide();
		$("#zvon h3, #zvi").show();
		$("#zvon").animate({marginLeft:0},600).css("cursor","default");
	});

	$("#ortels").click(function()
	{
		name = $("#orname").val();
		tel = $("#ortel").val();
		$("#zvi").html("Идет отправка данных. подождите...");
		$.get("/ajax/ordertel.php", {ordertel: tel, name: name},function(data){
			$("#zvon p").show();
			if (data) $("#zvi").html(data);
			else $("#zvi").html("Не удалось отправить заявку");
			});
	});
	
	
	$("#victcon, .vilink").click(function(){
		$("#victcon img#victi").hide();
		$("#vivi").text($("#vict").height());
		$("#victcon div, #victcon h3").css("cursor", "default");
		$("#victcon div p").css("visibility", "visible");
		$("#victcon").animate({marginLeft:0},600).css("cursor","default");
	});
	
	$("#victsend").click(function()
	{
		var vv = $("#victa").val();
		$("#victcon div").html("Идет отправка данных. подождите...");
		$.post("/ajax/vict.php", {otv: vv},function(data){
			$("#victcon div").html(data);
		});
		
	});


	$("body").click(function(e)
	{
		var x = e.clientX;
		var y = e.clientY;
		var w = document.body.clientWidth;
		if ((x > 231) || (y < 200 || y > 370))
		{
			$("#zvon").animate({marginLeft:"-153px"}, 600, "swing", function(){$("#zvon h3, #zvi").hide();$("#zvon img").show();$("#zvon").css("cursor","pointer");});
		}

		if ((x < w - 181) || (y < 200 || y > 200+$("#vict").height()))
		{
			if ((y < 135 || y > 170) && y > 30)
			{	
				$("#victcon").animate({marginLeft:150},600, "swing", function()
				{	
					$("#victcon img#victi").show();
					$("#victcon div p").css("visibility", "hidden");
				});
			}
		}
	});
	
	// Сообщение
	if ($("#msgal").size() > 0) alert($("#msgal").text());
	
	// ajax-проверка регистрационных данных 
	$("#edit-name").after('<span id="tstn"></span>');
	$("#edit-mail").after('<span id="tstm"></span>');
	$("#edit-name").keyup(function(e){$("#tstn").load("/ajax/regis_test.php?t=name&n=" + $("#edit-name").val());});
	$("#edit-mail").keyup(function(e) {$("#tstm").load("/ajax/regis_test.php?t=mail&n=" + $("#edit-mail").val());});


});

function open_window(link,w,h){var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=no,scrollbars=no,top=200,left=200";newWin = window.open(link,'newWin',win);};function open_window2(link,w,h){var win = "width="+w+",height="+h+",menubar=yes,location=no,resizable=no,scrollbars=yes,top=20,left=20";newWin = window.open(link,'newWin',win);};function open_window3(link){var win = "width="+450+",height="+150+",menubar=no,location=no,resizable=no,scrollbars=no,top=250,left=250";newWin = window.open(link,'newWin',win);};
