$(document).ready(function(){
	$.getJSON("http://krovinfo.ru/tpl/includeAreas/managerload.php?ajax=Y&callback=?", function(data){
		$('#contactsTop').html(data.txt);
	});

	function mainFormSubmit(error) {
		var errorArr;
		$('div#mainFormMessage').removeAttr('class').text();
		$('#mainContent form div.input input').removeAttr('class');
		if (error == 'bingooo') {
			$('div#mainFormMessage').addClass('green').text('Сообщение успешно отправлено');
		} else {
			errorArr = error.split(':');
			$('div#mainFormMessage').addClass('error').text(errorArr[1]);
			$(errorArr[0]).addClass('error');
		}
	}
	
	$('div#mainFormSubmit input').click(function(){
		var $getAss = $('form#mainForm').serialize();
		$.post(
			'/content/contacts/sendform.php?'+$getAss,
			function(data){
				mainFormSubmit(data);
			}
		)
		return false;
	});

	$("#content div.catalog-element table tr").each(function(i) {
		if (i > 0) {
			if (i % 2 == 1) {
				$(this).addClass('odd');
			}
			/*$(this).hover(function(){
				$(this).addClass('hover');
			}, function(){
				$(this).removeClass('hover');
			});*/
		}
	});
	
	$("#content div.mainPage div.item").each(function(i) {
		$(this).hover(function() {
			$(this).find('a.hover').stop().animate({opacity: 1}, 200);
		}, function() {
			$(this).find('a.hover').stop().animate({opacity: 0}, 200);
		});
	});
	
	$("#content div.mainPage div.manager").each(function(i) {
		$(this).hover(function() {
			$(this).find('a.manager').stop().animate({opacity: 1}, 200);
		}, function() {
			$(this).find('a.manager').stop().animate({opacity: 0}, 200);
		});
	});
	
	/*Функция проеверки формы*/
	function check_form(){
		var errors= new Array();
		if($("#backcall input[name='fio']").length>0 && ($("#backcall[name='fio']").val()!='')){
			var temp=$("#backcall input[name='fio']").val();
			var temp_mask = /^[а-яА-Я\.\,a-zA-z]{2,}[\s\.\,]{0,}[а-яА-Я\.\,a-zA-z\s]{0,}$/i;
			var ar_temp= new Array();
			ar_temp["pole"]="fio";
			ar_temp["alert"]="Вы не верно ввели имя!";
			if(!temp_mask.test(temp) || temp=="Представьтесь"){
				errors.push(ar_temp);
			}
		}
		if($("#backcall input[name='phone']").length>0 && ($("#backcall input[name='phone']").val()!='')){
			var temp=$("#backcall input[name='phone']").val();
			var temp_mask = /^[\+\s\-\(\)0-9]{5,15}$/i
			var ar_temp= new Array();
			ar_temp["pole"]="phone";
			ar_temp["alert"]="Вы не верно ввели телефон!";
			if(!temp_mask.test(temp)){
				errors.push(ar_temp);
			}
		}
		if(errors.length>0){
			for(var i=0;i<errors.length;i++){
				var div_alert="<div class=\"comments warning\" >"+errors[i]["alert"]+"</div>"
				if($("#backcall input[name='"+errors[i]["pole"]+"']").length>0){
				   $("#backcall input[name='"+errors[i]["pole"]+"']").addClass("error");
				}
			}
			return false;
		}
	}
	
	/*call back*/
	$(".backcall").click(function(){
		$('#bg_green').fadeIn(400).click(function(){
			$('#backcall').fadeOut(400);
			$(this).fadeOut(400);
		});
		
		$('#backcall').fadeIn(400);
		$('.picclose').click(function(){
			$('#backcall').fadeOut(400);
			$('#bg_green').fadeOut(400);
		})
		
		$("#backcall input[name=fio]").focus(function(){
			if($(this).val()=="Представьтесь"){
					$(this).val("");			
					$(this).removeClass("error")
			}
			$(this).blur(function () {
				if($(this).val()=="")
					$(this).val("Представьтесь");
			})
		})
		
		$("#backcall input[name=phone]").focus(function(){
			if($(this).val()=="Ваш номер телефона"){
				$(this).val("");
				$(this).removeClass("error");			
			}
			$(this).blur(function () {
				if($(this).val()=="")
				$(this).val("Ваш номер телефона");
			})
		})
		return false;
	});
	
	$('#backcall').ajaxForm({
		beforeSubmit: check_form,
		success: function(data) {
			if (/error_send/.test(data)) {
				$("#backcall .alert").replaceWith("");
				$("#backcall .warning").replaceWith("");							
				$('#backcall').html("Ошибка при отправке сообения!");	
				$('#backcall').addClass("red");
				$('#backcall').slideDown("slow");
			}
			if (/ok/.test(data)) {
				$('#backcall .inputs').clearForm();
				$('#backcall .inputs').hide();
				$('#backcall .footer').fadeIn("slow");
			}
		}
	});
	
	/*$('#slider').hover(function(){
		$(this).find('div.preview').stop().animate({height: 68}, 200);
	}, function(){
		$(this).find('div.preview').stop().animate({height: 0}, 200);
	});*/
	
	if ($('#topImages').size() > 0)
		$('#topImages').codaSlider();
	
});
