$(function() {
	var bh = $("body").height();
	var bw = $("body").width();
	
	$(document).scrollTop(0);
	
	$(window).resize(function() { center(); });
	center();
	
	if ($.browser.opera && $.browser.version < 9.50) {
		$(".block .t *").addClass("opera");
		$(".block .b *").addClass("opera");
	}
	
	var sec = false;
	if ($("#second").length>0) {
		sec = true;
	}
	
	var img = new Image();
	$(img).load(function() {
		$(document).scrollTop(0);
		$(this).hide();
		$("#bg").append(this);
		
		$(this).css({"left": bw/2-$(this).width()/2+"px"});
		$("#loader div").fadeOut();
		$(this).fadeIn();
		
		if (sec) {
			$("#bg").css({"bottom": "auto", "top": "-478px"});
			var bls = $("div.block");
			if (bls.length == 6) {
				if (bls.eq(2).height()>bls.eq(3).height()) {
					bls.eq(2).css("height",bls.eq(2).height()+"px");
					bls.eq(3).css("height",bls.eq(2).height()+"px");
					bls.eq(4).css("top",bls.eq(3).height()+161+"px");
					$("div.block:last").css("top",bls.eq(2).height()+230+"px");
				} else {
					bls.eq(2).css("height",bls.eq(3).height()+"px");
					bls.eq(3).css("height",bls.eq(3).height()+"px");
					bls.eq(4).css("top",bls.eq(3).height()+161+"px");
					$("div.block:last").css("top",bls.eq(3).height()+230+"px");
				}
			} else if (bls.length == 5) {
				bls.eq(2).css("height",bls.eq(2).height()+"px");
				bls.eq(3).css("top",bls.eq(2).height()+161+"px");
				$("div.block:last").css("top",bls.eq(2).height()+230+"px");
			} else if (bls.length == 8) {
				bls.eq(2).css("height",bls.eq(2).height()+"px");
				bls.eq(5).css("height",bls.eq(2).height()-210+"px");
				bls.eq(6).css("top",bls.eq(2).height()+161+"px");
				$("div.block:last").css("top",bls.eq(2).height()+230+"px");
			}
			
			center();
			
			setTimeout(function() {
				$("div.block").css({"visibility": "visible"});
			}, 100);
		} else {
			$("#bg").css({"bottom": "auto", "top": bh-333+"px"}).animate({"top": "-478px"},bh*4,"easeOutBack");
			setTimeout(function() {
				$("div.block").each(function() {
					var th = $(this);
					var delay = $("div.block").index(th)*(500-parseInt(th.css("top"))/3);
					setTimeout(function() {
						moveBlock(th,800);
					}, delay);
				});
			}, bh*3-(bh*4/2));
		}
		
		var lastTop = parseInt($("div.block:last").css("top"));
		var delay2 = ($("div.block").length-1)*(500-lastTop/3);
		setTimeout(function() {
			$("#lime").css({"visibility": "visible"}).hide().fadeIn(2000);
			$("#lineBot").css({"top": lastTop-96+$("#main").offset().top+"px"}).hide().fadeIn(2000);
		}, delay2+1500);
	}).attr("src", "/images/tpl/bg1.jpg");
	
	/* func2 */
	$("ul.labels li").each(function() {
		var ind = $("ul.labels li").index($(this));
		var len = $("ul.labels li").length;
		if (ind % 5 == 0 || ind > (len - len % 5) ) {
			$("div", $(this)).css("display","none");
		}
	});

	$("ul.prod_descr li").each(function() {
		$(this).css("width",parseInt(100/$("ul.prod_descr li").length)+"%");
	});
	
	$("input.txt1").focus(function () { clearForm(this, $(this).attr("title")) });
	$("input.txt2").focus(function () { clearForm(this, $(this).attr("title")) });

	
	$(".marker_or").hover(function() {
		$(this).find(".popup").show();
	}, function() {
		$(this).find(".popup").hide();
	});
	
	$(".marker_red, .popup_descr").click(function() {
		$(this).parent().parent().find(".popup").show().find(".close").click(function() {
			$(this).parent().hide();
		});
	});

	if ($("ul.nav2").length > 0) {
		var maxhn = 0;
		var maxhm = 0;
		$("ul.nav2 ul").each(function() {
			if ($("li", $(this)).length > maxhn) {
				maxhn = $("li", $(this)).length;
				maxhm = $(this).height();
			}
		});
		$("ul.nav2").height($("ul.nav2").height() + maxhm);
	}
});

function center() {
	var bh = $("body").height();
	var bw = $("body").width();
	var doch = $("div.block:last").offset().top+156;
	$("#root").height(doch);
	if (doch<bh) {
		$("#main").css({"top": bh/2-doch/2+25+"px"});
	}
	$("#bg img").css({"left": bw/2-$("#bg img").width()/2+"px"});
}

function moveBlock(o,del) {
	var bh = $("body").height();
	var bw = $("body").width();
	
	var top1 = o.css("top");
	
	o.css({"visibility": "visible", "top": bh+"px"}).queue(function() {
		$(this).dequeue();
		$(this).animate({"top": top1},del,"easeOutBack");
		
	});
}

function clearForm(o, txt) {
	if (o.value==txt) {
		o.value="";
	}
	o.onblur = function() {
		if (o.value=='') {
			o.value=txt;
		}
	}
}