src_txt = 'Поиск по блогу';
$(document).ready(function(){
	top_scrol = function (s) {
		if ($.browser.safari) {
			$("body").animate({ scrollTop: s* -1}, 300);
		}else{
			$("html").animate({ scrollTop: s* -1}, 300);
		}
	}
	$("input[name='s']").focus(function () {
		if($(this).val() == src_txt)
			$(this).val('');
	});
	$("input[name='s']").blur(function () {
		if($(this).val() == '')
			$(this).val(src_txt);
	});
	if($("#txt").length){
		if ( $("#dscr_l").is(":not(.act)") ){
			$("#txt").height($("#prcs").height());
		}else{
			$("#txt").height($("#dscr").height());
		}
	}
	if($(".faq").length){
		$(".faq h3 a").parent().next("p").slideToggle(300);
	}
	$("#dt a").click(function () {
		$("#dt a").removeClass("act");
		$(this).addClass("act");
		if ( $("#dscr_l").is(":not(.act)") ){
			$("#txt").animate({ height: $("#prcs").height()+'px' }, 500);
			$("#txt div").animate({ left: '-544px' }, 300);
	}else{
			$("#txt").animate({ height: $("#dscr").height()+'px' }, 500);
			$("#txt div").animate({ left: 0 }, 300);
		}
		return(false);
	});
	$("#prt ul li").hover(
		function () {
			$(this).addClass("hvr");
		},
		function () {
			$(this).removeClass("hvr");
		}
	);
	$("#p_c li").hover(
		function () {
			$(this).addClass("hvr");
		},
		function () {
			$(this).removeClass("hvr");
		}
	);
	$(".bt").hover(
		function () {
			$(this).addClass("hvr");
		},
		function () {
			$(this).removeClass("hvr");
		}
	);
	$(".srv li").hover(
		function () {
			$(this).addClass("hvr");
		},
		function () {
			$(this).removeClass("hvr");
		}
	);
	$("#frn li").hover(
		function () {
			$(this).addClass("hvr");
		},
		function () {
			$(this).removeClass("hvr");
		}
	);
	$(".bt a").click(function () {
		pz = $("#p_c table").offset().left - $("#p_c").offset().left;
		st = ($("#p_c table").width() - 864) * -1;
		if ( $(this).parent().is(":not(.pr)") ){
			if(pz > st ){
				$(this).addClass("hd");
				$("#p_c table").animate({ left: (pz-864)+'px' }, 300, function(){
					if(pz-864 != st){
						$(".nx a").removeClass("hd");
					}
					if( $(".pr a").is(".hd") ){
						$(".pr a").removeClass("hd");
					}
				});
			}
		}else{
			if(pz < 0){
				$(this).addClass("hd");
				$("#p_c table").animate({ left: (pz+864)+'px' }, 300, function(){
					if(pz != -864){
						$(".pr a").removeClass("hd");
					}
					if( $(".nx a").is(".hd") ){
						$(".nx a").removeClass("hd");
					}
				});
			}
		}
		return(false);
	});
	$(".faq h3 a").click(function(){
		$(this).parent().next("p").slideToggle(300);
			return false;
	});
	$(".tggl a").click(function(){
		lnk = $(this).attr('href');
		dv = $(this).parent();
		dv.next("div").slideToggle(300, function() {
			location.href = lnk;
		});
		if(dv.is(".hd"))
			dv.slideToggle(300);
		return false;
	});
	$(".prsg th").click(function () {
		$(this).parent().parent().parent().parent().toggleClass("opn");
    });
	$(".prsg td").hover(
		function () {
			$(this).parent().addClass("hv");
		},
		function () {
			$(this).parent().removeClass("hv");
		}
	);
	$(".obt").hover(
		function () {
			$(this).addClass("hv");
		},
		function () {
			$(this).removeClass("hv");
		}
	);
	$("#prt > li > a").click(function(){
		cr = $(this).parent();
		if(cr.is(".hd")){
			top_scrol($(this).offset().top);
			$("#prt > li:not(.hd)").find("ul").slideToggle('normal',function(){
				cr.insertBefore("#prt > li:not(.hd)");
				$("#prt > li:not(.hd)").addClass("hd");
				cr.find("ul").slideToggle('normal',function(){
					cr.removeClass("hd");
				});
			});
		}
		return false;
	});
/*	$(".mlm").click(function(){
		top_scrol($(this).offset().top);
		if($("#hdr .mlm").html() == 'Контакты'){
			if($("#t_f").html() == '&nbsp;'){
				$.ajax({
					url:"/kontakty.html",
					data: "topform",
					cache: true,
					dataType:"html",
					success:function(d){
						$("#t_f").html(d);
						$("#t_f").animate({ 'margin-top' : 0 }, 300, function(){
							$("#hdr .mlm").text('Свернуть');
						});
					}
				});
			}else{
				$("#t_f").animate({ 'margin-top' : 0 }, 300, function(){
					$("#hdr .mlm").html('Свернуть');
				});
			}
		}else{
			if ($(this).parent().parent().parent().attr("id") == 'hdr' ){
				$("#t_f").animate({ 'margin-top' : '-273px' }, 300, function(){
					$("#hdr .mlm").html('Контакты');
				});
			}
		}
		return false;
	});
*/	if($("#frn").length){
		$("#frn a").attr('target', '_blank');
	}
	$("#ftr p a, a.blnk").attr('target', '_blank');
	$(".tp a").click(function(){
		top_scrol($(this).offset().top);
		return false;
	});
	chk_txt = function(it){
		if(it.val()==''){
			it.prev("span").slideDown(300);
			return false;
		}else{
			it.prev("span").slideUp(300);
			return true
		}
	}
	chk_ml = function(it){
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		if(pattern.test(it.val())){
			it.prev("span").slideUp(300);
			return true
		}else{
			it.prev("span").slideDown(300);
			return false;
		}
	}
	chk_ext = function(it, ext){
		it_ext = it.val().match(/\.\w{1,4}$/gi);
		accpt = ext.split('|');
		for(var key in accpt){
			if(it_ext == '.' + accpt[key]){
				it.prev("span").slideUp(300);
				return true
			}
		}
		it.prev("span").slideDown(300);
		return false;
	}
	
	$("#add_rwv").submit( function () {
		if(!chk_txt($("#add_rwv input[name='nm']")))
			return false;
		if(!chk_txt($("#add_rwv input[name='ml']")) || !chk_ml($("#add_rwv input[name='ml']")))
			return false;
		if(!chk_ext($("#add_rwv input[name='pht']"), 'jpg|gif|png'))
			return false;
		if(!chk_txt($("#add_rwv textarea")))
			return false;
	});
	$("#ordr").submit( function () {
		if(!chk_txt($("#ordr textarea")))
			return false;
		if(!chk_txt($("#ordr input[name='nm']")))
			return false;
		if(!chk_txt($("#ordr input[name='ml']")) || !chk_ml($("#ordr input[name='ml']")))
			return false;
	});
	chk_sbj = function(){
		it = $("#ml_to select");
		y_it = $("#ml_to input[name='y_sbj']");
		if( it.val() == 0 && y_it.val() == ''){
			it.prev("span").slideDown(300);
		}else{
			it.prev("span").slideUp(300);
		}
		if(it.val() == 0){
			y_it.parent().slideDown(300);
		}else{
			y_it.val('');
			y_it.parent().slideUp(300);
		}
	}
	$("#ml_to select").change(function(){
		chk_sbj();
	});
	$("#ml_to").submit( function () {
		if(!chk_txt($("#ml_to textarea")))
			return false;
		if(!chk_txt($("#ml_to input[name='nm']")))
			return false;
		if(!chk_txt($("#ml_to input[name='ml']")) || !chk_ml($("#ml_to input[name='ml']")))
			return false;
	});

	if($('#ord_fls').length){
		$(function(){
			$('#ord_fls').MultiFile({
				accept:'gif|jpg|jpeg|png|swf|zip|rar|doc|docx|psd|pdf|xls|xlsx|rtf|txt|flv', max:10, STRING:{
				remove:'<img src="/i/del.gif" width="9" height="9"/> ',
				selected:'Выбран файл: $file',
				denied:'Файл не может иметь формат $ext!',
				duplicate:'Файл уже добавлен:\n$file!'
				}
			});
		});
	}
	$("#ordr select[name=ofr]").change(function(){
		$(".prc").html($(this+":selected").attr('class')+'<sup>$</sup>');
	});
	$("p.map a").click(function(){
		if($(this).text() == "Свернуть"){
			$(this).text("См. карту проезда к офису");
		}else{
			$(this).text("Свернуть");
		}
		$(this).next().slideToggle(300);
		return false;
	})
	$(".cnt_op a").click(function(){
		$(this).parent().next().slideToggle(300);
		return false;
	});
});

