jQuery(function($){

	function getBasketHTML(html)
	{
		txt = html.split('<!--start--><div id="bid">');
		txt = txt[1];
		txt = txt.split('</div><!--end-->');
		txt = txt[0];
		return txt;
	}
	$('input[name*="actionADD2BASKET"]').click(                
		function(){
			parent = $(this).parent();
			quantity_val = $('input[name*=quantity]').attr('value');
			id_val = $('input[name*=id]').attr('value');
			$.ajax({
				type: "post",
				url: parent.attr('action'),
				data: {quantity: quantity_val, id: id_val, actionADD2BASKET: 'В корзину', action: "BUY"},
				dataType: "html",
				success: function(out){
					$("#bid").html(getBasketHTML(out));
				}

			});
			$(".item_added").show();
			$(".item_added").fadeOut(3000);
		return false;
		}
	);
		
	$("#login_type").val(" ");
	$(".form_auto input[type=text]").mask("(999) 999-99-99"); //маска для формы авторизации
	$("#hid_left_log_1").mask("(999) 999-99-99"); //маска для формы авторизации
	$("#autho_type").mask("(999) 999-99-99");

	$(".content").css("min-height", ($(".left_menu").height() - 383));  //подгон высоты контента
	
	
	/* ------------------------------------------ */
	$("#down_arrow").click(function(){	//разворачивание корзины
		var currentHeight = $(".basket_midl").height();
		$(".basket_midl").css('height', 'auto');
		var height = $(".basket_midl").height();
		$(".basket_midl").css('height', currentHeight + 'px');
		$(".basket_midl").animate({'height': height});
		$("#down_arrow").hide();
		$("#up_arrow").show();
	});
	/* ------------------------------------------ */
	$("#up_arrow").click(function(){ //сворачивание корзины
		$(".basket_midl").animate({height: "135"});
		$("#up_arrow").hide();
		$("#down_arrow").show();
	});
	/* ------------------------------------------ */
	$(".big_basket_form input[type=checkbox]").change(function(){  //кастомные галочки
		if($(this).is(":checked")){
			$(this).next("label").addClass("LabelSelected");
		}else{
			$(this).next("label").removeClass("LabelSelected");
		}
	});
	/* ------------------------------------------ */
	var product_item_count = $(".product_item_detals").length;  //подгон высоты блоков
	var product_item_max_height = 160;	
	for (i = 0; i < product_item_count; i++) {  
		if ($("#product_item_"+i).height() > product_item_max_height) {
			
			product_item_max_height = $("#product_item_"+i).height();
		}
	}
	$(".product_item_detals").height(product_item_max_height);
	/* ------------------------------------------ */	
	if ($(window).width() < '1200') {		//подгон ширины сайта
		$(".content").addClass("narrow_content");
		$(".left_menu").addClass("narrow_menu");
		$(".navigation").addClass("navigation_narrow");
		$(".basket").addClass("basket_narrow");
	}
	else if ($(window).width() > '1200') {
		$(".content").removeClass("narrow_content");
		$(".left_menu").removeClass("narrow_menu");
		$(".navigation").removeClass("navigation_narrow");
		$(".basket").removeClass("basket_narrow");
	};
	$(window).resize(function(){
		if ($(window).width() < '1200') {
			$(".content").addClass("narrow_content");
			$(".left_menu").addClass("narrow_menu");
			$(".navigation").addClass("navigation_narrow");
			$(".basket").addClass("basket_narrow");
		}
		else if ($(window).width() > '1200') {
			$(".content").removeClass("narrow_content");
			$(".left_menu").removeClass("narrow_menu");
			$(".navigation").removeClass("navigation_narrow");
			$(".basket").removeClass("basket_narrow");
		};
	});
	
	
	$('#login_type').change(function(){
		var hphone = $('#login_type').attr("value");
		hphone = hphone.replace(/ /g,'');
		hphone = hphone.replace(/\(/g,'');
		hphone = hphone.replace(/\)/g,'');
		hphone = hphone.replace(/\-/g,'');
		$('.to_register').attr("href", '/registration.php?phone_n='+hphone);
	});
	

	$('#form_scroll').click(function(){
		$('#form_scroll').hide();
		$('#back_mail').animate({height: "498px"}, 1000);
		
	});
	
	var options = {
		
		url: "/back_mail.php",
		beforeSubmit: function() {
			var error_message = '';
			if (($('#back_form_name').val() == 'Имя')||($('#back_form_name').val() == '')) { error_message = 'Не заполнено поле Имя'; }
			if (($('#back_form_lastname').val() == 'Фамилия')||($('#back_form_lastname').val() == '')) { error_message = error_message + '\r\nНе заполнено поле Фамилия'; }
			if (($('#back_form_mail').val() == 'e-mail')||($('#back_form_mail').val() == '')) { error_message = error_message + '\r\nНе заполнено поле e-mail'; }			
			if ($('#back_form_mesage').val() == '') { error_message = error_message + '\r\nНе заполнено поле сообщения'; }
			if (error_message != '') {
				alert(error_message);
				return false;
			}
			else return true;
		},
		success: function() {
			$('#back_mail').html('<h4>Обратная связь</h4><div class="form_succ"></div>');
			$('#back_mail').animate({height: "34px"}, 1000);	
		}
	};
	$('#back_form').submit(function() {
		$(this).ajaxSubmit(options);
		return false;
	}); 
	$('#registr_subm').click(function(){
		var qqq = $('#hid_left_log_2').val();
		if( /_/.test(qqq) ){
			return false;
		}
	});
});




function tell2(obj)
{
	var hphone = $(obj).val();
	hphone = hphone.replace(/ /g,'');
	hphone = hphone.replace(/\(/g,'');
	hphone = hphone.replace(/\)/g,'');
	hphone = hphone.replace(/\-/g,'');
	$("#hid_left_log").attr('value',hphone);
	$("#hid_left_log_2").attr('value',hphone);
	$("#hid_left_log_2_mail").attr('value',hphone);
	$("#autho_type_2").attr('value',hphone);
	
}
function showPass()
{
	if($("#i_have_pass").css("display")=='none')
	{
		$("#i_have_pass").show();
		$("#pasw").attr({value:""});
	}
	else
	{
		$("#i_have_pass").hide();
		$("#pasw").attr({value:"123456"});
	}
}
function showEmail()
{
	if($("#i_wont_mail").css("display")=='none')
	{
		$("#i_wont_mail").show();
		$("#reg_mail").attr({value:""});
	}
	else
	{
		$("#i_wont_mail").hide();
		$("#reg_mail").attr({value:"none@none.com"});
	}
}

function addToCart(obj)
{
   var id = $(obj).prev().prev().prev().val();
   var price = $(obj).prev().prev().val();
   var quantity = $(obj).prev().prev().prev().prev().prev().val();
   var name = $(obj).prev().val();
   $.ajax({
       url: '/bitrix/components/user/catalog.section/ajax.php',
       data: "ID="+id+"&quantity="+quantity+"&PRICE="+price+"&NAME="+name,
       dataType : "html",
       success: function (data) {
         $("#bid").html(data);
         $(obj).next().show();
		 $(obj).next().fadeOut(3000);
       }
   });
}

function  increase(obj,act)
{
   var id = obj.val();
   if(act=="+")
     id++;
   else
     id--;
   if (id<=1)
     id=1;
   obj.attr("value",id);
}

