jQuery(document).ready(
	function()
	{
		function class_toggle(obj,arr,delay)
		{
			if(obj.className)
			{
				if(obj.className==arr[0])
				{
					obj.className=arr[1];
				}
				else
				{
					obj.className=arr[0];
				}
			}
			setTimeout(
				function()
				{
					class_toggle(obj,arr,delay)
				},
				delay
			);
		}
		$('.tabler_div .block').each(
			function(b_num)
			{
				if(b_num==0)
				{
					$(this).css('display','block');
				}
			}
		);
		$('.tabler a').each(
			function(num)
			{
				var a=this;
				$(this).parent().each(
					function()
					{
						if(this.className=='orange')
						{
							class_toggle(this,['orange','gray'],800);
						}
						a.rel=this.className;
						if(num==0)
						{
							this.className='';
						}
					}
				);
				
				$(this).click(
							
					function()
					{
						this.blur();
						
						$('.tabler_div .block').each(
							function(b_num)
							{
								if(num==b_num)
								{
									$('.tabler a').parent().each(
										function(m)
										{
											if(b_num==m)
											{
												p=this;
											}
											else
											{
												f=this;
												$(this).find('a').each(
													function()
													{
														var f_class=this.rel;
														f.className=f_class;
													}
												);
											}
										}
									);
								
									p.className='';
									$(this).css('display','block');
								}
								else
								{
									$(this).css('display','none');
								}
							}
						);
					}
				);
			}
		)
	}
);

function change_tab(num){
	for(i=1; i<=5; i++){
		if(i != num){
			//if(document.getElementById('td_'+i).style.display != 'none')
			//	document.getElementById('td_'+i).style.display = 'none';
			if(document.getElementById('block_'+i).style.display != 'none')
				document.getElementById('block_'+i).style.display = 'none';
				jQuery("#td_"+i).addClass("gray");
			//document.getElementById('td_'+i).addClass("gray");
		}
	}
	jQuery("#td_"+num).removeClass("gray");
	//document.getElementById('td_'+num).style.display = 'block';
	document.getElementById('block_'+num).style.display = 'block';
}


function resize_compare_div()
{
	$('.compare_div .bg_center').each(
		function()
		{
			var obj=this;
			$('.main_div').each(
				function()
				{
					var w=$(this).attr('offsetWidth');
					var wt=w-50;
					$(obj).css('width',wt);
					
					var o=$('.compare_div');
					$(o).css('width',w);
					
					if($(o).css('display')!='block')
					{
						if($.browser.msie==true && $.browser.version<9)
						{
							$(o).css('display','block');
							$(o).css('visibility','visible');
						}
						else
						{
							$(o).fadeIn('fast');
						}
					}
				}
			);
		}
	);
}

$(window).resize(function() {
	resize_compare_div();
});

jQuery(document).ready(
	function()
	{
		setTimeout(
			function()
			{
				resize_compare_div();
			}
			,
			800
		);
	}
);



//add to compare
function add_to_compare_all(compare_els, iblock){	
			var compare_data = {};
			compare_data['id'] = compare_els;
			compare_data['action'] = "ADD_TO_COMPARE_LIST";
			compare_data['iblock'] = iblock;
			
			$.ajax({
					url: '/bitrix/templates/termex/ajax/add_to_compare_all.php',
					data: compare_data,
					type: 'POST',
					dataType: 'html',
					/*beforeSend: function() {
                        jQuery("#compare_loader_"+compare_el_id).show();
						jQuery("#compare_text_"+compare_el_id).hide();
                    },*/
					success: function (result){
						if(result){
							get_compare_count(iblock);
							document.getElementById('compare_all_a').innerHTML = result;
							document.getElementById('mess').style.display = 'block';
						}
get_compare_count(iblock);
						compare_list(iblock);
					}
				});
	
}
function get_compare_count(iblock_id){
  var compare_count = {};
      compare_count['iblock'] = iblock_id;
	$.ajax({





					url: '/bitrix/templates/termex/ajax/compare_count.php',
					data: compare_count,
					type: 'POST',
					dataType: 'html',
					success: function (result){
						document.getElementById('num_in_compare').innerHTML = result;
					}
				});
}


