$(document).ready(function() {
	var tch=$("#tree_col").height();
	$("#tree_col .cont").height(tch-40);
	
	var tch=$("#second_page").height();
	$("#second_page .cont").height(tch-40);
	
	var tch=$("#second_page_bottom").height();
	$("#second_page_bottom .lc .cont").height(tch-60);
	$("#second_page_bottom .rc .cont").height(tch-60-$("#second_page_bottom .rc .banner").height());
	
	
	$(".slider").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        visible: 3
    });

/*
	$("#type1 li a, #type2 li a").click(function(){
		if($(this).parent("li").find("ul").length>0){
			$("li ul").slideUp("normal");
			if($(this).parent("li").find("ul").is(":hidden")){
				$(this).parent("li").find("ul").slideDown("normal");
			}else{
				$(this).parent("li").find("ul").slideUp("normal");
			}
			return false;
		}
	}); 
	*/
	
	$('a.gellery_element').lightBox({fixedNavigation:true});


	//////////////////ФОРМА
	$("#contacts_form input").focus(function(){
		if($(this).val()=="Представьтесь" || $(this).val()=="E-Mail" || $(this).val()=="Номер телефона") {
				$(this).attr("id",$(this).val()); 
				$(this).val("");
		}	
	}).blur(function(){
		if($(this).val()=='') {
			$(this).val($(this).attr("id"));
		}
	});
	
	$("#contacts_form textarea").focus(function(){
		if($(this).val()=="Ваш вопрос") {
				$(this).attr("id",$(this).html()); 
				$(this).html("");
		}	
	}).blur(function(){
		if($(this).html()=='') {
			$(this).html($(this).attr("id"));
		}
	});
	
	
	//Проверка заполненой формы
	function check_form(a,f,o){
		var ret=true;
		o.dataType = "html";
		
		$("#contacts_form .alert").replaceWith("");
		$("#contacts_form .warning").replaceWith("");
				
		var errors= new Array();
	
		if($("#contacts_form input[name='name']").length>0 && ($("#contacts_form input[name='name']").val()!='' || $("#contacts_form input[name='name']").hasClass("ob"))){
			var temp=$("#contacts_form input[name='name']").val();
			var temp_mask = /^[а-яА-Я\.\,a-zA-z]{2,}[\s\.\,]{0,}[а-яА-Я\.\,a-zA-z\s]{0,}$/i;
			var ar_temp= new Array();
			ar_temp["pole"]="name";
			ar_temp["alert"]="Вы не верно ввели имя!";
			if(!temp_mask.test(temp) || temp=='Представьтесь'){
				errors.push(ar_temp);
			}
		}
		
		if($("#contacts_form input[name='email']").length>0 && ($("#contacts_form input[name='email']").val()!='' || $("#contacts_form input[name='email']").hasClass("ob"))){
			var temp=$("#contacts_form input[name='email']").val();
			var temp_mask = /^[a-zA-Z0-9\-\(\)\.]{1,}[@][a-zA-Z0-9\-\(\)\.]{1,}[\.]{1}[a-zA-Z]{1,4}$/i
			var ar_temp= new Array();
			ar_temp["pole"]="email";
			ar_temp["alert"]="Вы не верно ввели адрес электронной почты!";
			if(!temp_mask.test(temp)){
				errors.push(ar_temp);
			}
		}
		
		
		if($("#contacts_form input[name='telephone']").length>0 && ($("#contacts_form input[name='telephone']").val()!='' || $("#contacts_form input[name='telephone']").hasClass("ob"))){
			var temp=$("#contacts_form input[name='telephone']").val();
			var temp_mask = /^[\s\-\(\)0-9]{5,15}$/i
			var ar_temp= new Array();
			ar_temp["pole"]="telephone";
			ar_temp["alert"]="Вы не верно ввели телефон!";
			if(!temp_mask.test(temp)){
				errors.push(ar_temp);
			}
		}
		
		if($("#contacts_form textarea[name='vopr']").length>0 && ($("#contacts_form textarea[name='vopr']").val()!='' || $("#contacts_form textarea[name='vopr']").hasClass("ob"))){
			var temp=$("#contacts_form textarea[name='vopr']").val();
			var temp_mask = /^[а-яА-Яa-zA-ZäöüÄÖÜßèéû0-9\-\(\)\.\,\s\?\_\!\;]{5,}$/i
			var ar_temp= new Array();
			ar_temp["pole"]="vopr";
			ar_temp["alert"]="Напишите текст сообщения!";
			if(!temp_mask.test(temp) || temp=='Ihre Nachricht'){
				errors.push(ar_temp);
			}
		}

		
		if(errors.length>0){
			var div="<div class=\"alert\"><img src=\"/tpl/images/attention.gif\"/></div>";	
			for(var i=0;i<errors.length;i++){
				var div_alert="<div class=\"comments warning\" >"+errors[i]["alert"]+"</div>";
				if($("#contacts_form input[name='"+errors[i]["pole"]+"']").length>0){
					$("#contacts_form input[name='"+errors[i]["pole"]+"']").before(div);
					//$("#contacts_form input[name='"+errors[i]["pole"]+"']").after(div_alert);
				}else{
					$("#contacts_form textarea[name='"+errors[i]["pole"]+"']").before(div);
					//$("#contacts_form textarea[name='"+errors[i]["pole"]+"']").after(div_alert);
				}
				//alert(errors[i]["alert"]);	
			}
			ret=false;
		}
		return ret;
	}
	
	$('#contacts_form').ajaxForm({
	beforeSubmit: check_form,
	success: function(data) {
			//alert(data);
			if (/ok/.test(data)) {
				$("#contacts_form .alert").replaceWith("");
				$("#contacts_form .warning").replaceWith("");
				$('#contacts_form .but').slideUp("slow");
				
				$('#form_output').html("Ваша заявка была отправлена!");	
				$('#form_output').addClass("green");
				$('#form_output').slideDown("slow");
			}
			
			
			

		}	
	}); 


});

