﻿$(document).ready(function(){

	// карта яндекс
	$('.fix_scroll').height($(window).height()-450);
	
	$('.magazin_pop .right').click(function(){
		if($(this).hasClass('goroda')){
			return false;		
		}
		$(this).closest('.pop_white_bl_01').next().slideUp(function(){
			$('#links').hide();
			$('#mapMenu').show();
		});
		$(this).closest('.pop_white_bl_01').next().slideDown();
		$(this).addClass('goroda');
	});
	
	
	$('.hidden_list').click(function(){
		$(this).closest('.pop_white_bl_02').slideUp();
		$('.magazin_pop .right').removeClass('goroda');
	});
	
	// конец карта
	
	
	// поле "код" в форме, после трёх символов переходим на след поле
	$('input.width_cf_01, input.c_form_text_width_01').keyup(function(){
		 if($(this).val().length>2){
				$(this).next('input').focus();
				return false;
		 }
	});
	
	// центрируем изображения в каталоге
	if($('.center_js').length >0){
		if($(window).height() > 820){
			$('.center_js').css('margin-top',50+(($(window).height()-820)/2)+'px');
		}
//		alert($(window).height());
	}
	
	
	// добавляем отступы для изображений добавленых через виз редактор
	if($('.content_page').find('img[style="float: left;"]').length>0) {
		$('.content_page').find('img[style="float: left;"]').css({'margin':'0 20px 10px 0','display':'block'})
	}
	if($('.content_page').find('img[style="float: right;"]').length>0) {
		$('.content_page').find('img[style="float: right;"]').css({'margin':'0 0 10px 20px','display':'block'})
	}
	if($('.content_page').find('img[align="left"]').length>0) {
		$('.content_page').find('img[align="left"]').css({'margin':'0 20px 10px 0','display':'block'})
	}
	if($('.content_page').find('img[align="right"]').length>0) {
		$('.content_page').find('img[align="right"]').css({'margin':'0 0 10px 20px','display':'block'})
	}
	
	// кнопка показать ещё
	$(".submit_big_center,.views_all_corm").click(function(){
		var $this = $(this);
		if($("input.nextLink:last").val().length < 1){
			return false;
		}else{
			var curPos = $("input.nextLink:last").attr("curPos");
		}
		$(".submit_big_center a, .views_all_corm a").hide();
		$(".submit_big_center img, .views_all_corm img").show();
		$.get(location.href ,{isNaked:1,curPos:curPos,cur_cc:49} , function(data) {
			if(data){
				data = data.replace(/[\s\S]*? id='for_get'>/, "").replace(/<\/div><!--for_get-->[\s\S]*/, "");
				$('#for_get').append(data);
				if($("input.nextLink").length > 1){
					if($("input.nextLink:last").val().length < 1){
						$(".submit_big_center,.views_all_corm").hide();							
					}else{
						$(".submit_big_center a, .views_all_corm a").show();
						$(".submit_big_center img, .views_all_corm img").hide();
					}
				}
			}
		});
	}); 

	// скрываем / показываем окна
	$(".dostavka_popup").click(function() {
            showPopup($("#popup1"));
	});
	$(".zakaz_popup").click(function() {
            showPopup($("#popup2"));
	});

	$(".close_pop, #black-sprite, .close_my").click(function () {
		$(".popup").hide();
		$("#black-sprite").hide();
	}); 
	
	
	$('.check_a').hover(
		function(){ //over
		var o = $(this);
		 o.prev('.catalog_tovar_all').fadeIn();
				  },
		function(){ //out
		var o = $(this);
			setTimeout(function(){
				if(o.prev().hasClass('tmp')){
					return false;
				 } else {
					  o.prev('.catalog_tovar_all').fadeOut();
				 }
			},300);
	});
	
	$('.catalog_tovar_all').hover(
		function(){ //over
			if($(this).hasClass('none')){
				 $(this).addClass('tmp');
			}
				  },
		function(){ //out
			if($(this).hasClass('none')){
			  $(this).removeClass('tmp').fadeOut();
			 }
	});
	
	$('.tehnology_bl').hover(
		function(){ //over
		 $(this).find('.pop_info_w').fadeIn();
				  },
		function(){ //out
		  $(this).find('.pop_info_w').fadeOut();
	});
		
		$(".catalog_tr_block:nth-child(2n)").addClass("catalog_tr_block_right").css('clear','both');
		$('.dop_menu').prev().prev().children().children().attr('href','javascript:;');
		$('.pop_left_m').prev().children().attr('href','javascript:;').css('cursor','default');
		$('.pop_left_3m').parent().children().attr('href','javascript:;').css('cursor','default');
		$('.activ_dop').closest('.dop_menu').prev().prev().children().children().css('text-decoration','none');
		$('.activ_dop').closest('.dop_menu').show();
	// выдвигающееся меню 2-го уровня
	$('.left_menu .lm').click(function(){
		if($(this).next().next().hasClass('dop_menu')){
			if($(this).hasClass('tmp2')){
				return false;
			} else {
				$('.dop_menu').slideUp();
				$('.left_menu .lm').removeClass('tmp2');
				$(this).next().next().slideDown();
				$(this).addClass('tmp2');
			}
		}
	});

	// повторная отправка формы
	$('.send_message_ok a').click(function(){
		$('.form_obj input,.form_obj textarea').val("");
		$('.title_green_mini_02').addClass('open');
		$('.send_message_ok').hide();
		$('.contacts_forma').slideDown();
	});
	$('.send_forma_ok a').click(function(){
		$('.forma_contacts .c_form_text, .forma_contacts textarea').val("");
		$('.forma_contacts .send_ok').hide();
		$('.forma_contacts > .gray_back_bl').slideDown();
	});
/*	$('.gray_back_bl_text textarea').focus();
	// по нажатию "enter"
		$(document).keydown(function(event) {
			if($('.title_green_mini_02').hasClass('open')){
				if (event.keyCode == '13') {
					$('.contacts_forma .send_cform').focus().trigger('click');
					return false;
				}
			}
		});
*/	
	// проверка полей и отправка формы
	$(".send_cform").click(function(){
		var check = 0;
		var o = $(this);
		var f = $(this).closest('form');
		f.find(".valid").each(function(index, value){
			if(value != undefined){
				if ($(value).attr('value') != ""){
					$(this).removeClass('tmp');
					$(this).parent().find('span').css('color','');
				} else {
					$(this).addClass('tmp');
					$(this).parent().find('span').css('color','#a8110a');
					check+=1;
				}
			}
		});
		if(check == 0){
			f.parent().find('.error_form').slideUp();
			o.hide();
			f.find('.loading_pic').show();
			$.post("/netcat/add.php",f.serialize(),function(data){
				if(data){
					f.parent('.contacts_forma').hide();
					f.parent().prev().children('.title_green_mini_02').removeClass('open');
					f.parent().next('.send_message_ok').fadeIn();
					o.show();
					f.find('.loading_pic').hide();
				} else {
				}
			});
			return false;
		} else{
			f.parent().find('.error_form').slideDown();
			f.find('.tmp:first').focus();
			return false;
		}
	});


	// проверка полей и отправка формы
	$(".send_cform2").click(function(){
		var check = 0;
		var o = $(this);
		var f = $(this).closest('form');
		f.find(".valid").each(function(index, value){
			if(value != undefined){
				if ($(value).attr('value') != ""){
					$(this).removeClass('tmp');
					$(this).parent().prev().find('span').css('color','');
				} else {
					$(this).addClass('tmp');
					$(this).parent().prev().find('span').css('color','#a8110a');
					check+=1;
				}
			}
		});
		if(check == 0){
			f.parent().find('.error_conform').slideUp();
			o.hide();
			f.find('.loading_pic').show();
			$.post("/netcat/add.php",f.serialize(),function(data){
				if(data){
					f.parent().parent('.gray_back_bl').hide();
					f.parent().next('.send_message_ok').fadeIn();
					f.parent().parent().next('.send_ok').fadeIn();
					o.show();
					f.find('.loading_pic').hide();
				} else {
				}
			});
			return false;
		} else{
			f.parent().find('.error_conform').slideDown();
			f.find('.tmp:first').focus();
			return false;
		}
	});


	// открытие закрытие формы
	$('.open_send').click(function(){
		$('.send_message_ok').hide();
		if($(this).parent().parent().hasClass('open')){
			$('.contacts_forma').slideUp();
			$(this).parent().parent().removeClass('open');
			return false;
		}
		$('.contacts_forma').slideDown();
		$(this).parent().parent().addClass('open');
	});
	$('.open img').hover(function(){
		return false;
	});

	// поправки по меню
	$('.pop_left_m').parent('.lm').addClass('contact_m');
	$('.pop_left_3m').parent().addClass('menu3p');
	if($('.dop_header_menu').length>0){
		$('body').addClass('old_background');
	}
	// украшаем контент
	$('.content_page ul').addClass('list_c_page');



	if($('.ya_maps').length > 0){ // смотрим есть ли блок с картой на этой странице, если да то выполняем
		var _w = $(window).height();  // узнаем текущую высоту окна браузера
//		_w = _w - 175; // вычитаем из неё footer и header
		$(".ya_maps").css({'height':(_w-175)+'px','overflow':'hidden','z-index':'1'}); // присваиваем полученную высоту
//		$(".header_center").css({'position':'relative','z-index':'-1'}); // чтоб "усики" header-а не налазили на карту  
		
		// а это чтоб убрать лицензионное соглашение
		setTimeout(function(){   // ждём 0,1сек а потом удаляем
			$('.YMaps-logo-link').remove();
			$('.YMaps-copyrights').remove();
		}, 100);
	}
});





function showPopup(popup){
    $(popup).css('display','block');
 	$(".pop_c2").hide();
	$(".pop_c").show();
    $(popup).fadeIn();
	
    centerizePopup($(popup));
    $(document.getElementById('black-sprite')).css('display','block');
}

function hidePopup(popup){
    $(popup).css('display','none');
    $(document.getElementById('black-sprite')).css('display','none');
}


function centerizePopup(item){
    item.css('margin-left',(-item.outerWidth()/2));
    if($(window).height() > item.outerHeight()){
        item.css('margin-top',-item.outerHeight()/2);
        item.css('top','25%');
        item.css('position','fixed');
    } else {
        item.css('margin-top',0);
        item.css('top',0);
        item.css('position','absolute');
        $(document).scrollTop(0);
    }
}