function check_compare(iblock_id, section_id, el){
	//alert(iblock_id+" | "+section_id+" | "+el);
	
  var check_compare_data = {};
      check_compare_data['iblock'] = iblock_id;
      check_compare_data['section'] = section_id;
	$.ajax({
					url: '/bitrix/templates/termex/ajax/check_compare.php',
					data: check_compare_data,
					type: 'POST',
					//dataType: 'html',
					success: function (result){
						if(result != 1){						
							document.getElementById('num_in_compare').innerHTML = Number(document.getElementById('num_in_compare').innerHTML ) + 1; 
							add_to_compare(el, iblock_id); 
							document.getElementById('compare'+el).innerHTML = 'Товар в сравнении';
						}
						else{
							confirm_compare(iblock_id, el);
						}
					}
				});
}
function check_compare_detail(iblock_id, section_id, el){
  var check_compare_data = {};
      check_compare_data['iblock'] = iblock_id;
      check_compare_data['section'] = section_id;
	$.ajax({
					url: '/bitrix/templates/termex/ajax/check_compare.php',
					data: check_compare_data,
					type: 'POST',
					//dataType: 'html',
					success: function (result){
						if(result != 1){						
							document.getElementById('num_in_compare').innerHTML = Number(document.getElementById('num_in_compare').innerHTML ) + 1; 
							add_to_compare(el, iblock_id); 
							document.getElementById('compare').innerHTML = '<img src="/bitrix/templates/termex/images/good_in_compare.gif" />';
						}
						else{
							confirm_compare(iblock_id, el);
						}
					}
				});
}

function confirm_compare(iblock_id, el)
{
	var r=confirm("В сравнении присутствуют товары другого раздела. Удалить и добавить в сравнение текущий товар нажмите ОК, продолжить без изменений нажмите Отмена");
	if (r==true)
	  {
	  	delete_all_from_compare(iblock_id);
		document.getElementById('num_in_compare').innerHTML = "1"; 
		add_to_compare(el, iblock_id); 
		document.getElementById('compare'+el).innerHTML = 'Товар в сравнении';
		compare_list(iblock_id);
	  }
}
function confirm_compare_all(iblock_id, col_mas)
{
	var r=confirm("В сравнении присутствуют товары другого раздела. Удалить и добавить в сравнение текущий товар нажмите ОК, продолжить без изменений нажмите Отмена");
	if (r==true)
	  {
	  	delete_all_from_compare(iblock_id);
		//document.getElementById('num_in_compare').innerHTML = "1"; 
		//add_to_compare(el, iblock_id); 
		//document.getElementById('compare'+el).innerHTML = 'Товар в сравнении';
		//compare_list(iblock_id);
		add_to_compare_all(col_mas, iblock_id);
	  }
}

function delete_all_from_compare(iblock_id){
   var del_all_from_compare = {};
   del_all_from_compare['iblock'] = iblock_id;
				$.ajax({
					url: '/bitrix/templates/termex/ajax/delete_all_from_compare.php',
					data: del_all_from_compare,
                    type: 'POST',
					dataType: 'html',
					success: function (result){
						//compare_list(iblock_id);
					}
				});
}


function add_to_compare(compare_el_id, iblock){	
			var compare_data = {};
			compare_data['id'] = compare_el_id;
			compare_data['action'] = "ADD_TO_COMPARE_LIST";
			compare_data['iblock'] = iblock;
			
			$.ajax({
					url: '/bitrix/templates/termex/ajax/add_to_compare.php',
					data: compare_data,
					type: 'POST',
					dataType: 'html',
					/*beforeSend: function() {
                        jQuery("#compare_loader_"+compare_el_id).show();
						jQuery("#compare_text_"+compare_el_id).hide();
                    },*/
					success: function (result){
						/*document.getElementById('compare_text_'+compare_el_id).innerHTML = result;
						jQuery("#compare_loader_"+compare_el_id).hide();
						jQuery("#compare_text_"+compare_el_id).show();*/
						compare_list(iblock);
					}
				});
}
//end add to compare
function compare_list(iblock_id){
var list_compare_data = {};
list_compare_data['iblock'] = iblock_id;

				$.ajax({
					url: '/bitrix/templates/termex/ajax/compare_list.php',
					data: list_compare_data,
                                        type: 'POST',
					dataType: 'html',
					success: function (result){
						if(document.getElementById('compare_list'))
							document.getElementById('compare_list').innerHTML = result;
					}
				});
}



function delete_from_compare(iblock_id, el_id){
   var del_from_compare = {};
   del_from_compare['iblock'] = iblock_id;
   del_from_compare['id'] = el_id;

				$.ajax({
					url: '/bitrix/templates/termex/ajax/delete_from_compare.php',
					data: del_from_compare,
                                        type: 'POST',
					dataType: 'html',
					success: function (result){
						compare_list(iblock_id);
document.getElementById('num_in_compare').innerHTML = Number(document.getElementById('num_in_compare').innerHTML ) - 1;
					}
				});
}

//проверка email на валидность
	function ch_email(id_mail_input){
		var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");  
		var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");  
		var isOK = !r1.test(id_mail_input) && r2.test(id_mail_input);
		if(isOK){ 
			return true;
		} else {
			return false;	
		}
	}
//конец проверка email на валидность
//позиционировани окна по центру экрана
jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

//для вызова $('#pre_order').center();
//позиционировани окна по центру экрана


//слой предзаказа, список товара
function pre_order_send(k1, k2, k3, k4, k5, k6, k7, k8, k9){
	//k1-название и код товара
	//k2-количество
	//k3-имя
	//k4-емейл
	//k5-телефон
	//k6-комент
	//k7-id web-form
	//k8-id блока куда будет выводиться результат
	//k9-e
	
var pre_order_data = {};
pre_order_data['name'] = k1;
pre_order_data['col'] = k2;
pre_order_data['fio'] = k3;
pre_order_data['email'] = k4;
pre_order_data['phone'] = k5;
pre_order_data['comment'] = k6;
pre_order_data['web_form'] = k7;
pre_order_data['e'] = k9;

	$.ajax({
		url: '/bitrix/templates/termex/ajax/pre_order.php',
		data: pre_order_data,
        type: 'POST',
		dataType: 'html',
		beforeSend: function() {
		   //jQuery("#"+k9).hide();
		   document.getElementById(k8).innerHTML = '<img height="15" width="125" src="/bitrix/templates/termex/images/loading.gif">';
        },
		success: function (result){
			if(result != 1){
				if(document.getElementById(k8)){
					document.getElementById(k8).innerHTML = result;
				}
			}else{
				if(document.getElementById(k8)){
					document.getElementById(k8).innerHTML = "<font color='green'>Спасибо! Ваша заявка принята. Наши менеджеры свяжутся с Вами в ближайшее время.</font><br />";
					setTimeout(
							function() {
							  	$("#layer"+k9).slideUp(3000)
							}
						,
						4000
					);
				}
			}
		}
		});
}

/*расчёт емс стоимости доставки*/

function calculate_ems(weight, price, location_to, system){
   var ems = {};
	   ems['WEIGHT'] = weight;
	   ems['PRICE'] = price;
	   ems['LOCATION_TO'] = location_to;
	   ems['SYSTEM'] = system;

				$.ajax({
					url: '/bitrix/templates/termex/ajax/calculate_ems.php',
					data: ems,
                    type: 'POST',
					dataType: 'html',
					success: function (result){
						if(result != 1){
							document.getElementById('ems_price').innerHTML = result;
							document.getElementById('no_avto_deliv_id').value = "1";
                                                        check_radio('ems');
						}else{
							document.getElementById('no_avto_deliv_id').value = "";
							document.getElementById('ems_price').innerHTML = '<font color="#ff0000">Не удалось рассчитать стоимость доставки!</font>';
							document.getElementById('cart_button').innerHTML = '<div class="small_button_left_grey"></div><div class="small_button_fon_grey"><div>Оформить заказ</div></div><div class="small_button_right_grey"></div>';	
						}
					}
				});
}
